decNumberLocal.h 37 KB

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