decContext.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* ------------------------------------------------------------------ */
  2. /* Decimal Context module */
  3. /* ------------------------------------------------------------------ */
  4. /* Copyright (c) IBM Corporation, 2000, 2009. 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 module comprises the routines for handling arithmetic */
  20. /* context structures. */
  21. /* ------------------------------------------------------------------ */
  22. #include <string.h> // for strcmp
  23. #include <stdio.h> // for printf if DECCHECK
  24. #include "decContext.h" // context and base types
  25. #include "decNumberLocal.h" // decNumber local types, etc.
  26. /* compile-time endian tester [assumes sizeof(Int)>1] */
  27. static const Int mfcone=1; // constant 1
  28. static const Flag *mfctop=(const Flag *)&mfcone; // -> top byte
  29. #define LITEND *mfctop // named flag; 1=little-endian
  30. /* ------------------------------------------------------------------ */
  31. /* round-for-reround digits */
  32. /* ------------------------------------------------------------------ */
  33. const uByte DECSTICKYTAB[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
  34. /* ------------------------------------------------------------------ */
  35. /* Powers of ten (powers[n]==10**n, 0<=n<=9) */
  36. /* ------------------------------------------------------------------ */
  37. const uInt DECPOWERS[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
  38. 10000000, 100000000, 1000000000};
  39. /* ------------------------------------------------------------------ */
  40. /* decContextClearStatus -- clear bits in current status */
  41. /* */
  42. /* context is the context structure to be queried */
  43. /* mask indicates the bits to be cleared (the status bit that */
  44. /* corresponds to each 1 bit in the mask is cleared) */
  45. /* returns context */
  46. /* */
  47. /* No error is possible. */
  48. /* ------------------------------------------------------------------ */
  49. decContext *decContextClearStatus(decContext *context, uInt mask) {
  50. context->status&=~mask;
  51. return context;
  52. } // decContextClearStatus
  53. /* ------------------------------------------------------------------ */
  54. /* decContextDefault -- initialize a context structure */
  55. /* */
  56. /* context is the structure to be initialized */
  57. /* kind selects the required set of default values, one of: */
  58. /* DEC_INIT_BASE -- select ANSI X3-274 defaults */
  59. /* DEC_INIT_DECIMAL32 -- select IEEE 754 defaults, 32-bit */
  60. /* DEC_INIT_DECIMAL64 -- select IEEE 754 defaults, 64-bit */
  61. /* DEC_INIT_DECIMAL128 -- select IEEE 754 defaults, 128-bit */
  62. /* For any other value a valid context is returned, but with */
  63. /* Invalid_operation set in the status field. */
  64. /* returns a context structure with the appropriate initial values. */
  65. /* ------------------------------------------------------------------ */
  66. decContext * decContextDefault(decContext *context, Int kind) {
  67. // set defaults...
  68. context->digits=9; // 9 digits
  69. context->emax=DEC_MAX_EMAX; // 9-digit exponents
  70. context->emin=DEC_MIN_EMIN; // .. balanced
  71. context->round=DEC_ROUND_HALF_UP; // 0.5 rises
  72. context->traps=DEC_Errors; // all but informational
  73. context->status=0; // cleared
  74. context->clamp=0; // no clamping
  75. #if DECSUBSET
  76. context->extended=0; // cleared
  77. #endif
  78. switch (kind) {
  79. case DEC_INIT_BASE:
  80. // [use defaults]
  81. break;
  82. case DEC_INIT_DECIMAL32:
  83. context->digits=7; // digits
  84. context->emax=96; // Emax
  85. context->emin=-95; // Emin
  86. context->round=DEC_ROUND_HALF_EVEN; // 0.5 to nearest even
  87. context->traps=0; // no traps set
  88. context->clamp=1; // clamp exponents
  89. #if DECSUBSET
  90. context->extended=1; // set
  91. #endif
  92. break;
  93. case DEC_INIT_DECIMAL64:
  94. context->digits=16; // digits
  95. context->emax=384; // Emax
  96. context->emin=-383; // Emin
  97. context->round=DEC_ROUND_HALF_EVEN; // 0.5 to nearest even
  98. context->traps=0; // no traps set
  99. context->clamp=1; // clamp exponents
  100. #if DECSUBSET
  101. context->extended=1; // set
  102. #endif
  103. break;
  104. case DEC_INIT_DECIMAL128:
  105. context->digits=34; // digits
  106. context->emax=6144; // Emax
  107. context->emin=-6143; // Emin
  108. context->round=DEC_ROUND_HALF_EVEN; // 0.5 to nearest even
  109. context->traps=0; // no traps set
  110. context->clamp=1; // clamp exponents
  111. #if DECSUBSET
  112. context->extended=1; // set
  113. #endif
  114. break;
  115. default: // invalid Kind
  116. // use defaults, and ..
  117. decContextSetStatus(context, DEC_Invalid_operation); // trap
  118. }
  119. return context;} // decContextDefault
  120. /* ------------------------------------------------------------------ */
  121. /* decContextGetRounding -- return current rounding mode */
  122. /* */
  123. /* context is the context structure to be queried */
  124. /* returns the rounding mode */
  125. /* */
  126. /* No error is possible. */
  127. /* ------------------------------------------------------------------ */
  128. enum rounding decContextGetRounding(decContext *context) {
  129. return context->round;
  130. } // decContextGetRounding
  131. /* ------------------------------------------------------------------ */
  132. /* decContextGetStatus -- return current status */
  133. /* */
  134. /* context is the context structure to be queried */
  135. /* returns status */
  136. /* */
  137. /* No error is possible. */
  138. /* ------------------------------------------------------------------ */
  139. uInt decContextGetStatus(decContext *context) {
  140. return context->status;
  141. } // decContextGetStatus
  142. /* ------------------------------------------------------------------ */
  143. /* decContextRestoreStatus -- restore bits in current status */
  144. /* */
  145. /* context is the context structure to be updated */
  146. /* newstatus is the source for the bits to be restored */
  147. /* mask indicates the bits to be restored (the status bit that */
  148. /* corresponds to each 1 bit in the mask is set to the value of */
  149. /* the correspnding bit in newstatus) */
  150. /* returns context */
  151. /* */
  152. /* No error is possible. */
  153. /* ------------------------------------------------------------------ */
  154. decContext *decContextRestoreStatus(decContext *context,
  155. uInt newstatus, uInt mask) {
  156. context->status&=~mask; // clear the selected bits
  157. context->status|=(mask&newstatus); // or in the new bits
  158. return context;
  159. } // decContextRestoreStatus
  160. /* ------------------------------------------------------------------ */
  161. /* decContextSaveStatus -- save bits in current status */
  162. /* */
  163. /* context is the context structure to be queried */
  164. /* mask indicates the bits to be saved (the status bits that */
  165. /* correspond to each 1 bit in the mask are saved) */
  166. /* returns the AND of the mask and the current status */
  167. /* */
  168. /* No error is possible. */
  169. /* ------------------------------------------------------------------ */
  170. uInt decContextSaveStatus(decContext *context, uInt mask) {
  171. return context->status&mask;
  172. } // decContextSaveStatus
  173. /* ------------------------------------------------------------------ */
  174. /* decContextSetRounding -- set current rounding mode */
  175. /* */
  176. /* context is the context structure to be updated */
  177. /* newround is the value which will replace the current mode */
  178. /* returns context */
  179. /* */
  180. /* No error is possible. */
  181. /* ------------------------------------------------------------------ */
  182. decContext *decContextSetRounding(decContext *context,
  183. enum rounding newround) {
  184. context->round=newround;
  185. return context;
  186. } // decContextSetRounding
  187. /* ------------------------------------------------------------------ */
  188. /* decContextSetStatus -- set status and raise trap if appropriate */
  189. /* */
  190. /* context is the context structure to be updated */
  191. /* status is the DEC_ exception code */
  192. /* returns the context structure */
  193. /* */
  194. /* Control may never return from this routine, if there is a signal */
  195. /* handler and it takes a long jump. */
  196. /* ------------------------------------------------------------------ */
  197. decContext * decContextSetStatus(decContext *context, uInt status) {
  198. context->status|=status;
  199. if (status & context->traps) raise(SIGFPE);
  200. return context;} // decContextSetStatus
  201. /* ------------------------------------------------------------------ */
  202. /* decContextSetStatusFromString -- set status from a string + trap */
  203. /* */
  204. /* context is the context structure to be updated */
  205. /* string is a string exactly equal to one that might be returned */
  206. /* by decContextStatusToString */
  207. /* */
  208. /* The status bit corresponding to the string is set, and a trap */
  209. /* is raised if appropriate. */
  210. /* */
  211. /* returns the context structure, unless the string is equal to */
  212. /* DEC_Condition_MU or is not recognized. In these cases NULL is */
  213. /* returned. */
  214. /* ------------------------------------------------------------------ */
  215. decContext * decContextSetStatusFromString(decContext *context,
  216. const char *string) {
  217. if (strcmp(string, DEC_Condition_CS)==0)
  218. return decContextSetStatus(context, DEC_Conversion_syntax);
  219. if (strcmp(string, DEC_Condition_DZ)==0)
  220. return decContextSetStatus(context, DEC_Division_by_zero);
  221. if (strcmp(string, DEC_Condition_DI)==0)
  222. return decContextSetStatus(context, DEC_Division_impossible);
  223. if (strcmp(string, DEC_Condition_DU)==0)
  224. return decContextSetStatus(context, DEC_Division_undefined);
  225. if (strcmp(string, DEC_Condition_IE)==0)
  226. return decContextSetStatus(context, DEC_Inexact);
  227. if (strcmp(string, DEC_Condition_IS)==0)
  228. return decContextSetStatus(context, DEC_Insufficient_storage);
  229. if (strcmp(string, DEC_Condition_IC)==0)
  230. return decContextSetStatus(context, DEC_Invalid_context);
  231. if (strcmp(string, DEC_Condition_IO)==0)
  232. return decContextSetStatus(context, DEC_Invalid_operation);
  233. #if DECSUBSET
  234. if (strcmp(string, DEC_Condition_LD)==0)
  235. return decContextSetStatus(context, DEC_Lost_digits);
  236. #endif
  237. if (strcmp(string, DEC_Condition_OV)==0)
  238. return decContextSetStatus(context, DEC_Overflow);
  239. if (strcmp(string, DEC_Condition_PA)==0)
  240. return decContextSetStatus(context, DEC_Clamped);
  241. if (strcmp(string, DEC_Condition_RO)==0)
  242. return decContextSetStatus(context, DEC_Rounded);
  243. if (strcmp(string, DEC_Condition_SU)==0)
  244. return decContextSetStatus(context, DEC_Subnormal);
  245. if (strcmp(string, DEC_Condition_UN)==0)
  246. return decContextSetStatus(context, DEC_Underflow);
  247. if (strcmp(string, DEC_Condition_ZE)==0)
  248. return context;
  249. return NULL; // Multiple status, or unknown
  250. } // decContextSetStatusFromString
  251. /* ------------------------------------------------------------------ */
  252. /* decContextSetStatusFromStringQuiet -- set status from a string */
  253. /* */
  254. /* context is the context structure to be updated */
  255. /* string is a string exactly equal to one that might be returned */
  256. /* by decContextStatusToString */
  257. /* */
  258. /* The status bit corresponding to the string is set; no trap is */
  259. /* raised. */
  260. /* */
  261. /* returns the context structure, unless the string is equal to */
  262. /* DEC_Condition_MU or is not recognized. In these cases NULL is */
  263. /* returned. */
  264. /* ------------------------------------------------------------------ */
  265. decContext * decContextSetStatusFromStringQuiet(decContext *context,
  266. const char *string) {
  267. if (strcmp(string, DEC_Condition_CS)==0)
  268. return decContextSetStatusQuiet(context, DEC_Conversion_syntax);
  269. if (strcmp(string, DEC_Condition_DZ)==0)
  270. return decContextSetStatusQuiet(context, DEC_Division_by_zero);
  271. if (strcmp(string, DEC_Condition_DI)==0)
  272. return decContextSetStatusQuiet(context, DEC_Division_impossible);
  273. if (strcmp(string, DEC_Condition_DU)==0)
  274. return decContextSetStatusQuiet(context, DEC_Division_undefined);
  275. if (strcmp(string, DEC_Condition_IE)==0)
  276. return decContextSetStatusQuiet(context, DEC_Inexact);
  277. if (strcmp(string, DEC_Condition_IS)==0)
  278. return decContextSetStatusQuiet(context, DEC_Insufficient_storage);
  279. if (strcmp(string, DEC_Condition_IC)==0)
  280. return decContextSetStatusQuiet(context, DEC_Invalid_context);
  281. if (strcmp(string, DEC_Condition_IO)==0)
  282. return decContextSetStatusQuiet(context, DEC_Invalid_operation);
  283. #if DECSUBSET
  284. if (strcmp(string, DEC_Condition_LD)==0)
  285. return decContextSetStatusQuiet(context, DEC_Lost_digits);
  286. #endif
  287. if (strcmp(string, DEC_Condition_OV)==0)
  288. return decContextSetStatusQuiet(context, DEC_Overflow);
  289. if (strcmp(string, DEC_Condition_PA)==0)
  290. return decContextSetStatusQuiet(context, DEC_Clamped);
  291. if (strcmp(string, DEC_Condition_RO)==0)
  292. return decContextSetStatusQuiet(context, DEC_Rounded);
  293. if (strcmp(string, DEC_Condition_SU)==0)
  294. return decContextSetStatusQuiet(context, DEC_Subnormal);
  295. if (strcmp(string, DEC_Condition_UN)==0)
  296. return decContextSetStatusQuiet(context, DEC_Underflow);
  297. if (strcmp(string, DEC_Condition_ZE)==0)
  298. return context;
  299. return NULL; // Multiple status, or unknown
  300. } // decContextSetStatusFromStringQuiet
  301. /* ------------------------------------------------------------------ */
  302. /* decContextSetStatusQuiet -- set status without trap */
  303. /* */
  304. /* context is the context structure to be updated */
  305. /* status is the DEC_ exception code */
  306. /* returns the context structure */
  307. /* */
  308. /* No error is possible. */
  309. /* ------------------------------------------------------------------ */
  310. decContext * decContextSetStatusQuiet(decContext *context, uInt status) {
  311. context->status|=status;
  312. return context;} // decContextSetStatusQuiet
  313. /* ------------------------------------------------------------------ */
  314. /* decContextStatusToString -- convert status flags to a string */
  315. /* */
  316. /* context is a context with valid status field */
  317. /* */
  318. /* returns a constant string describing the condition. If multiple */
  319. /* (or no) flags are set, a generic constant message is returned. */
  320. /* ------------------------------------------------------------------ */
  321. const char *decContextStatusToString(const decContext *context) {
  322. Int status=context->status;
  323. // test the five IEEE first, as some of the others are ambiguous when
  324. // DECEXTFLAG=0
  325. if (status==DEC_Invalid_operation ) return DEC_Condition_IO;
  326. if (status==DEC_Division_by_zero ) return DEC_Condition_DZ;
  327. if (status==DEC_Overflow ) return DEC_Condition_OV;
  328. if (status==DEC_Underflow ) return DEC_Condition_UN;
  329. if (status==DEC_Inexact ) return DEC_Condition_IE;
  330. if (status==DEC_Division_impossible ) return DEC_Condition_DI;
  331. if (status==DEC_Division_undefined ) return DEC_Condition_DU;
  332. if (status==DEC_Rounded ) return DEC_Condition_RO;
  333. if (status==DEC_Clamped ) return DEC_Condition_PA;
  334. if (status==DEC_Subnormal ) return DEC_Condition_SU;
  335. if (status==DEC_Conversion_syntax ) return DEC_Condition_CS;
  336. if (status==DEC_Insufficient_storage ) return DEC_Condition_IS;
  337. if (status==DEC_Invalid_context ) return DEC_Condition_IC;
  338. #if DECSUBSET
  339. if (status==DEC_Lost_digits ) return DEC_Condition_LD;
  340. #endif
  341. if (status==0 ) return DEC_Condition_ZE;
  342. return DEC_Condition_MU; // Multiple errors
  343. } // decContextStatusToString
  344. /* ------------------------------------------------------------------ */
  345. /* decContextTestEndian -- test whether DECLITEND is set correctly */
  346. /* */
  347. /* quiet is 1 to suppress message; 0 otherwise */
  348. /* returns 0 if DECLITEND is correct */
  349. /* 1 if DECLITEND is incorrect and should be 1 */
  350. /* -1 if DECLITEND is incorrect and should be 0 */
  351. /* */
  352. /* A message is displayed if the return value is not 0 and quiet==0. */
  353. /* */
  354. /* No error is possible. */
  355. /* ------------------------------------------------------------------ */
  356. Int decContextTestEndian(Flag quiet) {
  357. Int res=0; // optimist
  358. uInt dle=(uInt)DECLITEND; // unsign
  359. if (dle>1) dle=1; // ensure 0 or 1
  360. if (LITEND!=DECLITEND) {
  361. if (!quiet) { // always refer to this
  362. #if DECPRINT
  363. const char *adj;
  364. if (LITEND) adj="little";
  365. else adj="big";
  366. printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
  367. DECLITEND, adj);
  368. #endif
  369. }
  370. res=(Int)LITEND-dle;
  371. }
  372. return res;
  373. } // decContextTestEndian
  374. /* ------------------------------------------------------------------ */
  375. /* decContextTestSavedStatus -- test bits in saved status */
  376. /* */
  377. /* oldstatus is the status word to be tested */
  378. /* mask indicates the bits to be tested (the oldstatus bits that */
  379. /* correspond to each 1 bit in the mask are tested) */
  380. /* returns 1 if any of the tested bits are 1, or 0 otherwise */
  381. /* */
  382. /* No error is possible. */
  383. /* ------------------------------------------------------------------ */
  384. uInt decContextTestSavedStatus(uInt oldstatus, uInt mask) {
  385. return (oldstatus&mask)!=0;
  386. } // decContextTestSavedStatus
  387. /* ------------------------------------------------------------------ */
  388. /* decContextTestStatus -- test bits in current status */
  389. /* */
  390. /* context is the context structure to be updated */
  391. /* mask indicates the bits to be tested (the status bits that */
  392. /* correspond to each 1 bit in the mask are tested) */
  393. /* returns 1 if any of the tested bits are 1, or 0 otherwise */
  394. /* */
  395. /* No error is possible. */
  396. /* ------------------------------------------------------------------ */
  397. uInt decContextTestStatus(decContext *context, uInt mask) {
  398. return (context->status&mask)!=0;
  399. } // decContextTestStatus
  400. /* ------------------------------------------------------------------ */
  401. /* decContextZeroStatus -- clear all status bits */
  402. /* */
  403. /* context is the context structure to be updated */
  404. /* returns context */
  405. /* */
  406. /* No error is possible. */
  407. /* ------------------------------------------------------------------ */
  408. decContext *decContextZeroStatus(decContext *context) {
  409. context->status=0;
  410. return context;
  411. } // decContextZeroStatus