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