Coverage Report

Created: 2026-02-04 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tarantool/third_party/decNumber/decContext.c
Line
Count
Source
1
/* ------------------------------------------------------------------ */
2
/* Decimal Context module                                             */
3
/* ------------------------------------------------------------------ */
4
/* Copyright (c) IBM Corporation, 2000, 2009.  All rights reserved.   */
5
/*                                                                    */
6
/* This software is made available under the terms of the             */
7
/* ICU License -- ICU 1.8.1 and later.                                */
8
/*                                                                    */
9
/* The description and User's Guide ("The decNumber C Library") for   */
10
/* this software is called decNumber.pdf.  This document is           */
11
/* available, together with arithmetic and format specifications,     */
12
/* testcases, and Web links, on the General Decimal Arithmetic page.  */
13
/*                                                                    */
14
/* Please send comments, suggestions, and corrections to the author:  */
15
/*   mfc@uk.ibm.com                                                   */
16
/*   Mike Cowlishaw, IBM Fellow                                       */
17
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
18
/* ------------------------------------------------------------------ */
19
/* This module comprises the routines for handling arithmetic         */
20
/* context structures.                                                */
21
/* ------------------------------------------------------------------ */
22
23
#include <string.h>           // for strcmp
24
#include <stdio.h>            // for printf if DECCHECK
25
#include "decContext.h"       // context and base types
26
#include "decNumberLocal.h"   // decNumber local types, etc.
27
28
/* compile-time endian tester [assumes sizeof(Int)>1] */
29
static  const  Int mfcone=1;                       // constant 1
30
static  const  Flag *mfctop=(const Flag *)&mfcone; // -> top byte
31
0
#define LITEND *mfctop        // named flag; 1=little-endian
32
33
/* ------------------------------------------------------------------ */
34
/* round-for-reround digits                                           */
35
/* ------------------------------------------------------------------ */
36
const uByte DECSTICKYTAB[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
37
38
/* ------------------------------------------------------------------ */
39
/* Powers of ten (powers[n]==10**n, 0<=n<=9)                          */
40
/* ------------------------------------------------------------------ */
41
const uInt DECPOWERS[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
42
                          10000000, 100000000, 1000000000};
43
44
const uLong bigpowers[20]={1, 10, 100, 1000, 10000, 100000, 1000000,
45
                           10000000, 100000000, 1000000000, 10000000000,
46
         100000000000, 1000000000000, 10000000000000,
47
         100000000000000, 1000000000000000,
48
         10000000000000000, 100000000000000000,
49
         1000000000000000000ULL, 10000000000000000000ULL};
50
51
/* ------------------------------------------------------------------ */
52
/* decContextClearStatus -- clear bits in current status              */
53
/*                                                                    */
54
/*  context is the context structure to be queried                    */
55
/*  mask indicates the bits to be cleared (the status bit that        */
56
/*    corresponds to each 1 bit in the mask is cleared)               */
57
/*  returns context                                                   */
58
/*                                                                    */
59
/* No error is possible.                                              */
60
/* ------------------------------------------------------------------ */
61
0
decContext *decContextClearStatus(decContext *context, uInt mask) {
62
0
  context->status&=~mask;
63
0
  return context;
64
0
  } // decContextClearStatus
65
66
/* ------------------------------------------------------------------ */
67
/* decContextDefault -- initialize a context structure                */
68
/*                                                                    */
69
/*  context is the structure to be initialized                        */
70
/*  kind selects the required set of default values, one of:          */
71
/*      DEC_INIT_BASE       -- select ANSI X3-274 defaults            */
72
/*      DEC_INIT_DECIMAL32  -- select IEEE 754 defaults, 32-bit       */
73
/*      DEC_INIT_DECIMAL64  -- select IEEE 754 defaults, 64-bit       */
74
/*      DEC_INIT_DECIMAL128 -- select IEEE 754 defaults, 128-bit      */
75
/*      For any other value a valid context is returned, but with     */
76
/*      Invalid_operation set in the status field.                    */
77
/*  returns a context structure with the appropriate initial values.  */
78
/* ------------------------------------------------------------------ */
79
0
decContext * decContextDefault(decContext *context, Int kind) {
80
  // set defaults...
81
0
  context->digits=9;                         // 9 digits
82
0
  context->emax=DEC_MAX_EMAX;                // 9-digit exponents
83
0
  context->emin=DEC_MIN_EMIN;                // .. balanced
84
0
  context->round=DEC_ROUND_HALF_UP;          // 0.5 rises
85
0
  context->traps=DEC_Errors;                 // all but informational
86
0
  context->status=0;                         // cleared
87
0
  context->clamp=0;                          // no clamping
88
  #if DECSUBSET
89
  context->extended=0;                       // cleared
90
  #endif
91
0
  switch (kind) {
92
0
    case DEC_INIT_BASE:
93
      // [use defaults]
94
0
      break;
95
0
    case DEC_INIT_DECIMAL32:
96
0
      context->digits=7;                     // digits
97
0
      context->emax=96;                      // Emax
98
0
      context->emin=-95;                     // Emin
99
0
      context->round=DEC_ROUND_HALF_EVEN;    // 0.5 to nearest even
100
0
      context->traps=0;                      // no traps set
101
0
      context->clamp=1;                      // clamp exponents
102
      #if DECSUBSET
103
      context->extended=1;                   // set
104
      #endif
105
0
      break;
106
0
    case DEC_INIT_DECIMAL64:
107
0
      context->digits=16;                    // digits
108
0
      context->emax=384;                     // Emax
109
0
      context->emin=-383;                    // Emin
110
0
      context->round=DEC_ROUND_HALF_EVEN;    // 0.5 to nearest even
111
0
      context->traps=0;                      // no traps set
112
0
      context->clamp=1;                      // clamp exponents
113
      #if DECSUBSET
114
      context->extended=1;                   // set
115
      #endif
116
0
      break;
117
0
    case DEC_INIT_DECIMAL128:
118
0
      context->digits=34;                    // digits
119
0
      context->emax=6144;                    // Emax
120
0
      context->emin=-6143;                   // Emin
121
0
      context->round=DEC_ROUND_HALF_EVEN;    // 0.5 to nearest even
122
0
      context->traps=0;                      // no traps set
123
0
      context->clamp=1;                      // clamp exponents
124
      #if DECSUBSET
125
      context->extended=1;                   // set
126
      #endif
127
0
      break;
128
129
0
    default:                                 // invalid Kind
130
      // use defaults, and ..
131
0
      decContextSetStatus(context, DEC_Invalid_operation); // trap
132
0
    }
133
134
0
  return context;} // decContextDefault
135
136
/* ------------------------------------------------------------------ */
137
/* decContextGetRounding -- return current rounding mode              */
138
/*                                                                    */
139
/*  context is the context structure to be queried                    */
140
/*  returns the rounding mode                                         */
141
/*                                                                    */
142
/* No error is possible.                                              */
143
/* ------------------------------------------------------------------ */
144
0
enum rounding decContextGetRounding(decContext *context) {
145
0
  return context->round;
146
0
  } // decContextGetRounding
147
148
/* ------------------------------------------------------------------ */
149
/* decContextGetStatus -- return current status                       */
150
/*                                                                    */
151
/*  context is the context structure to be queried                    */
152
/*  returns status                                                    */
153
/*                                                                    */
154
/* No error is possible.                                              */
155
/* ------------------------------------------------------------------ */
156
11.0k
uInt decContextGetStatus(decContext *context) {
157
11.0k
  return context->status;
158
11.0k
  } // decContextGetStatus
159
160
/* ------------------------------------------------------------------ */
161
/* decContextRestoreStatus -- restore bits in current status          */
162
/*                                                                    */
163
/*  context is the context structure to be updated                    */
164
/*  newstatus is the source for the bits to be restored               */
165
/*  mask indicates the bits to be restored (the status bit that       */
166
/*    corresponds to each 1 bit in the mask is set to the value of    */
167
/*    the correspnding bit in newstatus)                              */
168
/*  returns context                                                   */
169
/*                                                                    */
170
/* No error is possible.                                              */
171
/* ------------------------------------------------------------------ */
172
decContext *decContextRestoreStatus(decContext *context,
173
0
                                    uInt newstatus, uInt mask) {
174
0
  context->status&=~mask;               // clear the selected bits
175
0
  context->status|=(mask&newstatus);    // or in the new bits
176
0
  return context;
177
0
  } // decContextRestoreStatus
178
179
/* ------------------------------------------------------------------ */
180
/* decContextSaveStatus -- save bits in current status                */
181
/*                                                                    */
182
/*  context is the context structure to be queried                    */
183
/*  mask indicates the bits to be saved (the status bits that         */
184
/*    correspond to each 1 bit in the mask are saved)                 */
185
/*  returns the AND of the mask and the current status                */
186
/*                                                                    */
187
/* No error is possible.                                              */
188
/* ------------------------------------------------------------------ */
189
0
uInt decContextSaveStatus(decContext *context, uInt mask) {
190
0
  return context->status&mask;
191
0
  } // decContextSaveStatus
192
193
/* ------------------------------------------------------------------ */
194
/* decContextSetRounding -- set current rounding mode                 */
195
/*                                                                    */
196
/*  context is the context structure to be updated                    */
197
/*  newround is the value which will replace the current mode         */
198
/*  returns context                                                   */
199
/*                                                                    */
200
/* No error is possible.                                              */
201
/* ------------------------------------------------------------------ */
202
decContext *decContextSetRounding(decContext *context,
203
0
                                  enum rounding newround) {
204
0
  context->round=newround;
205
0
  return context;
206
0
  } // decContextSetRounding
207
208
/* ------------------------------------------------------------------ */
209
/* decContextSetStatus -- set status and raise trap if appropriate    */
210
/*                                                                    */
211
/*  context is the context structure to be updated                    */
212
/*  status  is the DEC_ exception code                                */
213
/*  returns the context structure                                     */
214
/*                                                                    */
215
/* Control may never return from this routine, if there is a signal   */
216
/* handler and it takes a long jump.                                  */
217
/* ------------------------------------------------------------------ */
218
3.47k
decContext * decContextSetStatus(decContext *context, uInt status) {
219
3.47k
  context->status|=status;
220
3.47k
  if (status & context->traps) raise(SIGFPE);
221
3.47k
  return context;} // decContextSetStatus
222
223
/* ------------------------------------------------------------------ */
224
/* decContextSetStatusFromString -- set status from a string + trap   */
225
/*                                                                    */
226
/*  context is the context structure to be updated                    */
227
/*  string is a string exactly equal to one that might be returned    */
228
/*            by decContextStatusToString                             */
229
/*                                                                    */
230
/*  The status bit corresponding to the string is set, and a trap     */
231
/*  is raised if appropriate.                                         */
232
/*                                                                    */
233
/*  returns the context structure, unless the string is equal to      */
234
/*    DEC_Condition_MU or is not recognized.  In these cases NULL is  */
235
/*    returned.                                                       */
236
/* ------------------------------------------------------------------ */
237
decContext * decContextSetStatusFromString(decContext *context,
238
0
                                           const char *string) {
239
0
  if (strcmp(string, DEC_Condition_CS)==0)
240
0
    return decContextSetStatus(context, DEC_Conversion_syntax);
241
0
  if (strcmp(string, DEC_Condition_DZ)==0)
242
0
    return decContextSetStatus(context, DEC_Division_by_zero);
243
0
  if (strcmp(string, DEC_Condition_DI)==0)
244
0
    return decContextSetStatus(context, DEC_Division_impossible);
245
0
  if (strcmp(string, DEC_Condition_DU)==0)
246
0
    return decContextSetStatus(context, DEC_Division_undefined);
247
0
  if (strcmp(string, DEC_Condition_IE)==0)
248
0
    return decContextSetStatus(context, DEC_Inexact);
249
0
  if (strcmp(string, DEC_Condition_IS)==0)
250
0
    return decContextSetStatus(context, DEC_Insufficient_storage);
251
0
  if (strcmp(string, DEC_Condition_IC)==0)
252
0
    return decContextSetStatus(context, DEC_Invalid_context);
253
0
  if (strcmp(string, DEC_Condition_IO)==0)
254
0
    return decContextSetStatus(context, DEC_Invalid_operation);
255
  #if DECSUBSET
256
  if (strcmp(string, DEC_Condition_LD)==0)
257
    return decContextSetStatus(context, DEC_Lost_digits);
258
  #endif
259
0
  if (strcmp(string, DEC_Condition_OV)==0)
260
0
    return decContextSetStatus(context, DEC_Overflow);
261
0
  if (strcmp(string, DEC_Condition_PA)==0)
262
0
    return decContextSetStatus(context, DEC_Clamped);
263
0
  if (strcmp(string, DEC_Condition_RO)==0)
264
0
    return decContextSetStatus(context, DEC_Rounded);
265
0
  if (strcmp(string, DEC_Condition_SU)==0)
266
0
    return decContextSetStatus(context, DEC_Subnormal);
267
0
  if (strcmp(string, DEC_Condition_UN)==0)
268
0
    return decContextSetStatus(context, DEC_Underflow);
269
0
  if (strcmp(string, DEC_Condition_ZE)==0)
270
0
    return context;
271
0
  return NULL;  // Multiple status, or unknown
272
0
  } // decContextSetStatusFromString
273
274
/* ------------------------------------------------------------------ */
275
/* decContextSetStatusFromStringQuiet -- set status from a string     */
276
/*                                                                    */
277
/*  context is the context structure to be updated                    */
278
/*  string is a string exactly equal to one that might be returned    */
279
/*            by decContextStatusToString                             */
280
/*                                                                    */
281
/*  The status bit corresponding to the string is set; no trap is     */
282
/*  raised.                                                           */
283
/*                                                                    */
284
/*  returns the context structure, unless the string is equal to      */
285
/*    DEC_Condition_MU or is not recognized.  In these cases NULL is  */
286
/*    returned.                                                       */
287
/* ------------------------------------------------------------------ */
288
decContext * decContextSetStatusFromStringQuiet(decContext *context,
289
0
                                                const char *string) {
290
0
  if (strcmp(string, DEC_Condition_CS)==0)
291
0
    return decContextSetStatusQuiet(context, DEC_Conversion_syntax);
292
0
  if (strcmp(string, DEC_Condition_DZ)==0)
293
0
    return decContextSetStatusQuiet(context, DEC_Division_by_zero);
294
0
  if (strcmp(string, DEC_Condition_DI)==0)
295
0
    return decContextSetStatusQuiet(context, DEC_Division_impossible);
296
0
  if (strcmp(string, DEC_Condition_DU)==0)
297
0
    return decContextSetStatusQuiet(context, DEC_Division_undefined);
298
0
  if (strcmp(string, DEC_Condition_IE)==0)
299
0
    return decContextSetStatusQuiet(context, DEC_Inexact);
300
0
  if (strcmp(string, DEC_Condition_IS)==0)
301
0
    return decContextSetStatusQuiet(context, DEC_Insufficient_storage);
302
0
  if (strcmp(string, DEC_Condition_IC)==0)
303
0
    return decContextSetStatusQuiet(context, DEC_Invalid_context);
304
0
  if (strcmp(string, DEC_Condition_IO)==0)
305
0
    return decContextSetStatusQuiet(context, DEC_Invalid_operation);
306
  #if DECSUBSET
307
  if (strcmp(string, DEC_Condition_LD)==0)
308
    return decContextSetStatusQuiet(context, DEC_Lost_digits);
309
  #endif
310
0
  if (strcmp(string, DEC_Condition_OV)==0)
311
0
    return decContextSetStatusQuiet(context, DEC_Overflow);
312
0
  if (strcmp(string, DEC_Condition_PA)==0)
313
0
    return decContextSetStatusQuiet(context, DEC_Clamped);
314
0
  if (strcmp(string, DEC_Condition_RO)==0)
315
0
    return decContextSetStatusQuiet(context, DEC_Rounded);
316
0
  if (strcmp(string, DEC_Condition_SU)==0)
317
0
    return decContextSetStatusQuiet(context, DEC_Subnormal);
318
0
  if (strcmp(string, DEC_Condition_UN)==0)
319
0
    return decContextSetStatusQuiet(context, DEC_Underflow);
320
0
  if (strcmp(string, DEC_Condition_ZE)==0)
321
0
    return context;
322
0
  return NULL;  // Multiple status, or unknown
323
0
  } // decContextSetStatusFromStringQuiet
324
325
/* ------------------------------------------------------------------ */
326
/* decContextSetStatusQuiet -- set status without trap                */
327
/*                                                                    */
328
/*  context is the context structure to be updated                    */
329
/*  status  is the DEC_ exception code                                */
330
/*  returns the context structure                                     */
331
/*                                                                    */
332
/* No error is possible.                                              */
333
/* ------------------------------------------------------------------ */
334
0
decContext * decContextSetStatusQuiet(decContext *context, uInt status) {
335
0
  context->status|=status;
336
0
  return context;} // decContextSetStatusQuiet
337
338
/* ------------------------------------------------------------------ */
339
/* decContextStatusToString -- convert status flags to a string       */
340
/*                                                                    */
341
/*  context is a context with valid status field                      */
342
/*                                                                    */
343
/*  returns a constant string describing the condition.  If multiple  */
344
/*    (or no) flags are set, a generic constant message is returned.  */
345
/* ------------------------------------------------------------------ */
346
0
const char *decContextStatusToString(const decContext *context) {
347
0
  Int status=context->status;
348
349
  // test the five IEEE first, as some of the others are ambiguous when
350
  // DECEXTFLAG=0
351
0
  if (status==DEC_Invalid_operation    ) return DEC_Condition_IO;
352
0
  if (status==DEC_Division_by_zero     ) return DEC_Condition_DZ;
353
0
  if (status==DEC_Overflow             ) return DEC_Condition_OV;
354
0
  if (status==DEC_Underflow            ) return DEC_Condition_UN;
355
0
  if (status==DEC_Inexact              ) return DEC_Condition_IE;
356
357
0
  if (status==DEC_Division_impossible  ) return DEC_Condition_DI;
358
0
  if (status==DEC_Division_undefined   ) return DEC_Condition_DU;
359
0
  if (status==DEC_Rounded              ) return DEC_Condition_RO;
360
0
  if (status==DEC_Clamped              ) return DEC_Condition_PA;
361
0
  if (status==DEC_Subnormal            ) return DEC_Condition_SU;
362
0
  if (status==DEC_Conversion_syntax    ) return DEC_Condition_CS;
363
0
  if (status==DEC_Insufficient_storage ) return DEC_Condition_IS;
364
0
  if (status==DEC_Invalid_context      ) return DEC_Condition_IC;
365
  #if DECSUBSET
366
  if (status==DEC_Lost_digits          ) return DEC_Condition_LD;
367
  #endif
368
0
  if (status==0                        ) return DEC_Condition_ZE;
369
0
  return DEC_Condition_MU;  // Multiple errors
370
0
  } // decContextStatusToString
371
372
/* ------------------------------------------------------------------ */
373
/* decContextTestEndian -- test whether DECLITEND is set correctly    */
374
/*                                                                    */
375
/*  quiet is 1 to suppress message; 0 otherwise                       */
376
/*  returns 0 if DECLITEND is correct                                 */
377
/*          1 if DECLITEND is incorrect and should be 1               */
378
/*         -1 if DECLITEND is incorrect and should be 0               */
379
/*                                                                    */
380
/* A message is displayed if the return value is not 0 and quiet==0.  */
381
/*                                                                    */
382
/* No error is possible.                                              */
383
/* ------------------------------------------------------------------ */
384
0
Int decContextTestEndian(Flag quiet) {
385
0
  Int res=0;                  // optimist
386
0
  uInt dle=(uInt)DECLITEND;   // unsign
387
0
  if (dle>1) dle=1;           // ensure 0 or 1
388
389
0
  if (LITEND!=DECLITEND) {
390
0
    if (!quiet) {             // always refer to this
391
0
      #if DECPRINT
392
0
      const char *adj;
393
0
      if (LITEND) adj="little";
394
0
             else adj="big";
395
0
      printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
396
0
             DECLITEND, adj);
397
0
      #endif
398
0
      }
399
0
    res=(Int)LITEND-dle;
400
0
    }
401
0
  return res;
402
0
  } // decContextTestEndian
403
404
/* ------------------------------------------------------------------ */
405
/* decContextTestSavedStatus -- test bits in saved status             */
406
/*                                                                    */
407
/*  oldstatus is the status word to be tested                         */
408
/*  mask indicates the bits to be tested (the oldstatus bits that     */
409
/*    correspond to each 1 bit in the mask are tested)                */
410
/*  returns 1 if any of the tested bits are 1, or 0 otherwise         */
411
/*                                                                    */
412
/* No error is possible.                                              */
413
/* ------------------------------------------------------------------ */
414
0
uInt decContextTestSavedStatus(uInt oldstatus, uInt mask) {
415
0
  return (oldstatus&mask)!=0;
416
0
  } // decContextTestSavedStatus
417
418
/* ------------------------------------------------------------------ */
419
/* decContextTestStatus -- test bits in current status                */
420
/*                                                                    */
421
/*  context is the context structure to be updated                    */
422
/*  mask indicates the bits to be tested (the status bits that        */
423
/*    correspond to each 1 bit in the mask are tested)                */
424
/*  returns 1 if any of the tested bits are 1, or 0 otherwise         */
425
/*                                                                    */
426
/* No error is possible.                                              */
427
/* ------------------------------------------------------------------ */
428
0
uInt decContextTestStatus(decContext *context, uInt mask) {
429
0
  return (context->status&mask)!=0;
430
0
  } // decContextTestStatus
431
432
/* ------------------------------------------------------------------ */
433
/* decContextZeroStatus -- clear all status bits                      */
434
/*                                                                    */
435
/*  context is the context structure to be updated                    */
436
/*  returns context                                                   */
437
/*                                                                    */
438
/* No error is possible.                                              */
439
/* ------------------------------------------------------------------ */
440
11.3k
decContext *decContextZeroStatus(decContext *context) {
441
11.3k
  context->status=0;
442
11.3k
  return context;
443
11.3k
  } // decContextZeroStatus
444