context.c 5.9 KB

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