ยปCore Development>Code coverage>Modules/_decimal/libmpdec/context.c

Python code coverage for Modules/_decimal/libmpdec/context.c

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