context.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "mpdecimal.h"
  28. #include <stdio.h>
  29. #include <string.h>
  30. #ifndef _WIN32_WCE
  31. #include <signal.h>
  32. #endif
  33. void
  34. mpd_dflt_traphandler(mpd_context_t *ctx UNUSED)
  35. {
  36. #ifndef _WIN32_WCE
  37. raise(SIGFPE);
  38. #endif
  39. }
  40. void (* mpd_traphandler)(mpd_context_t *) = mpd_dflt_traphandler;
  41. void
  42. mpd_setminalloc(mpd_ssize_t n)
  43. {
  44. static int minalloc_is_set = 0;
  45. if (minalloc_is_set) {
  46. mpd_err_warn("mpd_setminalloc: ignoring request to set "
  47. "MPD_MINALLOC a second time\n");
  48. return;
  49. }
  50. if (n < MPD_MINALLOC_MIN || n > MPD_MINALLOC_MAX) {
  51. mpd_err_fatal("illegal value for MPD_MINALLOC"); /* GCOV_NOT_REACHED */
  52. }
  53. MPD_MINALLOC = n;
  54. minalloc_is_set = 1;
  55. }
  56. void
  57. mpd_init(mpd_context_t *ctx, mpd_ssize_t prec)
  58. {
  59. mpd_ssize_t ideal_minalloc;
  60. mpd_defaultcontext(ctx);
  61. if (!mpd_qsetprec(ctx, prec)) {
  62. mpd_addstatus_raise(ctx, MPD_Invalid_context);
  63. return;
  64. }
  65. ideal_minalloc = 2 * ((prec+MPD_RDIGITS-1) / MPD_RDIGITS);
  66. if (ideal_minalloc < MPD_MINALLOC_MIN) ideal_minalloc = MPD_MINALLOC_MIN;
  67. if (ideal_minalloc > MPD_MINALLOC_MAX) ideal_minalloc = MPD_MINALLOC_MAX;
  68. mpd_setminalloc(ideal_minalloc);
  69. }
  70. void
  71. mpd_maxcontext(mpd_context_t *ctx)
  72. {
  73. ctx->prec=MPD_MAX_PREC;
  74. ctx->emax=MPD_MAX_EMAX;
  75. ctx->emin=MPD_MIN_EMIN;
  76. ctx->round=MPD_ROUND_HALF_EVEN;
  77. ctx->traps=MPD_Traps;
  78. ctx->status=0;
  79. ctx->newtrap=0;
  80. ctx->clamp=0;
  81. ctx->allcr=1;
  82. }
  83. void
  84. mpd_maxcontext_plus(mpd_context_t *workctx, const mpd_context_t *ctx)
  85. {
  86. workctx->prec = ctx->prec > MPD_MAX_PREC ? ctx->prec : MPD_MAX_PREC;
  87. workctx->emax = ctx->emax > MPD_MAX_EMAX ? ctx->emax : MPD_MAX_EMAX;
  88. workctx->emin = ctx->emin < MPD_MIN_EMIN ? ctx->emin : MPD_MIN_EMIN;
  89. workctx->round=MPD_ROUND_HALF_EVEN;
  90. workctx->traps=MPD_Traps;
  91. workctx->status=0;
  92. workctx->newtrap=0;
  93. workctx->clamp=0;
  94. workctx->allcr=1;
  95. }
  96. void
  97. mpd_defaultcontext(mpd_context_t *ctx)
  98. {
  99. ctx->prec=2*MPD_RDIGITS;
  100. ctx->emax=MPD_MAX_EMAX;
  101. ctx->emin=MPD_MIN_EMIN;
  102. ctx->round=MPD_ROUND_HALF_UP;
  103. ctx->traps=MPD_Traps;
  104. ctx->status=0;
  105. ctx->newtrap=0;
  106. ctx->clamp=0;
  107. ctx->allcr=1;
  108. }
  109. void
  110. mpd_basiccontext(mpd_context_t *ctx)
  111. {
  112. ctx->prec=9;
  113. ctx->emax=MPD_MAX_EMAX;
  114. ctx->emin=MPD_MIN_EMIN;
  115. ctx->round=MPD_ROUND_HALF_UP;
  116. ctx->traps=MPD_Traps|MPD_Clamped;
  117. ctx->status=0;
  118. ctx->newtrap=0;
  119. ctx->clamp=0;
  120. ctx->allcr=1;
  121. }
  122. int
  123. mpd_ieee_context(mpd_context_t *ctx, int bits)
  124. {
  125. if (bits <= 0 || bits > MPD_IEEE_CONTEXT_MAX_BITS || bits % 32) {
  126. return -1;
  127. }
  128. ctx->prec = 9 * (bits/32) - 2;
  129. ctx->emax = 3 * ((mpd_ssize_t)1<<(bits/16+3));
  130. ctx->emin = 1 - ctx->emax;
  131. ctx->round=MPD_ROUND_HALF_EVEN;
  132. ctx->traps=0;
  133. ctx->status=0;
  134. ctx->newtrap=0;
  135. ctx->clamp=1;
  136. ctx->allcr=1;
  137. return 0;
  138. }
  139. mpd_ssize_t
  140. mpd_getprec(const mpd_context_t *ctx)
  141. {
  142. return ctx->prec;
  143. }
  144. mpd_ssize_t
  145. mpd_getemax(const mpd_context_t *ctx)
  146. {
  147. return ctx->emax;
  148. }
  149. mpd_ssize_t
  150. mpd_getemin(const mpd_context_t *ctx)
  151. {
  152. return ctx->emin;
  153. }
  154. int
  155. mpd_getround(const mpd_context_t *ctx)
  156. {
  157. return ctx->round;
  158. }
  159. uint32_t
  160. mpd_gettraps(const mpd_context_t *ctx)
  161. {
  162. return ctx->traps;
  163. }
  164. uint32_t
  165. mpd_getstatus(const mpd_context_t *ctx)
  166. {
  167. return ctx->status;
  168. }
  169. int
  170. mpd_getclamp(const mpd_context_t *ctx)
  171. {
  172. return ctx->clamp;
  173. }
  174. int
  175. mpd_getcr(const mpd_context_t *ctx)
  176. {
  177. return ctx->allcr;
  178. }
  179. int
  180. mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec)
  181. {
  182. if (prec <= 0 || prec > MPD_MAX_PREC) {
  183. return 0;
  184. }
  185. ctx->prec = prec;
  186. return 1;
  187. }
  188. int
  189. mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax)
  190. {
  191. if (emax < 0 || emax > MPD_MAX_EMAX) {
  192. return 0;
  193. }
  194. ctx->emax = emax;
  195. return 1;
  196. }
  197. int
  198. mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin)
  199. {
  200. if (emin > 0 || emin < MPD_MIN_EMIN) {
  201. return 0;
  202. }
  203. ctx->emin = emin;
  204. return 1;
  205. }
  206. int
  207. mpd_qsetround(mpd_context_t *ctx, int round)
  208. {
  209. int i;
  210. for (i = 0; i < MPD_ROUND_GUARD; i++) {
  211. if (i == round) {
  212. ctx->round = round;
  213. return 1;
  214. }
  215. }
  216. return 0;
  217. }
  218. int
  219. mpd_qsettraps(mpd_context_t *ctx, uint32_t traps)
  220. {
  221. if (traps > MPD_Max_status) {
  222. return 0;
  223. }
  224. ctx->traps = traps;
  225. return 1;
  226. }
  227. int
  228. mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags)
  229. {
  230. if (flags > MPD_Max_status) {
  231. return 0;
  232. }
  233. ctx->status = flags;
  234. return 1;
  235. }
  236. int
  237. mpd_qsetclamp(mpd_context_t *ctx, int c)
  238. {
  239. if (c != 0 && c != 1) {
  240. return 0;
  241. }
  242. ctx->clamp = c;
  243. return 1;
  244. }
  245. int
  246. mpd_qsetcr(mpd_context_t *ctx, int c)
  247. {
  248. if (c != 0 && c != 1) {
  249. return 0;
  250. }
  251. ctx->allcr = c;
  252. return 1;
  253. }
  254. void
  255. mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags)
  256. {
  257. ctx->status |= flags;
  258. if (flags&ctx->traps) {
  259. ctx->newtrap = (flags&ctx->traps);
  260. mpd_traphandler(ctx);
  261. }
  262. }