decContext.c 20 KB

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