context.c 5.9 KB

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