decNumberLocal.h 38 KB

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