decNumberLocal.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /* Local definitions for the decNumber C Library.
  2. Copyright (C) 2007-2013 Free Software Foundation, Inc.
  3. Contributed by IBM Corporation. Author Mike Cowlishaw.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* ------------------------------------------------------------------ */
  21. /* decNumber package local type, tuning, and macro definitions */
  22. /* ------------------------------------------------------------------ */
  23. /* This header file is included by all modules in the decNumber */
  24. /* library, and contains local type definitions, tuning parameters, */
  25. /* etc. It should not need to be used by application programs. */
  26. /* decNumber.h or one of decDouble (etc.) must be included first. */
  27. /* ------------------------------------------------------------------ */
  28. #if !defined(DECNUMBERLOC)
  29. #define DECNUMBERLOC
  30. #define DECVERSION "decNumber 3.61" /* Package Version [16 max.] */
  31. #define DECNLAUTHOR "Mike Cowlishaw" /* Who to blame */
  32. #include <stdlib.h> /* for abs */
  33. #include <string.h> /* for memset, strcpy */
  34. #include "dconfig.h" /* for WORDS_BIGENDIAN */
  35. /* Conditional code flag -- set this to match hardware platform */
  36. /* 1=little-endian, 0=big-endian */
  37. #if WORDS_BIGENDIAN
  38. #define DECLITEND 0
  39. #else
  40. #define DECLITEND 1
  41. #endif
  42. #if !defined(DECLITEND)
  43. #define DECLITEND 1 /* 1=little-endian, 0=big-endian */
  44. #endif
  45. /* Conditional code flag -- set this to 1 for best performance */
  46. #if !defined(DECUSE64)
  47. #define DECUSE64 1 /* 1=use int64s, 0=int32 & smaller only */
  48. #endif
  49. /* Conditional check flags -- set these to 0 for best performance */
  50. #if !defined(DECCHECK)
  51. #define DECCHECK 0 /* 1 to enable robust checking */
  52. #endif
  53. #if !defined(DECALLOC)
  54. #define DECALLOC 0 /* 1 to enable memory accounting */
  55. #endif
  56. #if !defined(DECTRACE)
  57. #define DECTRACE 0 /* 1 to trace certain internals, etc. */
  58. #endif
  59. /* Tuning parameter for decNumber (arbitrary precision) module */
  60. #if !defined(DECBUFFER)
  61. #define DECBUFFER 36 /* Size basis for local buffers. This */
  62. /* should be a common maximum precision */
  63. /* rounded up to a multiple of 4; must */
  64. /* be zero or positive. */
  65. #endif
  66. /* ---------------------------------------------------------------- */
  67. /* Definitions for all modules (general-purpose) */
  68. /* ---------------------------------------------------------------- */
  69. /* Local names for common types -- for safety, decNumber modules do */
  70. /* not use int or long directly. */
  71. #define Flag uint8_t
  72. #define Byte int8_t
  73. #define uByte uint8_t
  74. #define Short int16_t
  75. #define uShort uint16_t
  76. #define Int int32_t
  77. #define uInt uint32_t
  78. #define Unit decNumberUnit
  79. #if DECUSE64
  80. #define Long int64_t
  81. #define uLong uint64_t
  82. #endif
  83. /* Development-use definitions */
  84. typedef long int LI; /* for printf arguments only */
  85. #define DECNOINT 0 /* 1 to check no internal use of 'int' */
  86. /* or stdint types */
  87. #if DECNOINT
  88. /* if these interfere with your C includes, do not set DECNOINT */
  89. #define int ? /* enable to ensure that plain C 'int' */
  90. #define long ?? /* .. or 'long' types are not used */
  91. #endif
  92. /* Shared lookup tables */
  93. extern const uByte DECSTICKYTAB[10]; /* re-round digits if sticky */
  94. extern const uInt DECPOWERS[10]; /* powers of ten table */
  95. /* The following are included from decDPD.h */
  96. #include "decDPDSymbols.h"
  97. extern const uShort DPD2BIN[1024]; /* DPD -> 0-999 */
  98. extern const uShort BIN2DPD[1000]; /* 0-999 -> DPD */
  99. extern const uInt DPD2BINK[1024]; /* DPD -> 0-999000 */
  100. extern const uInt DPD2BINM[1024]; /* DPD -> 0-999000000 */
  101. extern const uByte DPD2BCD8[4096]; /* DPD -> ddd + len */
  102. extern const uByte BIN2BCD8[4000]; /* 0-999 -> ddd + len */
  103. extern const uShort BCD2DPD[2458]; /* 0-0x999 -> DPD (0x999=2457)*/
  104. /* LONGMUL32HI -- set w=(u*v)>>32, where w, u, and v are uInts */
  105. /* (that is, sets w to be the high-order word of the 64-bit result; */
  106. /* the low-order word is simply u*v.) */
  107. /* This version is derived from Knuth via Hacker's Delight; */
  108. /* it seems to optimize better than some others tried */
  109. #define LONGMUL32HI(w, u, v) { \
  110. uInt u0, u1, v0, v1, w0, w1, w2, t; \
  111. u0=u & 0xffff; u1=u>>16; \
  112. v0=v & 0xffff; v1=v>>16; \
  113. w0=u0*v0; \
  114. t=u1*v0 + (w0>>16); \
  115. w1=t & 0xffff; w2=t>>16; \
  116. w1=u0*v1 + w1; \
  117. (w)=u1*v1 + w2 + (w1>>16);}
  118. /* ROUNDUP -- round an integer up to a multiple of n */
  119. #define ROUNDUP(i, n) ((((i)+(n)-1)/n)*n)
  120. #define ROUNDUP4(i) (((i)+3)&~3) /* special for n=4 */
  121. /* ROUNDDOWN -- round an integer down to a multiple of n */
  122. #define ROUNDDOWN(i, n) (((i)/n)*n)
  123. #define ROUNDDOWN4(i) ((i)&~3) /* special for n=4 */
  124. /* References to multi-byte sequences under different sizes; these */
  125. /* require locally declared variables, but do not violate strict */
  126. /* aliasing or alignment (as did the UINTAT simple cast to uInt). */
  127. /* Variables needed are uswork, uiwork, etc. [so do not use at same */
  128. /* level in an expression, e.g., UBTOUI(x)==UBTOUI(y) may fail]. */
  129. /* Return a uInt, etc., from bytes starting at a char* or uByte* */
  130. #define UBTOUS(b) (memcpy((void *)&uswork, b, 2), uswork)
  131. #define UBTOUI(b) (memcpy((void *)&uiwork, b, 4), uiwork)
  132. /* Store a uInt, etc., into bytes starting at a char* or uByte*. */
  133. /* Has to use uiwork because i may be an expression. */
  134. #define UBFROMUS(b, i) (uswork=(i), memcpy(b, (void *)&uswork, 2))
  135. #define UBFROMUI(b, i) (uiwork=(i), memcpy(b, (void *)&uiwork, 4))
  136. /* X10 and X100 -- multiply integer i by 10 or 100 */
  137. /* [shifts are usually faster than multiply; could be conditional] */
  138. #define X10(i) (((i)<<1)+((i)<<3))
  139. #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6))
  140. /* MAXI and MINI -- general max & min (not in ANSI) for integers */
  141. #define MAXI(x,y) ((x)<(y)?(y):(x))
  142. #define MINI(x,y) ((x)>(y)?(y):(x))
  143. /* Useful constants */
  144. #define BILLION 1000000000 /* 10**9 */
  145. /* CHARMASK: 0x30303030 for ASCII/UTF8; 0xF0F0F0F0 for EBCDIC */
  146. #define CHARMASK ((((((((uInt)'0')<<8)+'0')<<8)+'0')<<8)+'0')
  147. /* ---------------------------------------------------------------- */
  148. /* Definitions for arbitary-precision modules (only valid after */
  149. /* decNumber.h has been included) */
  150. /* ---------------------------------------------------------------- */
  151. /* Limits and constants */
  152. #define DECNUMMAXP 999999999 /* maximum precision code can handle */
  153. #define DECNUMMAXE 999999999 /* maximum adjusted exponent ditto */
  154. #define DECNUMMINE -999999999 /* minimum adjusted exponent ditto */
  155. #if (DECNUMMAXP != DEC_MAX_DIGITS)
  156. #error Maximum digits mismatch
  157. #endif
  158. #if (DECNUMMAXE != DEC_MAX_EMAX)
  159. #error Maximum exponent mismatch
  160. #endif
  161. #if (DECNUMMINE != DEC_MIN_EMIN)
  162. #error Minimum exponent mismatch
  163. #endif
  164. /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN */
  165. /* digits, and D2UTABLE -- the initializer for the D2U table */
  166. #if DECDPUN==1
  167. #define DECDPUNMAX 9
  168. #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, \
  169. 18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \
  170. 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \
  171. 48,49}
  172. #elif DECDPUN==2
  173. #define DECDPUNMAX 99
  174. #define D2UTABLE {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10, \
  175. 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18, \
  176. 18,19,19,20,20,21,21,22,22,23,23,24,24,25}
  177. #elif DECDPUN==3
  178. #define DECDPUNMAX 999
  179. #define D2UTABLE {0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7, \
  180. 8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13, \
  181. 13,14,14,14,15,15,15,16,16,16,17}
  182. #elif DECDPUN==4
  183. #define DECDPUNMAX 9999
  184. #define D2UTABLE {0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6, \
  185. 6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11, \
  186. 11,11,11,12,12,12,12,13}
  187. #elif DECDPUN==5
  188. #define DECDPUNMAX 99999
  189. #define D2UTABLE {0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5, \
  190. 5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9, \
  191. 9,9,10,10,10,10}
  192. #elif DECDPUN==6
  193. #define DECDPUNMAX 999999
  194. #define D2UTABLE {0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4, \
  195. 4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8, \
  196. 8,8,8,8,8,9}
  197. #elif DECDPUN==7
  198. #define DECDPUNMAX 9999999
  199. #define D2UTABLE {0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3, \
  200. 4,4,4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6,6,6,6,7, \
  201. 7,7,7,7,7,7}
  202. #elif DECDPUN==8
  203. #define DECDPUNMAX 99999999
  204. #define D2UTABLE {0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3, \
  205. 3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6, \
  206. 6,6,6,6,6,7}
  207. #elif DECDPUN==9
  208. #define DECDPUNMAX 999999999
  209. #define D2UTABLE {0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3, \
  210. 3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5, \
  211. 5,5,6,6,6,6}
  212. #elif defined(DECDPUN)
  213. #error DECDPUN must be in the range 1-9
  214. #endif
  215. /* ----- Shared data (in decNumber.c) ----- */
  216. /* Public lookup table used by the D2U macro (see below) */
  217. #define DECMAXD2U 49
  218. extern const uByte d2utable[DECMAXD2U+1];
  219. /* ----- Macros ----- */
  220. /* ISZERO -- return true if decNumber dn is a zero */
  221. /* [performance-critical in some situations] */
  222. #define ISZERO(dn) decNumberIsZero(dn) /* now just a local name */
  223. /* D2U -- return the number of Units needed to hold d digits */
  224. /* (runtime version, with table lookaside for small d) */
  225. #if DECDPUN==8
  226. #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3))
  227. #elif DECDPUN==4
  228. #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2))
  229. #else
  230. #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN)
  231. #endif
  232. /* SD2U -- static D2U macro (for compile-time calculation) */
  233. #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN)
  234. /* MSUDIGITS -- returns digits in msu, from digits, calculated */
  235. /* using D2U */
  236. #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN)
  237. /* D2N -- return the number of decNumber structs that would be */
  238. /* needed to contain that number of digits (and the initial */
  239. /* decNumber struct) safely. Note that one Unit is included in the */
  240. /* initial structure. Used for allocating space that is aligned on */
  241. /* a decNumber struct boundary. */
  242. #define D2N(d) \
  243. ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber))
  244. /* TODIGIT -- macro to remove the leading digit from the unsigned */
  245. /* integer u at column cut (counting from the right, LSD=0) and */
  246. /* place it as an ASCII character into the character pointed to by */
  247. /* c. Note that cut must be <= 9, and the maximum value for u is */
  248. /* 2,000,000,000 (as is needed for negative exponents of */
  249. /* subnormals). The unsigned integer pow is used as a temporary */
  250. /* variable. */
  251. #define TODIGIT(u, cut, c, pow) { \
  252. *(c)='0'; \
  253. pow=DECPOWERS[cut]*2; \
  254. if ((u)>pow) { \
  255. pow*=4; \
  256. if ((u)>=pow) {(u)-=pow; *(c)+=8;} \
  257. pow/=2; \
  258. if ((u)>=pow) {(u)-=pow; *(c)+=4;} \
  259. pow/=2; \
  260. } \
  261. if ((u)>=pow) {(u)-=pow; *(c)+=2;} \
  262. pow/=2; \
  263. if ((u)>=pow) {(u)-=pow; *(c)+=1;} \
  264. }
  265. /* ---------------------------------------------------------------- */
  266. /* Definitions for fixed-precision modules (only valid after */
  267. /* decSingle.h, decDouble.h, or decQuad.h has been included) */
  268. /* ---------------------------------------------------------------- */
  269. /* bcdnum -- a structure describing a format-independent finite */
  270. /* number, whose coefficient is a string of bcd8 uBytes */
  271. typedef struct {
  272. uByte *msd; /* -> most significant digit */
  273. uByte *lsd; /* -> least ditto */
  274. uInt sign; /* 0=positive, DECFLOAT_Sign=negative */
  275. Int exponent; /* Unadjusted signed exponent (q), or */
  276. /* DECFLOAT_NaN etc. for a special */
  277. } bcdnum;
  278. /* Test if exponent or bcdnum exponent must be a special, etc. */
  279. #define EXPISSPECIAL(exp) ((exp)>=DECFLOAT_MinSp)
  280. #define EXPISINF(exp) (exp==DECFLOAT_Inf)
  281. #define EXPISNAN(exp) (exp==DECFLOAT_qNaN || exp==DECFLOAT_sNaN)
  282. #define NUMISSPECIAL(num) (EXPISSPECIAL((num)->exponent))
  283. /* Refer to a 32-bit word or byte in a decFloat (df) by big-endian */
  284. /* (array) notation (the 0 word or byte contains the sign bit), */
  285. /* automatically adjusting for endianness; similarly address a word */
  286. /* in the next-wider format (decFloatWider, or dfw) */
  287. #define DECWORDS (DECBYTES/4)
  288. #define DECWWORDS (DECWBYTES/4)
  289. #if DECLITEND
  290. #define DFBYTE(df, off) ((df)->bytes[DECBYTES-1-(off)])
  291. #define DFWORD(df, off) ((df)->words[DECWORDS-1-(off)])
  292. #define DFWWORD(dfw, off) ((dfw)->words[DECWWORDS-1-(off)])
  293. #else
  294. #define DFBYTE(df, off) ((df)->bytes[off])
  295. #define DFWORD(df, off) ((df)->words[off])
  296. #define DFWWORD(dfw, off) ((dfw)->words[off])
  297. #endif
  298. /* Tests for sign or specials, directly on DECFLOATs */
  299. #define DFISSIGNED(df) (DFWORD(df, 0)&0x80000000)
  300. #define DFISSPECIAL(df) ((DFWORD(df, 0)&0x78000000)==0x78000000)
  301. #define DFISINF(df) ((DFWORD(df, 0)&0x7c000000)==0x78000000)
  302. #define DFISNAN(df) ((DFWORD(df, 0)&0x7c000000)==0x7c000000)
  303. #define DFISQNAN(df) ((DFWORD(df, 0)&0x7e000000)==0x7c000000)
  304. #define DFISSNAN(df) ((DFWORD(df, 0)&0x7e000000)==0x7e000000)
  305. /* Shared lookup tables */
  306. #include "decCommonSymbols.h"
  307. extern const uInt DECCOMBMSD[64]; /* Combination field -> MSD */
  308. extern const uInt DECCOMBFROM[48]; /* exp+msd -> Combination */
  309. /* Private generic (utility) routine */
  310. #if DECCHECK || DECTRACE
  311. extern void decShowNum(const bcdnum *, const char *);
  312. #endif
  313. /* Format-dependent macros and constants */
  314. #if defined(DECPMAX)
  315. /* Useful constants */
  316. #define DECPMAX9 (ROUNDUP(DECPMAX, 9)/9) /* 'Pmax' in 10**9s */
  317. /* Top words for a zero */
  318. #define SINGLEZERO 0x22500000
  319. #define DOUBLEZERO 0x22380000
  320. #define QUADZERO 0x22080000
  321. /* [ZEROWORD is defined to be one of these in the DFISZERO macro] */
  322. /* Format-dependent common tests: */
  323. /* DFISZERO -- test for (any) zero */
  324. /* DFISCCZERO -- test for coefficient continuation being zero */
  325. /* DFISCC01 -- test for coefficient contains only 0s and 1s */
  326. /* DFISINT -- test for finite and exponent q=0 */
  327. /* DFISUINT01 -- test for sign=0, finite, exponent q=0, and */
  328. /* MSD=0 or 1 */
  329. /* ZEROWORD is also defined here. */
  330. /* In DFISZERO the first test checks the least-significant word */
  331. /* (most likely to be non-zero); the penultimate tests MSD and */
  332. /* DPDs in the signword, and the final test excludes specials and */
  333. /* MSD>7. DFISINT similarly has to allow for the two forms of */
  334. /* MSD codes. DFISUINT01 only has to allow for one form of MSD */
  335. /* code. */
  336. #if DECPMAX==7
  337. #define ZEROWORD SINGLEZERO
  338. /* [test macros not needed except for Zero] */
  339. #define DFISZERO(df) ((DFWORD(df, 0)&0x1c0fffff)==0 \
  340. && (DFWORD(df, 0)&0x60000000)!=0x60000000)
  341. #elif DECPMAX==16
  342. #define ZEROWORD DOUBLEZERO
  343. #define DFISZERO(df) ((DFWORD(df, 1)==0 \
  344. && (DFWORD(df, 0)&0x1c03ffff)==0 \
  345. && (DFWORD(df, 0)&0x60000000)!=0x60000000))
  346. #define DFISINT(df) ((DFWORD(df, 0)&0x63fc0000)==0x22380000 \
  347. ||(DFWORD(df, 0)&0x7bfc0000)==0x6a380000)
  348. #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbfc0000)==0x22380000)
  349. #define DFISCCZERO(df) (DFWORD(df, 1)==0 \
  350. && (DFWORD(df, 0)&0x0003ffff)==0)
  351. #define DFISCC01(df) ((DFWORD(df, 0)&~0xfffc9124)==0 \
  352. && (DFWORD(df, 1)&~0x49124491)==0)
  353. #elif DECPMAX==34
  354. #define ZEROWORD QUADZERO
  355. #define DFISZERO(df) ((DFWORD(df, 3)==0 \
  356. && DFWORD(df, 2)==0 \
  357. && DFWORD(df, 1)==0 \
  358. && (DFWORD(df, 0)&0x1c003fff)==0 \
  359. && (DFWORD(df, 0)&0x60000000)!=0x60000000))
  360. #define DFISINT(df) ((DFWORD(df, 0)&0x63ffc000)==0x22080000 \
  361. ||(DFWORD(df, 0)&0x7bffc000)==0x6a080000)
  362. #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbffc000)==0x22080000)
  363. #define DFISCCZERO(df) (DFWORD(df, 3)==0 \
  364. && DFWORD(df, 2)==0 \
  365. && DFWORD(df, 1)==0 \
  366. && (DFWORD(df, 0)&0x00003fff)==0)
  367. #define DFISCC01(df) ((DFWORD(df, 0)&~0xffffc912)==0 \
  368. && (DFWORD(df, 1)&~0x44912449)==0 \
  369. && (DFWORD(df, 2)&~0x12449124)==0 \
  370. && (DFWORD(df, 3)&~0x49124491)==0)
  371. #endif
  372. /* Macros to test if a certain 10 bits of a uInt or pair of uInts */
  373. /* are a canonical declet [higher or lower bits are ignored]. */
  374. /* declet is at offset 0 (from the right) in a uInt: */
  375. #define CANONDPD(dpd) (((dpd)&0x300)==0 || ((dpd)&0x6e)!=0x6e)
  376. /* declet is at offset k (a multiple of 2) in a uInt: */
  377. #define CANONDPDOFF(dpd, k) (((dpd)&(0x300<<(k)))==0 \
  378. || ((dpd)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
  379. /* declet is at offset k (a multiple of 2) in a pair of uInts: */
  380. /* [the top 2 bits will always be in the more-significant uInt] */
  381. #define CANONDPDTWO(hi, lo, k) (((hi)&(0x300>>(32-(k))))==0 \
  382. || ((hi)&(0x6e>>(32-(k))))!=(0x6e>>(32-(k))) \
  383. || ((lo)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
  384. /* Macro to test whether a full-length (length DECPMAX) BCD8 */
  385. /* coefficient, starting at uByte u, is all zeros */
  386. /* Test just the LSWord first, then the remainder as a sequence */
  387. /* of tests in order to avoid same-level use of UBTOUI */
  388. #if DECPMAX==7
  389. #define ISCOEFFZERO(u) ( \
  390. UBTOUI((u)+DECPMAX-4)==0 \
  391. && UBTOUS((u)+DECPMAX-6)==0 \
  392. && *(u)==0)
  393. #elif DECPMAX==16
  394. #define ISCOEFFZERO(u) ( \
  395. UBTOUI((u)+DECPMAX-4)==0 \
  396. && UBTOUI((u)+DECPMAX-8)==0 \
  397. && UBTOUI((u)+DECPMAX-12)==0 \
  398. && UBTOUI(u)==0)
  399. #elif DECPMAX==34
  400. #define ISCOEFFZERO(u) ( \
  401. UBTOUI((u)+DECPMAX-4)==0 \
  402. && UBTOUI((u)+DECPMAX-8)==0 \
  403. && UBTOUI((u)+DECPMAX-12)==0 \
  404. && UBTOUI((u)+DECPMAX-16)==0 \
  405. && UBTOUI((u)+DECPMAX-20)==0 \
  406. && UBTOUI((u)+DECPMAX-24)==0 \
  407. && UBTOUI((u)+DECPMAX-28)==0 \
  408. && UBTOUI((u)+DECPMAX-32)==0 \
  409. && UBTOUS(u)==0)
  410. #endif
  411. /* Macros and masks for the exponent continuation field and MSD */
  412. /* Get the exponent continuation from a decFloat *df as an Int */
  413. #define GETECON(df) ((Int)((DFWORD((df), 0)&0x03ffffff)>>(32-6-DECECONL)))
  414. /* Ditto, from the next-wider format */
  415. #define GETWECON(df) ((Int)((DFWWORD((df), 0)&0x03ffffff)>>(32-6-DECWECONL)))
  416. /* Get the biased exponent similarly */
  417. #define GETEXP(df) ((Int)(DECCOMBEXP[DFWORD((df), 0)>>26]+GETECON(df)))
  418. /* Get the unbiased exponent similarly */
  419. #define GETEXPUN(df) ((Int)GETEXP(df)-DECBIAS)
  420. /* Get the MSD similarly (as uInt) */
  421. #define GETMSD(df) (DECCOMBMSD[DFWORD((df), 0)>>26])
  422. /* Compile-time computes of the exponent continuation field masks */
  423. /* full exponent continuation field: */
  424. #define ECONMASK ((0x03ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
  425. /* same, not including its first digit (the qNaN/sNaN selector): */
  426. #define ECONNANMASK ((0x01ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
  427. /* Macros to decode the coefficient in a finite decFloat *df into */
  428. /* a BCD string (uByte *bcdin) of length DECPMAX uBytes. */
  429. /* In-line sequence to convert least significant 10 bits of uInt */
  430. /* dpd to three BCD8 digits starting at uByte u. Note that an */
  431. /* extra byte is written to the right of the three digits because */
  432. /* four bytes are moved at a time for speed; the alternative */
  433. /* macro moves exactly three bytes (usually slower). */
  434. #define dpd2bcd8(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 4)
  435. #define dpd2bcd83(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 3)
  436. /* Decode the declets. After extracting each one, it is decoded */
  437. /* to BCD8 using a table lookup (also used for variable-length */
  438. /* decode). Each DPD decode is 3 bytes BCD8 plus a one-byte */
  439. /* length which is not used, here). Fixed-length 4-byte moves */
  440. /* are fast, however, almost everywhere, and so are used except */
  441. /* for the final three bytes (to avoid overrun). The code below */
  442. /* is 36 instructions for Doubles and about 70 for Quads, even */
  443. /* on IA32. */
  444. /* Two macros are defined for each format: */
  445. /* GETCOEFF extracts the coefficient of the current format */
  446. /* GETWCOEFF extracts the coefficient of the next-wider format. */
  447. /* The latter is a copy of the next-wider GETCOEFF using DFWWORD. */
  448. #if DECPMAX==7
  449. #define GETCOEFF(df, bcd) { \
  450. uInt sourhi=DFWORD(df, 0); \
  451. *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
  452. dpd2bcd8(bcd+1, sourhi>>10); \
  453. dpd2bcd83(bcd+4, sourhi);}
  454. #define GETWCOEFF(df, bcd) { \
  455. uInt sourhi=DFWWORD(df, 0); \
  456. uInt sourlo=DFWWORD(df, 1); \
  457. *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
  458. dpd2bcd8(bcd+1, sourhi>>8); \
  459. dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30)); \
  460. dpd2bcd8(bcd+7, sourlo>>20); \
  461. dpd2bcd8(bcd+10, sourlo>>10); \
  462. dpd2bcd83(bcd+13, sourlo);}
  463. #elif DECPMAX==16
  464. #define GETCOEFF(df, bcd) { \
  465. uInt sourhi=DFWORD(df, 0); \
  466. uInt sourlo=DFWORD(df, 1); \
  467. *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
  468. dpd2bcd8(bcd+1, sourhi>>8); \
  469. dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30)); \
  470. dpd2bcd8(bcd+7, sourlo>>20); \
  471. dpd2bcd8(bcd+10, sourlo>>10); \
  472. dpd2bcd83(bcd+13, sourlo);}
  473. #define GETWCOEFF(df, bcd) { \
  474. uInt sourhi=DFWWORD(df, 0); \
  475. uInt sourmh=DFWWORD(df, 1); \
  476. uInt sourml=DFWWORD(df, 2); \
  477. uInt sourlo=DFWWORD(df, 3); \
  478. *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
  479. dpd2bcd8(bcd+1, sourhi>>4); \
  480. dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26)); \
  481. dpd2bcd8(bcd+7, sourmh>>16); \
  482. dpd2bcd8(bcd+10, sourmh>>6); \
  483. dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28)); \
  484. dpd2bcd8(bcd+16, sourml>>18); \
  485. dpd2bcd8(bcd+19, sourml>>8); \
  486. dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30)); \
  487. dpd2bcd8(bcd+25, sourlo>>20); \
  488. dpd2bcd8(bcd+28, sourlo>>10); \
  489. dpd2bcd83(bcd+31, sourlo);}
  490. #elif DECPMAX==34
  491. #define GETCOEFF(df, bcd) { \
  492. uInt sourhi=DFWORD(df, 0); \
  493. uInt sourmh=DFWORD(df, 1); \
  494. uInt sourml=DFWORD(df, 2); \
  495. uInt sourlo=DFWORD(df, 3); \
  496. *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
  497. dpd2bcd8(bcd+1, sourhi>>4); \
  498. dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26)); \
  499. dpd2bcd8(bcd+7, sourmh>>16); \
  500. dpd2bcd8(bcd+10, sourmh>>6); \
  501. dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28)); \
  502. dpd2bcd8(bcd+16, sourml>>18); \
  503. dpd2bcd8(bcd+19, sourml>>8); \
  504. dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30)); \
  505. dpd2bcd8(bcd+25, sourlo>>20); \
  506. dpd2bcd8(bcd+28, sourlo>>10); \
  507. dpd2bcd83(bcd+31, sourlo);}
  508. #define GETWCOEFF(df, bcd) {??} /* [should never be used] */
  509. #endif
  510. /* Macros to decode the coefficient in a finite decFloat *df into */
  511. /* a base-billion uInt array, with the least-significant */
  512. /* 0-999999999 'digit' at offset 0. */
  513. /* Decode the declets. After extracting each one, it is decoded */
  514. /* to binary using a table lookup. Three tables are used; one */
  515. /* the usual DPD to binary, the other two pre-multiplied by 1000 */
  516. /* and 1000000 to avoid multiplication during decode. These */
  517. /* tables can also be used for multiplying up the MSD as the DPD */
  518. /* code for 0 through 9 is the identity. */
  519. #define DPD2BIN0 DPD2BIN /* for prettier code */
  520. #if DECPMAX==7
  521. #define GETCOEFFBILL(df, buf) { \
  522. uInt sourhi=DFWORD(df, 0); \
  523. (buf)[0]=DPD2BIN0[sourhi&0x3ff] \
  524. +DPD2BINK[(sourhi>>10)&0x3ff] \
  525. +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
  526. #elif DECPMAX==16
  527. #define GETCOEFFBILL(df, buf) { \
  528. uInt sourhi, sourlo; \
  529. sourlo=DFWORD(df, 1); \
  530. (buf)[0]=DPD2BIN0[sourlo&0x3ff] \
  531. +DPD2BINK[(sourlo>>10)&0x3ff] \
  532. +DPD2BINM[(sourlo>>20)&0x3ff]; \
  533. sourhi=DFWORD(df, 0); \
  534. (buf)[1]=DPD2BIN0[((sourhi<<2) | (sourlo>>30))&0x3ff] \
  535. +DPD2BINK[(sourhi>>8)&0x3ff] \
  536. +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
  537. #elif DECPMAX==34
  538. #define GETCOEFFBILL(df, buf) { \
  539. uInt sourhi, sourmh, sourml, sourlo; \
  540. sourlo=DFWORD(df, 3); \
  541. (buf)[0]=DPD2BIN0[sourlo&0x3ff] \
  542. +DPD2BINK[(sourlo>>10)&0x3ff] \
  543. +DPD2BINM[(sourlo>>20)&0x3ff]; \
  544. sourml=DFWORD(df, 2); \
  545. (buf)[1]=DPD2BIN0[((sourml<<2) | (sourlo>>30))&0x3ff] \
  546. +DPD2BINK[(sourml>>8)&0x3ff] \
  547. +DPD2BINM[(sourml>>18)&0x3ff]; \
  548. sourmh=DFWORD(df, 1); \
  549. (buf)[2]=DPD2BIN0[((sourmh<<4) | (sourml>>28))&0x3ff] \
  550. +DPD2BINK[(sourmh>>6)&0x3ff] \
  551. +DPD2BINM[(sourmh>>16)&0x3ff]; \
  552. sourhi=DFWORD(df, 0); \
  553. (buf)[3]=DPD2BIN0[((sourhi<<6) | (sourmh>>26))&0x3ff] \
  554. +DPD2BINK[(sourhi>>4)&0x3ff] \
  555. +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
  556. #endif
  557. /* Macros to decode the coefficient in a finite decFloat *df into */
  558. /* a base-thousand uInt array (of size DECLETS+1, to allow for */
  559. /* the MSD), with the least-significant 0-999 'digit' at offset 0.*/
  560. /* Decode the declets. After extracting each one, it is decoded */
  561. /* to binary using a table lookup. */
  562. #if DECPMAX==7
  563. #define GETCOEFFTHOU(df, buf) { \
  564. uInt sourhi=DFWORD(df, 0); \
  565. (buf)[0]=DPD2BIN[sourhi&0x3ff]; \
  566. (buf)[1]=DPD2BIN[(sourhi>>10)&0x3ff]; \
  567. (buf)[2]=DECCOMBMSD[sourhi>>26];}
  568. #elif DECPMAX==16
  569. #define GETCOEFFTHOU(df, buf) { \
  570. uInt sourhi, sourlo; \
  571. sourlo=DFWORD(df, 1); \
  572. (buf)[0]=DPD2BIN[sourlo&0x3ff]; \
  573. (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff]; \
  574. (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff]; \
  575. sourhi=DFWORD(df, 0); \
  576. (buf)[3]=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff]; \
  577. (buf)[4]=DPD2BIN[(sourhi>>8)&0x3ff]; \
  578. (buf)[5]=DECCOMBMSD[sourhi>>26];}
  579. #elif DECPMAX==34
  580. #define GETCOEFFTHOU(df, buf) { \
  581. uInt sourhi, sourmh, sourml, sourlo; \
  582. sourlo=DFWORD(df, 3); \
  583. (buf)[0]=DPD2BIN[sourlo&0x3ff]; \
  584. (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff]; \
  585. (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff]; \
  586. sourml=DFWORD(df, 2); \
  587. (buf)[3]=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff]; \
  588. (buf)[4]=DPD2BIN[(sourml>>8)&0x3ff]; \
  589. (buf)[5]=DPD2BIN[(sourml>>18)&0x3ff]; \
  590. sourmh=DFWORD(df, 1); \
  591. (buf)[6]=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff]; \
  592. (buf)[7]=DPD2BIN[(sourmh>>6)&0x3ff]; \
  593. (buf)[8]=DPD2BIN[(sourmh>>16)&0x3ff]; \
  594. sourhi=DFWORD(df, 0); \
  595. (buf)[9]=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff]; \
  596. (buf)[10]=DPD2BIN[(sourhi>>4)&0x3ff]; \
  597. (buf)[11]=DECCOMBMSD[sourhi>>26];}
  598. #endif
  599. /* Macros to decode the coefficient in a finite decFloat *df and */
  600. /* add to a base-thousand uInt array (as for GETCOEFFTHOU). */
  601. /* After the addition then most significant 'digit' in the array */
  602. /* might have a value larger then 10 (with a maximum of 19). */
  603. #if DECPMAX==7
  604. #define ADDCOEFFTHOU(df, buf) { \
  605. uInt sourhi=DFWORD(df, 0); \
  606. (buf)[0]+=DPD2BIN[sourhi&0x3ff]; \
  607. if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \
  608. (buf)[1]+=DPD2BIN[(sourhi>>10)&0x3ff]; \
  609. if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \
  610. (buf)[2]+=DECCOMBMSD[sourhi>>26];}
  611. #elif DECPMAX==16
  612. #define ADDCOEFFTHOU(df, buf) { \
  613. uInt sourhi, sourlo; \
  614. sourlo=DFWORD(df, 1); \
  615. (buf)[0]+=DPD2BIN[sourlo&0x3ff]; \
  616. if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \
  617. (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff]; \
  618. if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \
  619. (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff]; \
  620. if (buf[2]>999) {buf[2]-=1000; buf[3]++;} \
  621. sourhi=DFWORD(df, 0); \
  622. (buf)[3]+=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff]; \
  623. if (buf[3]>999) {buf[3]-=1000; buf[4]++;} \
  624. (buf)[4]+=DPD2BIN[(sourhi>>8)&0x3ff]; \
  625. if (buf[4]>999) {buf[4]-=1000; buf[5]++;} \
  626. (buf)[5]+=DECCOMBMSD[sourhi>>26];}
  627. #elif DECPMAX==34
  628. #define ADDCOEFFTHOU(df, buf) { \
  629. uInt sourhi, sourmh, sourml, sourlo; \
  630. sourlo=DFWORD(df, 3); \
  631. (buf)[0]+=DPD2BIN[sourlo&0x3ff]; \
  632. if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \
  633. (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff]; \
  634. if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \
  635. (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff]; \
  636. if (buf[2]>999) {buf[2]-=1000; buf[3]++;} \
  637. sourml=DFWORD(df, 2); \
  638. (buf)[3]+=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff]; \
  639. if (buf[3]>999) {buf[3]-=1000; buf[4]++;} \
  640. (buf)[4]+=DPD2BIN[(sourml>>8)&0x3ff]; \
  641. if (buf[4]>999) {buf[4]-=1000; buf[5]++;} \
  642. (buf)[5]+=DPD2BIN[(sourml>>18)&0x3ff]; \
  643. if (buf[5]>999) {buf[5]-=1000; buf[6]++;} \
  644. sourmh=DFWORD(df, 1); \
  645. (buf)[6]+=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff]; \
  646. if (buf[6]>999) {buf[6]-=1000; buf[7]++;} \
  647. (buf)[7]+=DPD2BIN[(sourmh>>6)&0x3ff]; \
  648. if (buf[7]>999) {buf[7]-=1000; buf[8]++;} \
  649. (buf)[8]+=DPD2BIN[(sourmh>>16)&0x3ff]; \
  650. if (buf[8]>999) {buf[8]-=1000; buf[9]++;} \
  651. sourhi=DFWORD(df, 0); \
  652. (buf)[9]+=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff]; \
  653. if (buf[9]>999) {buf[9]-=1000; buf[10]++;} \
  654. (buf)[10]+=DPD2BIN[(sourhi>>4)&0x3ff]; \
  655. if (buf[10]>999) {buf[10]-=1000; buf[11]++;} \
  656. (buf)[11]+=DECCOMBMSD[sourhi>>26];}
  657. #endif
  658. /* Set a decFloat to the maximum positive finite number (Nmax) */
  659. #if DECPMAX==7
  660. #define DFSETNMAX(df) \
  661. {DFWORD(df, 0)=0x77f3fcff;}
  662. #elif DECPMAX==16
  663. #define DFSETNMAX(df) \
  664. {DFWORD(df, 0)=0x77fcff3f; \
  665. DFWORD(df, 1)=0xcff3fcff;}
  666. #elif DECPMAX==34
  667. #define DFSETNMAX(df) \
  668. {DFWORD(df, 0)=0x77ffcff3; \
  669. DFWORD(df, 1)=0xfcff3fcf; \
  670. DFWORD(df, 2)=0xf3fcff3f; \
  671. DFWORD(df, 3)=0xcff3fcff;}
  672. #endif
  673. /* [end of format-dependent macros and constants] */
  674. #endif
  675. #else
  676. #error decNumberLocal included more than once
  677. #endif