Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(77088)

Side by Side Diff: Modules/_decimal/libmpdec/context.c

Issue 7652: Merge C version of decimal into py3k.
Patch Set: Created 7 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Modules/_decimal/libmpdec/constants.h ('k') | Modules/_decimal/libmpdec/convolute.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
28
29 #include "mpdecimal.h"
30 #include <stdio.h>
31 #include <string.h>
32 #include <signal.h>
33
34
35 void
36 mpd_dflt_traphandler(mpd_context_t *ctx UNUSED)
37 {
38 raise(SIGFPE);
39 }
40
41 void (* mpd_traphandler)(mpd_context_t *) = mpd_dflt_traphandler;
42
43
44 /* Set guaranteed minimum number of coefficient words. The function may
45 be used once at program start. Setting MPD_MINALLOC to out-of-bounds
46 values is a catastrophic error, so in that case the function exits rather
47 than relying on the user to check a return value. */
48 void
49 mpd_setminalloc(mpd_ssize_t n)
50 {
51 static int minalloc_is_set = 0;
52
53 if (minalloc_is_set) {
54 mpd_err_warn("mpd_setminalloc: ignoring request to set "
55 "MPD_MINALLOC a second time\n");
56 return;
57 }
58 if (n < MPD_MINALLOC_MIN || n > MPD_MINALLOC_MAX) {
59 mpd_err_fatal("illegal value for MPD_MINALLOC"); /* GCOV_NOT_REA CHED */
60 }
61 MPD_MINALLOC = n;
62 minalloc_is_set = 1;
63 }
64
65 void
66 mpd_init(mpd_context_t *ctx, mpd_ssize_t prec)
67 {
68 mpd_ssize_t ideal_minalloc;
69
70 mpd_defaultcontext(ctx);
71
72 if (!mpd_qsetprec(ctx, prec)) {
73 mpd_addstatus_raise(ctx, MPD_Invalid_context);
74 return;
75 }
76
77 ideal_minalloc = 2 * ((prec+MPD_RDIGITS-1) / MPD_RDIGITS);
78 if (ideal_minalloc < MPD_MINALLOC_MIN) ideal_minalloc = MPD_MINALLOC_MIN ;
79 if (ideal_minalloc > MPD_MINALLOC_MAX) ideal_minalloc = MPD_MINALLOC_MAX ;
80
81 mpd_setminalloc(ideal_minalloc);
82 }
83
84 void
85 mpd_maxcontext(mpd_context_t *ctx)
86 {
87 ctx->prec=MPD_MAX_PREC;
88 ctx->emax=MPD_MAX_EMAX;
89 ctx->emin=MPD_MIN_EMIN;
90 ctx->round=MPD_ROUND_HALF_EVEN;
91 ctx->traps=MPD_Traps;
92 ctx->status=0;
93 ctx->newtrap=0;
94 ctx->clamp=0;
95 ctx->allcr=1;
96 }
97
98 void
99 mpd_maxcontext_plus(mpd_context_t *workctx, const mpd_context_t *ctx)
100 {
101 workctx->prec = ctx->prec > MPD_MAX_PREC ? ctx->prec : MPD_MAX_PREC;
102 workctx->emax = ctx->emax > MPD_MAX_EMAX ? ctx->emax : MPD_MAX_EMAX;
103 workctx->emin = ctx->emin < MPD_MIN_EMIN ? ctx->emin : MPD_MIN_EMIN;
104 workctx->round=MPD_ROUND_HALF_EVEN;
105 workctx->traps=MPD_Traps;
106 workctx->status=0;
107 workctx->newtrap=0;
108 workctx->clamp=0;
109 workctx->allcr=1;
110 }
111
112 void
113 mpd_defaultcontext(mpd_context_t *ctx)
114 {
115 ctx->prec=2*MPD_RDIGITS;
116 ctx->emax=MPD_MAX_EMAX;
117 ctx->emin=MPD_MIN_EMIN;
118 ctx->round=MPD_ROUND_HALF_UP;
119 ctx->traps=MPD_Traps;
120 ctx->status=0;
121 ctx->newtrap=0;
122 ctx->clamp=0;
123 ctx->allcr=1;
124 }
125
126 void
127 mpd_basiccontext(mpd_context_t *ctx)
128 {
129 ctx->prec=9;
130 ctx->emax=MPD_MAX_EMAX;
131 ctx->emin=MPD_MIN_EMIN;
132 ctx->round=MPD_ROUND_HALF_UP;
133 ctx->traps=MPD_Traps|MPD_Clamped;
134 ctx->status=0;
135 ctx->newtrap=0;
136 ctx->clamp=0;
137 ctx->allcr=1;
138 }
139
140 int
141 mpd_ieee_context(mpd_context_t *ctx, int bits)
142 {
143 if (bits <= 0 || bits > MPD_IEEE_CONTEXT_MAX_BITS || bits % 32) {
144 return -1;
145 }
146
147 ctx->prec = 9 * (bits/32) - 2;
148 ctx->emax = 3 * ((mpd_ssize_t)1<<(bits/16+3));
149 ctx->emin = 1 - ctx->emax;
150 ctx->round=MPD_ROUND_HALF_EVEN;
151 ctx->traps=0;
152 ctx->status=0;
153 ctx->newtrap=0;
154 ctx->clamp=1;
155 ctx->allcr=1;
156
157 return 0;
158 }
159
160 mpd_ssize_t
161 mpd_getprec(const mpd_context_t *ctx)
162 {
163 return ctx->prec;
164 }
165
166 mpd_ssize_t
167 mpd_getemax(const mpd_context_t *ctx)
168 {
169 return ctx->emax;
170 }
171
172 mpd_ssize_t
173 mpd_getemin(const mpd_context_t *ctx)
174 {
175 return ctx->emin;
176 }
177
178 int
179 mpd_getround(const mpd_context_t *ctx)
180 {
181 return ctx->round;
182 }
183
184 uint32_t
185 mpd_gettraps(const mpd_context_t *ctx)
186 {
187 return ctx->traps;
188 }
189
190 uint32_t
191 mpd_getstatus(const mpd_context_t *ctx)
192 {
193 return ctx->status;
194 }
195
196 int
197 mpd_getclamp(const mpd_context_t *ctx)
198 {
199 return ctx->clamp;
200 }
201
202 int
203 mpd_getcr(const mpd_context_t *ctx)
204 {
205 return ctx->allcr;
206 }
207
208
209 int
210 mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec)
211 {
212 if (prec <= 0 || prec > MPD_MAX_PREC) {
213 return 0;
214 }
215 ctx->prec = prec;
216 return 1;
217 }
218
219 int
220 mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax)
221 {
222 if (emax < 0 || emax > MPD_MAX_EMAX) {
223 return 0;
224 }
225 ctx->emax = emax;
226 return 1;
227 }
228
229 int
230 mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin)
231 {
232 if (emin > 0 || emin < MPD_MIN_EMIN) {
233 return 0;
234 }
235 ctx->emin = emin;
236 return 1;
237 }
238
239 int
240 mpd_qsetround(mpd_context_t *ctx, int round)
241 {
242 if (!(0 <= round && round < MPD_ROUND_GUARD)) {
243 return 0;
244 }
245 ctx->round = round;
246 return 1;
247 }
248
249 int
250 mpd_qsettraps(mpd_context_t *ctx, uint32_t traps)
251 {
252 if (traps > MPD_Max_status) {
253 return 0;
254 }
255 ctx->traps = traps;
256 return 1;
257 }
258
259 int
260 mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags)
261 {
262 if (flags > MPD_Max_status) {
263 return 0;
264 }
265 ctx->status = flags;
266 return 1;
267 }
268
269 int
270 mpd_qsetclamp(mpd_context_t *ctx, int c)
271 {
272 if (c != 0 && c != 1) {
273 return 0;
274 }
275 ctx->clamp = c;
276 return 1;
277 }
278
279 int
280 mpd_qsetcr(mpd_context_t *ctx, int c)
281 {
282 if (c != 0 && c != 1) {
283 return 0;
284 }
285 ctx->allcr = c;
286 return 1;
287 }
288
289
290 void
291 mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags)
292 {
293 ctx->status |= flags;
294 if (flags&ctx->traps) {
295 ctx->newtrap = (flags&ctx->traps);
296 mpd_traphandler(ctx);
297 }
298 }
299
300
OLDNEW
« no previous file with comments | « Modules/_decimal/libmpdec/constants.h ('k') | Modules/_decimal/libmpdec/convolute.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+