summaryrefslogtreecommitdiffstats
path: root/Modules/_decimal/libmpdec/context.c
blob: 172794b67d800946e7030b5dd43da880cee6c6c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (c) 2008-2020 Stefan Krah. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */


#include "mpdecimal.h"

#include <signal.h>
#include <stdio.h>
#include <string.h>


void
mpd_dflt_traphandler(mpd_context_t *ctx)
{
    (void)ctx;
    raise(SIGFPE);
}

void (* mpd_traphandler)(mpd_context_t *) = mpd_dflt_traphandler;


/* Set guaranteed minimum number of coefficient words. The function may
   be used once at program start. Setting MPD_MINALLOC to out-of-bounds
   values is a catastrophic error, so in that case the function exits rather
   than relying on the user to check a return value. */
void
mpd_setminalloc(mpd_ssize_t n)
{
    static int minalloc_is_set = 0;

    if (minalloc_is_set) {
        mpd_err_warn("mpd_setminalloc: ignoring request to set "
                     "MPD_MINALLOC a second time\n");
        return;
    }
    if (n < MPD_MINALLOC_MIN || n > MPD_MINALLOC_MAX) {
        mpd_err_fatal("illegal value for MPD_MINALLOC"); /* GCOV_NOT_REACHED */
    }
    MPD_MINALLOC = n;
    minalloc_is_set = 1;
}

void
mpd_init(mpd_context_t *ctx, mpd_ssize_t prec)
{
    mpd_ssize_t ideal_minalloc;

    mpd_defaultcontext(ctx);

    if (!mpd_qsetprec(ctx, prec)) {
        mpd_addstatus_raise(ctx, MPD_Invalid_context);
        return;
    }

    ideal_minalloc = 2 * ((prec+MPD_RDIGITS-1) / MPD_RDIGITS);
    if (ideal_minalloc < MPD_MINALLOC_MIN) ideal_minalloc = MPD_MINALLOC_MIN;
    if (ideal_minalloc > MPD_MINALLOC_MAX) ideal_minalloc = MPD_MINALLOC_MAX;

    mpd_setminalloc(ideal_minalloc);
}

void
mpd_maxcontext(mpd_context_t *ctx)
{
    ctx->prec=MPD_MAX_PREC;
    ctx->emax=MPD_MAX_EMAX;
    ctx->emin=MPD_MIN_EMIN;
    ctx->round=MPD_ROUND_HALF_EVEN;
    ctx->traps=MPD_Traps;
    ctx->status=0;
    ctx->newtrap=0;
    ctx->clamp=0;
    ctx->allcr=1;
}

void
mpd_defaultcontext(mpd_context_t *ctx)
{
    ctx->prec=2*MPD_RDIGITS;
    ctx->emax=MPD_MAX_EMAX;
    ctx->emin=MPD_MIN_EMIN;
    ctx->round=MPD_ROUND_HALF_UP;
    ctx->traps=MPD_Traps;
    ctx->status=0;
    ctx->newtrap=0;
    ctx->clamp=0;
    ctx->allcr=1;
}

void
mpd_basiccontext(mpd_context_t *ctx)
{
    ctx->prec=9;
    ctx->emax=MPD_MAX_EMAX;
    ctx->emin=MPD_MIN_EMIN;
    ctx->round=MPD_ROUND_HALF_UP;
    ctx->traps=MPD_Traps|MPD_Clamped;
    ctx->status=0;
    ctx->newtrap=0;
    ctx->clamp=0;
    ctx->allcr=1;
}

int
mpd_ieee_context(mpd_context_t *ctx, int bits)
{
    if (bits <= 0 || bits > MPD_IEEE_CONTEXT_MAX_BITS || bits % 32) {
        return -1;
    }

    ctx->prec = 9 * (bits/32) - 2;
    ctx->emax = 3 * ((mpd_ssize_t)1<<(bits/16+3));
    ctx->emin = 1 - ctx->emax;
    ctx->round=MPD_ROUND_HALF_EVEN;
    ctx->traps=0;
    ctx->status=0;
    ctx->newtrap=0;
    ctx->clamp=1;
    ctx->allcr=1;

    return 0;
}

mpd_ssize_t
mpd_getprec(const mpd_context_t *ctx)
{
    return ctx->prec;
}

mpd_ssize_t
mpd_getemax(const mpd_context_t *ctx)
{
    return ctx->emax;
}

mpd_ssize_t
mpd_getemin(const mpd_context_t *ctx)
{
    return ctx->emin;
}

int
mpd_getround(const mpd_context_t *ctx)
{
    return ctx->round;
}

uint32_t
mpd_gettraps(const mpd_context_t *ctx)
{
    return ctx->traps;
}

uint32_t
mpd_getstatus(const mpd_context_t *ctx)
{
    return ctx->status;
}

int
mpd_getclamp(const mpd_context_t *ctx)
{
    return ctx->clamp;
}

int
mpd_getcr(const mpd_context_t *ctx)
{
    return ctx->allcr;
}


int
mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec)
{
    if (prec <= 0 || prec > MPD_MAX_PREC) {
        return 0;
    }
    ctx->prec = prec;
    return 1;
}

int
mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax)
{
    if (emax < 0 || emax > MPD_MAX_EMAX) {
        return 0;
    }
    ctx->emax = emax;
    return 1;
}

int
mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin)
{
    if (emin > 0 || emin < MPD_MIN_EMIN) {
        return 0;
    }
    ctx->emin = emin;
    return 1;
}

int
mpd_qsetround(mpd_context_t *ctx, int round)
{
    if (!(0 <= round && round < MPD_ROUND_GUARD)) {
        return 0;
    }
    ctx->round = round;
    return 1;
}

int
mpd_qsettraps(mpd_context_t *ctx, uint32_t flags)
{
    if (flags > MPD_Max_status) {
        return 0;
    }
    ctx->traps = flags;
    return 1;
}

int
mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags)
{
    if (flags > MPD_Max_status) {
        return 0;
    }
    ctx->status = flags;
    return 1;
}

int
mpd_qsetclamp(mpd_context_t *ctx, int c)
{
    if (c != 0 && c != 1) {
        return 0;
    }
    ctx->clamp = c;
    return 1;
}

int
mpd_qsetcr(mpd_context_t *ctx, int c)
{
    if (c != 0 && c != 1) {
        return 0;
    }
    ctx->allcr = c;
    return 1;
}


void
mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags)
{
    ctx->status |= flags;
    if (flags&ctx->traps) {
        ctx->newtrap = (flags&ctx->traps);
        mpd_traphandler(ctx);
    }
}