context.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <signal.h>
  31. void
  32. mpd_dflt_traphandler(mpd_context_t *ctx UNUSED)
  33. {
  34. raise(SIGFPE);
  35. }
  36. void (* mpd_traphandler)(mpd_context_t *) = mpd_dflt_traphandler;
  37. void
  38. mpd_setminalloc(mpd_ssize_t n)
  39. {
  40. static int minalloc_is_set = 0;
  41. if (minalloc_is_set) {
  42. mpd_err_warn("mpd_setminalloc: ignoring request to set "
  43. "MPD_MINALLOC a second time\n");
  44. return;
  45. }
  46. if (n < MPD_MINALLOC_MIN || n > MPD_MINALLOC_MAX) {
  47. mpd_err_fatal("illegal value for MPD_MINALLOC"); /* GCOV_NOT_REACHED */
  48. }
  49. MPD_MINALLOC = n;
  50. minalloc_is_set = 1;
  51. }
  52. void
  53. mpd_init(mpd_context_t *ctx, mpd_ssize_t prec)
  54. {
  55. mpd_ssize_t ideal_minalloc;
  56. mpd_defaultcontext(ctx);
  57. if (!mpd_qsetprec(ctx, prec)) {
  58. mpd_addstatus_raise(ctx, MPD_Invalid_context);
  59. return;
  60. }
  61. ideal_minalloc = 2 * ((prec+MPD_RDIGITS-1) / MPD_RDIGITS);
  62. if (ideal_minalloc < MPD_MINALLOC_MIN) ideal_minalloc = MPD_MINALLOC_MIN;
  63. if (ideal_minalloc > MPD_MINALLOC_MAX) ideal_minalloc = MPD_MINALLOC_MAX;
  64. mpd_setminalloc(ideal_minalloc);
  65. }
  66. void
  67. mpd_maxcontext(mpd_context_t *ctx)
  68. {
  69. ctx->prec=MPD_MAX_PREC;
  70. ctx->emax=MPD_MAX_EMAX;
  71. ctx->emin=MPD_MIN_EMIN;
  72. ctx->round=MPD_ROUND_HALF_EVEN;
  73. ctx->traps=MPD_Traps;
  74. ctx->status=0;
  75. ctx->newtrap=0;
  76. ctx->clamp=0;
  77. ctx->allcr=1;
  78. }
  79. void
  80. mpd_maxcontext_plus(mpd_context_t *workctx, const mpd_context_t *ctx)
  81. {
  82. workctx->prec = ctx->prec > MPD_MAX_PREC ? ctx->prec : MPD_MAX_PREC;
  83. workctx->emax = ctx->emax > MPD_MAX_EMAX ? ctx->emax : MPD_MAX_EMAX;
  84. workctx->emin = ctx->emin < MPD_MIN_EMIN ? ctx->emin : MPD_MIN_EMIN;
  85. workctx->round=MPD_ROUND_HALF_EVEN;
  86. workctx->traps=MPD_Traps;
  87. workctx->status=0;
  88. workctx->newtrap=0;
  89. workctx->clamp=0;
  90. workctx->allcr=1;
  91. }
  92. void
  93. mpd_defaultcontext(mpd_context_t *ctx)
  94. {
  95. ctx->prec=2*MPD_RDIGITS;
  96. ctx->emax=MPD_MAX_EMAX;
  97. ctx->emin=MPD_MIN_EMIN;
  98. ctx->round=MPD_ROUND_HALF_UP;
  99. ctx->traps=MPD_Traps;
  100. ctx->status=0;
  101. ctx->newtrap=0;
  102. ctx->clamp=0;
  103. ctx->allcr=1;
  104. }
  105. void
  106. mpd_basiccontext(mpd_context_t *ctx)
  107. {
  108. ctx->prec=9;
  109. ctx->emax=MPD_MAX_EMAX;
  110. ctx->emin=MPD_MIN_EMIN;
  111. ctx->round=MPD_ROUND_HALF_UP;
  112. ctx->traps=MPD_Traps|MPD_Clamped;
  113. ctx->status=0;
  114. ctx->newtrap=0;
  115. ctx->clamp=0;
  116. ctx->allcr=1;
  117. }
  118. int
  119. mpd_ieee_context(mpd_context_t *ctx, int bits)
  120. {
  121. if (bits <= 0 || bits > MPD_IEEE_CONTEXT_MAX_BITS || bits % 32) {
  122. return -1;
  123. }
  124. ctx->prec = 9 * (bits/32) - 2;
  125. ctx->emax = 3 * ((mpd_ssize_t)1<<(bits/16+3));
  126. ctx->emin = 1 - ctx->emax;
  127. ctx->round=MPD_ROUND_HALF_EVEN;
  128. ctx->traps=0;
  129. ctx->status=0;
  130. ctx->newtrap=0;
  131. ctx->clamp=1;
  132. ctx->allcr=1;
  133. return 0;
  134. }
  135. mpd_ssize_t
  136. mpd_getprec(const mpd_context_t *ctx)
  137. {
  138. return ctx->prec;
  139. }
  140. mpd_ssize_t
  141. mpd_getemax(const mpd_context_t *ctx)
  142. {
  143. return ctx->emax;
  144. }
  145. mpd_ssize_t
  146. mpd_getemin(const mpd_context_t *ctx)
  147. {
  148. return ctx->emin;
  149. }
  150. int
  151. mpd_getround(const mpd_context_t *ctx)
  152. {
  153. return ctx->round;
  154. }
  155. uint32_t
  156. mpd_gettraps(const mpd_context_t *ctx)
  157. {
  158. return ctx->traps;
  159. }
  160. uint32_t
  161. mpd_getstatus(const mpd_context_t *ctx)
  162. {
  163. return ctx->status;
  164. }
  165. int
  166. mpd_getclamp(const mpd_context_t *ctx)
  167. {
  168. return ctx->clamp;
  169. }
  170. int
  171. mpd_getcr(const mpd_context_t *ctx)
  172. {
  173. return ctx->allcr;
  174. }
  175. int
  176. mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec)
  177. {
  178. if (prec <= 0 || prec > MPD_MAX_PREC) {
  179. return 0;
  180. }
  181. ctx->prec = prec;
  182. return 1;
  183. }
  184. int
  185. mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax)
  186. {
  187. if (emax < 0 || emax > MPD_MAX_EMAX) {
  188. return 0;
  189. }
  190. ctx->emax = emax;
  191. return 1;
  192. }
  193. int
  194. mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin)
  195. {
  196. if (emin > 0 || emin < MPD_MIN_EMIN) {
  197. return 0;
  198. }
  199. ctx->emin = emin;
  200. return 1;
  201. }
  202. int
  203. mpd_qsetround(mpd_context_t *ctx, int round)
  204. {
  205. int i;
  206. for (i = 0; i < MPD_ROUND_GUARD; i++) {
  207. if (i == round) {
  208. ctx->round = round;
  209. return 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. int
  215. mpd_qsettraps(mpd_context_t *ctx, uint32_t traps)
  216. {
  217. if (traps > MPD_Max_status) {
  218. return 0;
  219. }
  220. ctx->traps = traps;
  221. return 1;
  222. }
  223. int
  224. mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags)
  225. {
  226. if (flags > MPD_Max_status) {
  227. return 0;
  228. }
  229. ctx->status = flags;
  230. return 1;
  231. }
  232. int
  233. mpd_qsetclamp(mpd_context_t *ctx, int c)
  234. {
  235. if (c != 0 && c != 1) {
  236. return 0;
  237. }
  238. ctx->clamp = c;
  239. return 1;
  240. }
  241. int
  242. mpd_qsetcr(mpd_context_t *ctx, int c)
  243. {
  244. if (c != 0 && c != 1) {
  245. return 0;
  246. }
  247. ctx->allcr = c;
  248. return 1;
  249. }
  250. void
  251. mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags)
  252. {
  253. ctx->status |= flags;
  254. if (flags&ctx->traps) {
  255. ctx->newtrap = (flags&ctx->traps);
  256. mpd_traphandler(ctx);
  257. }
  258. }