bigdecimal 2.0.0 → 4.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,62 +10,49 @@
10
10
  #define RUBY_BIG_DECIMAL_H 1
11
11
 
12
12
  #define RUBY_NO_OLD_COMPATIBILITY
13
-
14
13
  #include "ruby/ruby.h"
15
- #include <float.h>
16
-
17
- #ifndef RB_UNUSED_VAR
18
- # ifdef __GNUC__
19
- # define RB_UNUSED_VAR(x) x __attribute__ ((unused))
20
- # else
21
- # define RB_UNUSED_VAR(x) x
22
- # endif
23
- #endif
14
+ #include "missing.h"
24
15
 
25
- #ifndef UNREACHABLE
26
- # define UNREACHABLE /* unreachable */
16
+ #ifdef HAVE_FLOAT_H
17
+ # include <float.h>
27
18
  #endif
28
19
 
29
- #undef BDIGIT
30
- #undef SIZEOF_BDIGITS
31
- #undef BDIGIT_DBL
32
- #undef BDIGIT_DBL_SIGNED
33
- #undef PRI_BDIGIT_PREFIX
34
- #undef PRI_BDIGIT_DBL_PREFIX
35
-
36
- #ifdef HAVE_INT64_T
37
- # define BDIGIT uint32_t
38
- # define BDIGIT_DBL uint64_t
39
- # define BDIGIT_DBL_SIGNED int64_t
40
- # define SIZEOF_BDIGITS 4
41
- # define PRI_BDIGIT_PREFIX ""
42
- # ifdef PRI_LL_PREFIX
43
- # define PRI_BDIGIT_DBL_PREFIX PRI_LL_PREFIX
44
- # else
45
- # define PRI_BDIGIT_DBL_PREFIX "l"
46
- # endif
20
+ #define DECDIG uint32_t
21
+ #define DECDIG_DBL uint64_t
22
+ #define DECDIG_DBL_SIGNED int64_t
23
+ #define SIZEOF_DECDIG 4
24
+ #define PRI_DECDIG_PREFIX ""
25
+ #ifdef PRI_LL_PREFIX
26
+ # define PRI_DECDIG_DBL_PREFIX PRI_LL_PREFIX
47
27
  #else
48
- # define BDIGIT uint16_t
49
- # define BDIGIT_DBL uint32_t
50
- # define BDIGIT_DBL_SIGNED int32_t
51
- # define SIZEOF_BDIGITS 2
52
- # define PRI_BDIGIT_PREFIX "h"
53
- # define PRI_BDIGIT_DBL_PREFIX ""
28
+ # define PRI_DECDIG_DBL_PREFIX "l"
54
29
  #endif
55
30
 
56
- #define PRIdBDIGIT PRI_BDIGIT_PREFIX"d"
57
- #define PRIiBDIGIT PRI_BDIGIT_PREFIX"i"
58
- #define PRIoBDIGIT PRI_BDIGIT_PREFIX"o"
59
- #define PRIuBDIGIT PRI_BDIGIT_PREFIX"u"
60
- #define PRIxBDIGIT PRI_BDIGIT_PREFIX"x"
61
- #define PRIXBDIGIT PRI_BDIGIT_PREFIX"X"
31
+ #define PRIdDECDIG PRI_DECDIG_PREFIX"d"
32
+ #define PRIiDECDIG PRI_DECDIG_PREFIX"i"
33
+ #define PRIoDECDIG PRI_DECDIG_PREFIX"o"
34
+ #define PRIuDECDIG PRI_DECDIG_PREFIX"u"
35
+ #define PRIxDECDIG PRI_DECDIG_PREFIX"x"
36
+ #define PRIXDECDIG PRI_DECDIG_PREFIX"X"
37
+
38
+ #define PRIdDECDIG_DBL PRI_DECDIG_DBL_PREFIX"d"
39
+ #define PRIiDECDIG_DBL PRI_DECDIG_DBL_PREFIX"i"
40
+ #define PRIoDECDIG_DBL PRI_DECDIG_DBL_PREFIX"o"
41
+ #define PRIuDECDIG_DBL PRI_DECDIG_DBL_PREFIX"u"
42
+ #define PRIxDECDIG_DBL PRI_DECDIG_DBL_PREFIX"x"
43
+ #define PRIXDECDIG_DBL PRI_DECDIG_DBL_PREFIX"X"
44
+
45
+ #define BIGDECIMAL_BASE ((DECDIG)1000000000U)
46
+ #define BIGDECIMAL_COMPONENT_FIGURES 9
47
+ /*
48
+ * The number of components required for a 64-bit integer.
49
+ *
50
+ * INT64_MAX: 9_223372036_854775807
51
+ * UINT64_MAX: 18_446744073_709551615
52
+ */
53
+ #define BIGDECIMAL_INT64_MAX_LENGTH 3
62
54
 
63
- #define PRIdBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"d"
64
- #define PRIiBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"i"
65
- #define PRIoBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"o"
66
- #define PRIuBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"u"
67
- #define PRIxBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"x"
68
- #define PRIXBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"X"
55
+ #define BIGDECIMAL_DOUBLE_FIGURES (1+DBL_DIG)
69
56
 
70
57
  #if defined(__cplusplus)
71
58
  extern "C" {
@@ -74,111 +61,8 @@ extern "C" {
74
61
  #endif
75
62
  #endif
76
63
 
77
- #ifndef HAVE_LABS
78
- static inline long
79
- labs(long const x)
80
- {
81
- if (x < 0) return -x;
82
- return x;
83
- }
84
- #endif
85
-
86
- #ifndef HAVE_LLABS
87
- static inline LONG_LONG
88
- llabs(LONG_LONG const x)
89
- {
90
- if (x < 0) return -x;
91
- return x;
92
- }
93
- #endif
94
-
95
- #ifndef HAVE_FINITE
96
- static int
97
- finite(double)
98
- {
99
- return !isnan(n) && !isinf(n);
100
- }
101
- #endif
102
-
103
- #ifndef isfinite
104
- # ifndef HAVE_ISFINITE
105
- # define HAVE_ISFINITE 1
106
- # define isfinite(x) finite(x)
107
- # endif
108
- #endif
109
-
110
- #ifndef FIX_CONST_VALUE_PTR
111
- # if defined(__fcc__) || defined(__fcc_version) || \
112
- defined(__FCC__) || defined(__FCC_VERSION)
113
- /* workaround for old version of Fujitsu C Compiler (fcc) */
114
- # define FIX_CONST_VALUE_PTR(x) ((const VALUE *)(x))
115
- # else
116
- # define FIX_CONST_VALUE_PTR(x) (x)
117
- # endif
118
- #endif
119
-
120
- #ifndef HAVE_RB_ARRAY_CONST_PTR
121
- static inline const VALUE *
122
- rb_array_const_ptr(VALUE a)
123
- {
124
- return FIX_CONST_VALUE_PTR((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ?
125
- RARRAY(a)->as.ary : RARRAY(a)->as.heap.ptr);
126
- }
127
- #endif
128
-
129
- #ifndef RARRAY_CONST_PTR
130
- # define RARRAY_CONST_PTR(a) rb_array_const_ptr(a)
131
- #endif
132
-
133
- #ifndef RARRAY_AREF
134
- # define RARRAY_AREF(a, i) (RARRAY_CONST_PTR(a)[i])
135
- #endif
136
-
137
- #ifndef HAVE_RB_SYM2STR
138
- static inline VALUE
139
- rb_sym2str(VALUE sym)
140
- {
141
- return rb_id2str(SYM2ID(sym));
142
- }
143
- #endif
144
-
145
- #ifndef ST2FIX
146
- # undef RB_ST2FIX
147
- # define RB_ST2FIX(h) LONG2FIX((long)(h))
148
- # define ST2FIX(h) RB_ST2FIX(h)
149
- #endif
150
-
151
- #ifdef vabs
152
- # undef vabs
153
- #endif
154
- #if SIZEOF_VALUE <= SIZEOF_INT
155
- # define vabs abs
156
- #elif SIZEOF_VALUE <= SIZEOF_LONG
157
- # define vabs labs
158
- #elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
159
- # define vabs llabs
160
- #endif
161
-
162
64
  extern VALUE rb_cBigDecimal;
163
65
 
164
- #if 0 || SIZEOF_BDIGITS >= 16
165
- # define RMPD_COMPONENT_FIGURES 38
166
- # define RMPD_BASE ((BDIGIT)100000000000000000000000000000000000000U)
167
- #elif SIZEOF_BDIGITS >= 8
168
- # define RMPD_COMPONENT_FIGURES 19
169
- # define RMPD_BASE ((BDIGIT)10000000000000000000U)
170
- #elif SIZEOF_BDIGITS >= 4
171
- # define RMPD_COMPONENT_FIGURES 9
172
- # define RMPD_BASE ((BDIGIT)1000000000U)
173
- #elif SIZEOF_BDIGITS >= 2
174
- # define RMPD_COMPONENT_FIGURES 4
175
- # define RMPD_BASE ((BDIGIT)10000U)
176
- #else
177
- # define RMPD_COMPONENT_FIGURES 2
178
- # define RMPD_BASE ((BDIGIT)100U)
179
- #endif
180
-
181
-
182
66
  /*
183
67
  * NaN & Infinity
184
68
  */
@@ -193,7 +77,7 @@ extern VALUE rb_cBigDecimal;
193
77
  */
194
78
  #define VP_EXPORT static
195
79
 
196
- /* Exception codes */
80
+ /* Exception mode */
197
81
  #define VP_EXCEPTION_ALL ((unsigned short)0x00FF)
198
82
  #define VP_EXCEPTION_INFINITY ((unsigned short)0x0001)
199
83
  #define VP_EXCEPTION_NaN ((unsigned short)0x0002)
@@ -203,22 +87,39 @@ extern VALUE rb_cBigDecimal;
203
87
 
204
88
  /* Following 2 exceptions can't controlled by user */
205
89
  #define VP_EXCEPTION_OP ((unsigned short)0x0020)
206
- #define VP_EXCEPTION_MEMORY ((unsigned short)0x0040)
207
90
 
208
- #define RMPD_EXCEPTION_MODE_DEFAULT 0U
91
+ #define BIGDECIMAL_EXCEPTION_MODE_DEFAULT 0U
209
92
 
210
- /* Computation mode */
93
+ /* This is used in BigDecimal#mode */
211
94
  #define VP_ROUND_MODE ((unsigned short)0x0100)
212
- #define VP_ROUND_UP 1
213
- #define VP_ROUND_DOWN 2
214
- #define VP_ROUND_HALF_UP 3
215
- #define VP_ROUND_HALF_DOWN 4
216
- #define VP_ROUND_CEIL 5
217
- #define VP_ROUND_FLOOR 6
218
- #define VP_ROUND_HALF_EVEN 7
219
-
220
- #define RMPD_ROUNDING_MODE_DEFAULT VP_ROUND_HALF_UP
221
95
 
96
+ /* Rounding mode */
97
+ #define VP_ROUND_UP RBD_ROUND_UP
98
+ #define VP_ROUND_DOWN RBD_ROUND_DOWN
99
+ #define VP_ROUND_HALF_UP RBD_ROUND_HALF_UP
100
+ #define VP_ROUND_HALF_DOWN RBD_ROUND_HALF_DOWN
101
+ #define VP_ROUND_CEIL RBD_ROUND_CEIL
102
+ #define VP_ROUND_FLOOR RBD_ROUND_FLOOR
103
+ #define VP_ROUND_HALF_EVEN RBD_ROUND_HALF_EVEN
104
+
105
+ enum rbd_rounding_mode {
106
+ RBD_ROUND_UP = 1,
107
+ RBD_ROUND_DOWN = 2,
108
+ RBD_ROUND_HALF_UP = 3,
109
+ RBD_ROUND_HALF_DOWN = 4,
110
+ RBD_ROUND_CEIL = 5,
111
+ RBD_ROUND_FLOOR = 6,
112
+ RBD_ROUND_HALF_EVEN = 7,
113
+
114
+ RBD_ROUND_DEFAULT = RBD_ROUND_HALF_UP,
115
+ RBD_ROUND_TRUNCATE = RBD_ROUND_DOWN,
116
+ RBD_ROUND_BANKER = RBD_ROUND_HALF_EVEN,
117
+ RBD_ROUND_CEILING = RBD_ROUND_CEIL
118
+ };
119
+
120
+ #define BIGDECIMAL_ROUNDING_MODE_DEFAULT VP_ROUND_HALF_UP
121
+
122
+ /* Sign flag */
222
123
  #define VP_SIGN_NaN 0 /* NaN */
223
124
  #define VP_SIGN_POSITIVE_ZERO 1 /* Positive zero */
224
125
  #define VP_SIGN_NEGATIVE_ZERO -1 /* Negative zero */
@@ -227,6 +128,7 @@ extern VALUE rb_cBigDecimal;
227
128
  #define VP_SIGN_POSITIVE_INFINITE 3 /* Positive infinite number */
228
129
  #define VP_SIGN_NEGATIVE_INFINITE -3 /* Negative infinite number */
229
130
 
131
+ /* The size of fraction part array */
230
132
  #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
231
133
  #define FLEXIBLE_ARRAY_SIZE /* */
232
134
  #elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
@@ -240,7 +142,6 @@ extern VALUE rb_cBigDecimal;
240
142
  * r = 0.xxxxxxxxx *BASE**exponent
241
143
  */
242
144
  typedef struct {
243
- VALUE obj; /* Back pointer(VALUE) for Ruby object. */
244
145
  size_t MaxPrec; /* Maximum precision size */
245
146
  /* This is the actual size of frac[] */
246
147
  /*(frac[0] to frac[MaxPrec] are available). */
@@ -259,30 +160,26 @@ typedef struct {
259
160
  * -3 : Negative infinite number
260
161
  */
261
162
  short flag; /* Not used in vp_routines,space for user. */
262
- BDIGIT frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
163
+ DECDIG frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
263
164
  } Real;
264
165
 
166
+ typedef struct {
167
+ VALUE bigdecimal;
168
+ Real *real;
169
+ } BDVALUE;
170
+
171
+ typedef struct {
172
+ VALUE bigdecimal_or_nil;
173
+ Real *real_or_null;
174
+ } NULLABLE_BDVALUE;
175
+
265
176
  /*
266
177
  * ------------------
267
178
  * EXPORTables.
268
179
  * ------------------
269
180
  */
270
181
 
271
- VP_EXPORT Real *
272
- VpNewRbClass(size_t mx, char const *str, VALUE klass);
273
-
274
- VP_EXPORT Real *VpCreateRbObject(size_t mx,const char *str);
275
-
276
- static inline BDIGIT
277
- rmpd_base_value(void) { return RMPD_BASE; }
278
- static inline size_t
279
- rmpd_component_figures(void) { return RMPD_COMPONENT_FIGURES; }
280
- static inline size_t
281
- rmpd_double_figures(void) { return 1+DBL_DIG; }
282
-
283
- #define VpBaseFig() rmpd_component_figures()
284
- #define VpDblFig() rmpd_double_figures()
285
- #define VpBaseVal() rmpd_base_value()
182
+ #define VpBaseFig() BIGDECIMAL_COMPONENT_FIGURES
286
183
 
287
184
  /* Zero,Inf,NaN (isinf(),isnan() used to check) */
288
185
  VP_EXPORT double VpGetDoubleNaN(void);
@@ -292,7 +189,7 @@ VP_EXPORT double VpGetDoubleNegZero(void);
292
189
 
293
190
  /* These 2 functions added at v1.1.7 */
294
191
  VP_EXPORT size_t VpGetPrecLimit(void);
295
- VP_EXPORT size_t VpSetPrecLimit(size_t n);
192
+ VP_EXPORT void VpSetPrecLimit(size_t n);
296
193
 
297
194
  /* Round mode */
298
195
  VP_EXPORT int VpIsRoundMode(unsigned short n);
@@ -300,41 +197,51 @@ VP_EXPORT unsigned short VpGetRoundMode(void);
300
197
  VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
301
198
 
302
199
  VP_EXPORT int VpException(unsigned short f,const char *str,int always);
303
- #if 0 /* unused */
304
- VP_EXPORT int VpIsNegDoubleZero(double v);
305
- #endif
306
200
  VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
307
- VP_EXPORT size_t VpInit(BDIGIT BaseVal);
308
- VP_EXPORT void *VpMemAlloc(size_t mb);
309
- VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
310
- VP_EXPORT void VpFree(Real *pv);
311
- VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal, int strict_p, int exc);
201
+ VP_EXPORT size_t VpInit(DECDIG BaseVal);
202
+ VP_EXPORT NULLABLE_BDVALUE VpAlloc(const char *szVal, int strict_p, int exc);
312
203
  VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
313
204
  VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
314
205
  VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
315
206
  VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
207
+ VP_EXPORT int VpNmlz(Real *a);
316
208
  VP_EXPORT int VpComp(Real *a,Real *b);
317
209
  VP_EXPORT ssize_t VpExponent10(Real *a);
318
- VP_EXPORT void VpSzMantissa(Real *a,char *psz);
319
- VP_EXPORT int VpToSpecialString(Real *a,char *psz,int fPlus);
320
- VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus);
321
- VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus);
210
+ VP_EXPORT void VpSzMantissa(Real *a, char *buf, size_t bufsize);
211
+ VP_EXPORT int VpToSpecialString(Real *a, char *buf, size_t bufsize, int fPlus);
212
+ VP_EXPORT void VpToString(Real *a, char *buf, size_t bufsize, size_t fFmt, int fPlus);
213
+ VP_EXPORT void VpToFString(Real *a, char *buf, size_t bufsize, size_t fFmt, int fPlus);
322
214
  VP_EXPORT int VpCtoV(Real *a, const char *int_chr, size_t ni, const char *frac, size_t nf, const char *exp_chr, size_t ne);
323
215
  VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
324
- VP_EXPORT void VpDtoV(Real *m,double d);
325
- #if 0 /* unused */
326
- VP_EXPORT void VpItoV(Real *m,S_INT ival);
327
- #endif
328
- VP_EXPORT int VpSqrt(Real *y,Real *x);
329
216
  VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
330
217
  VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
331
218
  VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
332
219
  VP_EXPORT void VpFrac(Real *y, Real *x);
333
- VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n);
220
+ VP_EXPORT int AddExponent(Real *a, SIGNED_VALUE n);
334
221
 
335
222
  /* VP constants */
336
223
  VP_EXPORT Real *VpOne(void);
337
224
 
225
+ /*
226
+ * **** BigDecimal part ****
227
+ */
228
+ VP_EXPORT VALUE BigDecimal_lt(VALUE self, VALUE r);
229
+ VP_EXPORT VALUE BigDecimal_ge(VALUE self, VALUE r);
230
+ VP_EXPORT VALUE BigDecimal_exponent(VALUE self);
231
+ VP_EXPORT VALUE BigDecimal_fix(VALUE self);
232
+ VP_EXPORT VALUE BigDecimal_frac(VALUE self);
233
+ VP_EXPORT VALUE BigDecimal_add(VALUE self, VALUE b);
234
+ VP_EXPORT VALUE BigDecimal_sub(VALUE self, VALUE b);
235
+ VP_EXPORT VALUE BigDecimal_mult(VALUE self, VALUE b);
236
+ VP_EXPORT VALUE BigDecimal_add2(VALUE self, VALUE b, VALUE n);
237
+ VP_EXPORT VALUE BigDecimal_sub2(VALUE self, VALUE b, VALUE n);
238
+ VP_EXPORT VALUE BigDecimal_mult2(VALUE self, VALUE b, VALUE n);
239
+ VP_EXPORT VALUE BigDecimal_split(VALUE self);
240
+ VP_EXPORT VALUE BigDecimal_decimal_shift(VALUE self, VALUE v);
241
+ VP_EXPORT inline BDVALUE GetBDValueMust(VALUE v);
242
+ VP_EXPORT inline BDVALUE rbd_allocate_struct_zero_wrap(int sign, size_t const digits);
243
+ #define NewZeroWrap rbd_allocate_struct_zero_wrap
244
+
338
245
  /*
339
246
  * ------------------
340
247
  * MACRO definitions.
@@ -344,10 +251,6 @@ VP_EXPORT Real *VpOne(void);
344
251
  #define Max(a, b) (((a)>(b))?(a):(b))
345
252
  #define Min(a, b) (((a)>(b))?(b):(a))
346
253
 
347
- #define VpMaxPrec(a) ((a)->MaxPrec)
348
- #define VpPrec(a) ((a)->Prec)
349
- #define VpGetFlag(a) ((a)->flag)
350
-
351
254
  /* Sign */
352
255
 
353
256
  /* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
@@ -364,25 +267,24 @@ VP_EXPORT Real *VpOne(void);
364
267
  #define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
365
268
  #define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
366
269
  #define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
367
- #define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
368
- #define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
270
+ #define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_POSITIVE_ZERO)
271
+ #define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
369
272
  #define VpSetZero(a,s) (void)(((s)>0)?VpSetPosZero(a):VpSetNegZero(a))
370
273
 
371
274
  /* NaN */
372
275
  #define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
373
- #define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
276
+ #define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_NaN)
374
277
 
375
278
  /* Infinity */
376
279
  #define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
377
280
  #define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
378
281
  #define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
379
282
  #define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
380
- #define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
381
- #define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
283
+ #define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
284
+ #define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
382
285
  #define VpSetInf(a,s) (void)(((s)>0)?VpSetPosInf(a):VpSetNegInf(a))
383
286
  #define VpHasVal(a) (a->frac[0])
384
287
  #define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
385
- #define VpExponent(a) (a->exponent)
386
288
  #ifdef BIGDECIMAL_DEBUG
387
289
  int VpVarCheck(Real * v);
388
290
  #endif /* BIGDECIMAL_DEBUG */
@@ -0,0 +1,144 @@
1
+ #ifndef BIGDECIMAL_BITS_H
2
+ #define BIGDECIMAL_BITS_H
3
+
4
+ #include "feature.h"
5
+ #include "static_assert.h"
6
+
7
+ #if defined(__x86_64__) && defined(HAVE_X86INTRIN_H)
8
+ # include <x86intrin.h> /* for _lzcnt_u64, etc. */
9
+ #elif defined(_MSC_VER) && defined(HAVE_INTRIN_H)
10
+ # include <intrin.h> /* for the following intrinsics */
11
+ #endif
12
+
13
+ #if defined(_MSC_VER) && defined(__AVX2__)
14
+ # pragma intrinsic(__lzcnt)
15
+ # pragma intrinsic(__lzcnt64)
16
+ #endif
17
+
18
+ #define numberof(array) ((int)(sizeof(array) / sizeof((array)[0])))
19
+ #define roomof(x, y) (((x) + (y) - 1) / (y))
20
+ #define type_roomof(x, y) roomof(sizeof(x), sizeof(y))
21
+
22
+ #define MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
23
+ (a) == 0 ? 0 : \
24
+ (a) == -1 ? (b) < -(max) : \
25
+ (a) > 0 ? \
26
+ ((b) > 0 ? (max) / (a) < (b) : (min) / (a) > (b)) : \
27
+ ((b) > 0 ? (min) / (a) < (b) : (max) / (a) > (b)))
28
+
29
+ #define ADD_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
30
+ ((a) > 0) == ((b) > 0) && ((a) > 0 ? (max) - (a) < (b) : (min) - (a) > (b)))
31
+
32
+ #ifdef HAVE_UINT128_T
33
+ # define bit_length(x) \
34
+ (unsigned int) \
35
+ (sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \
36
+ sizeof(x) <= sizeof(int64_t) ? 64 - nlz_int64((uint64_t)(x)) : \
37
+ 128 - nlz_int128((uint128_t)(x)))
38
+ #else
39
+ # define bit_length(x) \
40
+ (unsigned int) \
41
+ (sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \
42
+ 64 - nlz_int64((uint64_t)(x)))
43
+ #endif
44
+
45
+ static inline unsigned nlz_int32(uint32_t x);
46
+ static inline unsigned nlz_int64(uint64_t x);
47
+ #ifdef HAVE_UINT128_T
48
+ static inline unsigned nlz_int128(uint128_t x);
49
+ #endif
50
+
51
+ static inline unsigned int
52
+ nlz_int32(uint32_t x)
53
+ {
54
+ #if defined(_MSC_VER) && defined(__AVX2__) && defined(HAVE___LZCNT)
55
+ /* Note: It seems there is no such thing like __LZCNT__ predefined in MSVC.
56
+ * AMD CPUs have had this instruction for decades (since K10) but for
57
+ * Intel, Haswell is the oldest one. We need to use __AVX2__ for maximum
58
+ * safety. */
59
+ return (unsigned int)__lzcnt(x);
60
+
61
+ #elif defined(__x86_64__) && defined(__LZCNT__) && defined(HAVE__LZCNT_U32)
62
+ return (unsigned int)_lzcnt_u32(x);
63
+
64
+ #elif defined(_MSC_VER) && defined(HAVE__BITSCANREVERSE)
65
+ unsigned long r;
66
+ return _BitScanReverse(&r, x) ? (31 - (int)r) : 32;
67
+
68
+ #elif __has_builtin(__builtin_clz)
69
+ STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT == 32);
70
+ return x ? (unsigned int)__builtin_clz(x) : 32;
71
+
72
+ #else
73
+ uint32_t y;
74
+ unsigned n = 32;
75
+ y = x >> 16; if (y) {n -= 16; x = y;}
76
+ y = x >> 8; if (y) {n -= 8; x = y;}
77
+ y = x >> 4; if (y) {n -= 4; x = y;}
78
+ y = x >> 2; if (y) {n -= 2; x = y;}
79
+ y = x >> 1; if (y) {return n - 2;}
80
+ return (unsigned int)(n - x);
81
+ #endif
82
+ }
83
+
84
+ static inline unsigned int
85
+ nlz_int64(uint64_t x)
86
+ {
87
+ #if defined(_MSC_VER) && defined(__AVX2__) && defined(HAVE___LZCNT64)
88
+ return (unsigned int)__lzcnt64(x);
89
+
90
+ #elif defined(__x86_64__) && defined(__LZCNT__) && defined(HAVE__LZCNT_U64)
91
+ return (unsigned int)_lzcnt_u64(x);
92
+
93
+ #elif defined(_WIN64) && defined(_MSC_VER) && defined(HAVE__BITSCANREVERSE64)
94
+ unsigned long r;
95
+ return _BitScanReverse64(&r, x) ? (63u - (unsigned int)r) : 64;
96
+
97
+ #elif __has_builtin(__builtin_clzl) && __has_builtin(__builtin_clzll) && !(defined(__sun) && defined(__sparc))
98
+ if (x == 0) {
99
+ return 64;
100
+ }
101
+ else if (sizeof(long) * CHAR_BIT == 64) {
102
+ return (unsigned int)__builtin_clzl((unsigned long)x);
103
+ }
104
+ else if (sizeof(long long) * CHAR_BIT == 64) {
105
+ return (unsigned int)__builtin_clzll((unsigned long long)x);
106
+ }
107
+ else {
108
+ /* :FIXME: Is there a way to make this branch a compile-time error? */
109
+ __builtin_unreachable();
110
+ }
111
+
112
+ #else
113
+ uint64_t y;
114
+ unsigned int n = 64;
115
+ y = x >> 32; if (y) {n -= 32; x = y;}
116
+ y = x >> 16; if (y) {n -= 16; x = y;}
117
+ y = x >> 8; if (y) {n -= 8; x = y;}
118
+ y = x >> 4; if (y) {n -= 4; x = y;}
119
+ y = x >> 2; if (y) {n -= 2; x = y;}
120
+ y = x >> 1; if (y) {return n - 2;}
121
+ return (unsigned int)(n - x);
122
+
123
+ #endif
124
+ }
125
+
126
+ #ifdef HAVE_UINT128_T
127
+ static inline unsigned int
128
+ nlz_int128(uint128_t x)
129
+ {
130
+ uint64_t y = (uint64_t)(x >> 64);
131
+
132
+ if (x == 0) {
133
+ return 128;
134
+ }
135
+ else if (y == 0) {
136
+ return (unsigned int)nlz_int64(x) + 64;
137
+ }
138
+ else {
139
+ return (unsigned int)nlz_int64(y);
140
+ }
141
+ }
142
+ #endif
143
+
144
+ #endif /* BIGDECIMAL_BITS_H */