ed-precompiled_bigdecimal 3.3.1-x86_64-linux
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.
- checksums.yaml +7 -0
- data/LICENSE +56 -0
- data/bigdecimal.gemspec +57 -0
- data/ext/bigdecimal/bigdecimal.c +6238 -0
- data/ext/bigdecimal/bigdecimal.h +292 -0
- data/ext/bigdecimal/bits.h +144 -0
- data/ext/bigdecimal/extconf.rb +60 -0
- data/ext/bigdecimal/feature.h +68 -0
- data/ext/bigdecimal/missing/dtoa.c +3462 -0
- data/ext/bigdecimal/missing.c +28 -0
- data/ext/bigdecimal/missing.h +104 -0
- data/ext/bigdecimal/static_assert.h +54 -0
- data/lib/3.0/bigdecimal.so +0 -0
- data/lib/3.1/bigdecimal.so +0 -0
- data/lib/3.2/bigdecimal.so +0 -0
- data/lib/3.3/bigdecimal.so +0 -0
- data/lib/3.4/bigdecimal.so +0 -0
- data/lib/bigdecimal/jacobian.rb +90 -0
- data/lib/bigdecimal/ludcmp.rb +89 -0
- data/lib/bigdecimal/math.rb +249 -0
- data/lib/bigdecimal/newton.rb +80 -0
- data/lib/bigdecimal/util.rb +186 -0
- data/lib/bigdecimal.rb +361 -0
- data/lib/bigdecimal.so +0 -0
- data/sample/linear.rb +74 -0
- data/sample/nlsolve.rb +40 -0
- data/sample/pi.rb +21 -0
- metadata +74 -0
@@ -0,0 +1,292 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Ruby BigDecimal(Variable decimal precision) extension library.
|
4
|
+
*
|
5
|
+
* Copyright(C) 2002 by Shigeo Kobayashi(shigeo@tinyforest.gr.jp)
|
6
|
+
*
|
7
|
+
*/
|
8
|
+
|
9
|
+
#ifndef RUBY_BIG_DECIMAL_H
|
10
|
+
#define RUBY_BIG_DECIMAL_H 1
|
11
|
+
|
12
|
+
#define RUBY_NO_OLD_COMPATIBILITY
|
13
|
+
#include "ruby/ruby.h"
|
14
|
+
#include "missing.h"
|
15
|
+
|
16
|
+
#ifdef HAVE_FLOAT_H
|
17
|
+
# include <float.h>
|
18
|
+
#endif
|
19
|
+
|
20
|
+
#if defined(HAVE_INT64_T) && !defined(BIGDECIMAL_USE_DECDIG_UINT16_T)
|
21
|
+
# define DECDIG uint32_t
|
22
|
+
# define DECDIG_DBL uint64_t
|
23
|
+
# define DECDIG_DBL_SIGNED int64_t
|
24
|
+
# define SIZEOF_DECDIG 4
|
25
|
+
# define PRI_DECDIG_PREFIX ""
|
26
|
+
# ifdef PRI_LL_PREFIX
|
27
|
+
# define PRI_DECDIG_DBL_PREFIX PRI_LL_PREFIX
|
28
|
+
# else
|
29
|
+
# define PRI_DECDIG_DBL_PREFIX "l"
|
30
|
+
# endif
|
31
|
+
#else
|
32
|
+
# define DECDIG uint16_t
|
33
|
+
# define DECDIG_DBL uint32_t
|
34
|
+
# define DECDIG_DBL_SIGNED int32_t
|
35
|
+
# define SIZEOF_DECDIG 2
|
36
|
+
# define PRI_DECDIG_PREFIX "h"
|
37
|
+
# define PRI_DECDIG_DBL_PREFIX ""
|
38
|
+
#endif
|
39
|
+
|
40
|
+
#define PRIdDECDIG PRI_DECDIG_PREFIX"d"
|
41
|
+
#define PRIiDECDIG PRI_DECDIG_PREFIX"i"
|
42
|
+
#define PRIoDECDIG PRI_DECDIG_PREFIX"o"
|
43
|
+
#define PRIuDECDIG PRI_DECDIG_PREFIX"u"
|
44
|
+
#define PRIxDECDIG PRI_DECDIG_PREFIX"x"
|
45
|
+
#define PRIXDECDIG PRI_DECDIG_PREFIX"X"
|
46
|
+
|
47
|
+
#define PRIdDECDIG_DBL PRI_DECDIG_DBL_PREFIX"d"
|
48
|
+
#define PRIiDECDIG_DBL PRI_DECDIG_DBL_PREFIX"i"
|
49
|
+
#define PRIoDECDIG_DBL PRI_DECDIG_DBL_PREFIX"o"
|
50
|
+
#define PRIuDECDIG_DBL PRI_DECDIG_DBL_PREFIX"u"
|
51
|
+
#define PRIxDECDIG_DBL PRI_DECDIG_DBL_PREFIX"x"
|
52
|
+
#define PRIXDECDIG_DBL PRI_DECDIG_DBL_PREFIX"X"
|
53
|
+
|
54
|
+
#if SIZEOF_DECDIG == 4
|
55
|
+
# define BIGDECIMAL_BASE ((DECDIG)1000000000U)
|
56
|
+
# define BIGDECIMAL_COMPONENT_FIGURES 9
|
57
|
+
/*
|
58
|
+
* The number of components required for a 64-bit integer.
|
59
|
+
*
|
60
|
+
* INT64_MAX: 9_223372036_854775807
|
61
|
+
* UINT64_MAX: 18_446744073_709551615
|
62
|
+
*/
|
63
|
+
# define BIGDECIMAL_INT64_MAX_LENGTH 3
|
64
|
+
|
65
|
+
#elif SIZEOF_DECDIG == 2
|
66
|
+
# define BIGDECIMAL_BASE ((DECDIG)10000U)
|
67
|
+
# define BIGDECIMAL_COMPONENT_FIGURES 4
|
68
|
+
/*
|
69
|
+
* The number of components required for a 64-bit integer.
|
70
|
+
*
|
71
|
+
* INT64_MAX: 922_3372_0368_5477_5807
|
72
|
+
* UINT64_MAX: 1844_6744_0737_0955_1615
|
73
|
+
*/
|
74
|
+
# define BIGDECIMAL_INT64_MAX_LENGTH 5
|
75
|
+
|
76
|
+
#else
|
77
|
+
# error Unknown size of DECDIG
|
78
|
+
#endif
|
79
|
+
|
80
|
+
#define BIGDECIMAL_DOUBLE_FIGURES (1+DBL_DIG)
|
81
|
+
|
82
|
+
#if defined(__cplusplus)
|
83
|
+
extern "C" {
|
84
|
+
#if 0
|
85
|
+
} /* satisfy cc-mode */
|
86
|
+
#endif
|
87
|
+
#endif
|
88
|
+
|
89
|
+
extern VALUE rb_cBigDecimal;
|
90
|
+
|
91
|
+
/*
|
92
|
+
* NaN & Infinity
|
93
|
+
*/
|
94
|
+
#define SZ_NaN "NaN"
|
95
|
+
#define SZ_INF "Infinity"
|
96
|
+
#define SZ_PINF "+Infinity"
|
97
|
+
#define SZ_NINF "-Infinity"
|
98
|
+
|
99
|
+
/*
|
100
|
+
* #define VP_EXPORT other than static to let VP_ routines
|
101
|
+
* be called from outside of this module.
|
102
|
+
*/
|
103
|
+
#define VP_EXPORT static
|
104
|
+
|
105
|
+
/* Exception mode */
|
106
|
+
#define VP_EXCEPTION_ALL ((unsigned short)0x00FF)
|
107
|
+
#define VP_EXCEPTION_INFINITY ((unsigned short)0x0001)
|
108
|
+
#define VP_EXCEPTION_NaN ((unsigned short)0x0002)
|
109
|
+
#define VP_EXCEPTION_UNDERFLOW ((unsigned short)0x0004)
|
110
|
+
#define VP_EXCEPTION_OVERFLOW ((unsigned short)0x0001) /* 0x0008) */
|
111
|
+
#define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0010)
|
112
|
+
|
113
|
+
/* Following 2 exceptions can't controlled by user */
|
114
|
+
#define VP_EXCEPTION_OP ((unsigned short)0x0020)
|
115
|
+
|
116
|
+
#define BIGDECIMAL_EXCEPTION_MODE_DEFAULT 0U
|
117
|
+
|
118
|
+
/* This is used in BigDecimal#mode */
|
119
|
+
#define VP_ROUND_MODE ((unsigned short)0x0100)
|
120
|
+
|
121
|
+
/* Rounding mode */
|
122
|
+
#define VP_ROUND_UP RBD_ROUND_UP
|
123
|
+
#define VP_ROUND_DOWN RBD_ROUND_DOWN
|
124
|
+
#define VP_ROUND_HALF_UP RBD_ROUND_HALF_UP
|
125
|
+
#define VP_ROUND_HALF_DOWN RBD_ROUND_HALF_DOWN
|
126
|
+
#define VP_ROUND_CEIL RBD_ROUND_CEIL
|
127
|
+
#define VP_ROUND_FLOOR RBD_ROUND_FLOOR
|
128
|
+
#define VP_ROUND_HALF_EVEN RBD_ROUND_HALF_EVEN
|
129
|
+
|
130
|
+
enum rbd_rounding_mode {
|
131
|
+
RBD_ROUND_UP = 1,
|
132
|
+
RBD_ROUND_DOWN = 2,
|
133
|
+
RBD_ROUND_HALF_UP = 3,
|
134
|
+
RBD_ROUND_HALF_DOWN = 4,
|
135
|
+
RBD_ROUND_CEIL = 5,
|
136
|
+
RBD_ROUND_FLOOR = 6,
|
137
|
+
RBD_ROUND_HALF_EVEN = 7,
|
138
|
+
|
139
|
+
RBD_ROUND_DEFAULT = RBD_ROUND_HALF_UP,
|
140
|
+
RBD_ROUND_TRUNCATE = RBD_ROUND_DOWN,
|
141
|
+
RBD_ROUND_BANKER = RBD_ROUND_HALF_EVEN,
|
142
|
+
RBD_ROUND_CEILING = RBD_ROUND_CEIL
|
143
|
+
};
|
144
|
+
|
145
|
+
#define BIGDECIMAL_ROUNDING_MODE_DEFAULT VP_ROUND_HALF_UP
|
146
|
+
|
147
|
+
/* Sign flag */
|
148
|
+
#define VP_SIGN_NaN 0 /* NaN */
|
149
|
+
#define VP_SIGN_POSITIVE_ZERO 1 /* Positive zero */
|
150
|
+
#define VP_SIGN_NEGATIVE_ZERO -1 /* Negative zero */
|
151
|
+
#define VP_SIGN_POSITIVE_FINITE 2 /* Positive finite number */
|
152
|
+
#define VP_SIGN_NEGATIVE_FINITE -2 /* Negative finite number */
|
153
|
+
#define VP_SIGN_POSITIVE_INFINITE 3 /* Positive infinite number */
|
154
|
+
#define VP_SIGN_NEGATIVE_INFINITE -3 /* Negative infinite number */
|
155
|
+
|
156
|
+
/* The size of fraction part array */
|
157
|
+
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
158
|
+
#define FLEXIBLE_ARRAY_SIZE /* */
|
159
|
+
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
160
|
+
#define FLEXIBLE_ARRAY_SIZE 0
|
161
|
+
#else
|
162
|
+
#define FLEXIBLE_ARRAY_SIZE 1
|
163
|
+
#endif
|
164
|
+
|
165
|
+
/*
|
166
|
+
* VP representation
|
167
|
+
* r = 0.xxxxxxxxx *BASE**exponent
|
168
|
+
*/
|
169
|
+
typedef struct {
|
170
|
+
size_t MaxPrec; /* Maximum precision size */
|
171
|
+
/* This is the actual size of frac[] */
|
172
|
+
/*(frac[0] to frac[MaxPrec] are available). */
|
173
|
+
size_t Prec; /* Current precision size. */
|
174
|
+
/* This indicates how much the */
|
175
|
+
/* array frac[] is actually used. */
|
176
|
+
SIGNED_VALUE exponent; /* Exponent part. */
|
177
|
+
short sign; /* Attributes of the value. */
|
178
|
+
/*
|
179
|
+
* ==0 : NaN
|
180
|
+
* 1 : Positive zero
|
181
|
+
* -1 : Negative zero
|
182
|
+
* 2 : Positive number
|
183
|
+
* -2 : Negative number
|
184
|
+
* 3 : Positive infinite number
|
185
|
+
* -3 : Negative infinite number
|
186
|
+
*/
|
187
|
+
short flag; /* Not used in vp_routines,space for user. */
|
188
|
+
DECDIG frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
|
189
|
+
} Real;
|
190
|
+
|
191
|
+
/*
|
192
|
+
* ------------------
|
193
|
+
* EXPORTables.
|
194
|
+
* ------------------
|
195
|
+
*/
|
196
|
+
|
197
|
+
#define VpBaseFig() BIGDECIMAL_COMPONENT_FIGURES
|
198
|
+
|
199
|
+
/* Zero,Inf,NaN (isinf(),isnan() used to check) */
|
200
|
+
VP_EXPORT double VpGetDoubleNaN(void);
|
201
|
+
VP_EXPORT double VpGetDoublePosInf(void);
|
202
|
+
VP_EXPORT double VpGetDoubleNegInf(void);
|
203
|
+
VP_EXPORT double VpGetDoubleNegZero(void);
|
204
|
+
|
205
|
+
/* These 2 functions added at v1.1.7 */
|
206
|
+
VP_EXPORT size_t VpGetPrecLimit(void);
|
207
|
+
VP_EXPORT void VpSetPrecLimit(size_t n);
|
208
|
+
|
209
|
+
/* Round mode */
|
210
|
+
VP_EXPORT int VpIsRoundMode(unsigned short n);
|
211
|
+
VP_EXPORT unsigned short VpGetRoundMode(void);
|
212
|
+
VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
|
213
|
+
|
214
|
+
VP_EXPORT int VpException(unsigned short f,const char *str,int always);
|
215
|
+
VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
|
216
|
+
VP_EXPORT size_t VpInit(DECDIG BaseVal);
|
217
|
+
VP_EXPORT Real *VpAlloc(const char *szVal, int strict_p, int exc);
|
218
|
+
VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
|
219
|
+
VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
|
220
|
+
VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
|
221
|
+
VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
|
222
|
+
VP_EXPORT int VpNmlz(Real *a);
|
223
|
+
VP_EXPORT int VpComp(Real *a,Real *b);
|
224
|
+
VP_EXPORT ssize_t VpExponent10(Real *a);
|
225
|
+
VP_EXPORT void VpSzMantissa(Real *a, char *buf, size_t bufsize);
|
226
|
+
VP_EXPORT int VpToSpecialString(Real *a, char *buf, size_t bufsize, int fPlus);
|
227
|
+
VP_EXPORT void VpToString(Real *a, char *buf, size_t bufsize, size_t fFmt, int fPlus);
|
228
|
+
VP_EXPORT void VpToFString(Real *a, char *buf, size_t bufsize, size_t fFmt, int fPlus);
|
229
|
+
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);
|
230
|
+
VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
|
231
|
+
VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
|
232
|
+
VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
|
233
|
+
VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
|
234
|
+
VP_EXPORT void VpFrac(Real *y, Real *x);
|
235
|
+
|
236
|
+
/* VP constants */
|
237
|
+
VP_EXPORT Real *VpOne(void);
|
238
|
+
|
239
|
+
/*
|
240
|
+
* ------------------
|
241
|
+
* MACRO definitions.
|
242
|
+
* ------------------
|
243
|
+
*/
|
244
|
+
#define Abs(a) (((a)>= 0)?(a):(-(a)))
|
245
|
+
#define Max(a, b) (((a)>(b))?(a):(b))
|
246
|
+
#define Min(a, b) (((a)>(b))?(b):(a))
|
247
|
+
|
248
|
+
/* Sign */
|
249
|
+
|
250
|
+
/* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
|
251
|
+
#define VpGetSign(a) (((a)->sign>0)?1:(-1))
|
252
|
+
/* Change sign of a to a>0,a<0 if s = 1,-1 respectively */
|
253
|
+
#define VpChangeSign(a,s) {if((s)>0) (a)->sign=(short)Abs((ssize_t)(a)->sign);else (a)->sign=-(short)Abs((ssize_t)(a)->sign);}
|
254
|
+
/* Sets sign of a to a>0,a<0 if s = 1,-1 respectively */
|
255
|
+
#define VpSetSign(a,s) {if((s)>0) (a)->sign=(short)VP_SIGN_POSITIVE_FINITE;else (a)->sign=(short)VP_SIGN_NEGATIVE_FINITE;}
|
256
|
+
|
257
|
+
/* 1 */
|
258
|
+
#define VpSetOne(a) {(a)->Prec=(a)->exponent=(a)->frac[0]=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
|
259
|
+
|
260
|
+
/* ZEROs */
|
261
|
+
#define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
|
262
|
+
#define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
|
263
|
+
#define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
|
264
|
+
#define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
|
265
|
+
#define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
|
266
|
+
#define VpSetZero(a,s) (void)(((s)>0)?VpSetPosZero(a):VpSetNegZero(a))
|
267
|
+
|
268
|
+
/* NaN */
|
269
|
+
#define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
|
270
|
+
#define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
|
271
|
+
|
272
|
+
/* Infinity */
|
273
|
+
#define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
|
274
|
+
#define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
|
275
|
+
#define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
|
276
|
+
#define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
|
277
|
+
#define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
|
278
|
+
#define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
|
279
|
+
#define VpSetInf(a,s) (void)(((s)>0)?VpSetPosInf(a):VpSetNegInf(a))
|
280
|
+
#define VpHasVal(a) (a->frac[0])
|
281
|
+
#define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
|
282
|
+
#ifdef BIGDECIMAL_DEBUG
|
283
|
+
int VpVarCheck(Real * v);
|
284
|
+
#endif /* BIGDECIMAL_DEBUG */
|
285
|
+
|
286
|
+
#if defined(__cplusplus)
|
287
|
+
#if 0
|
288
|
+
{ /* satisfy cc-mode */
|
289
|
+
#endif
|
290
|
+
} /* extern "C" { */
|
291
|
+
#endif
|
292
|
+
#endif /* RUBY_BIG_DECIMAL_H */
|
@@ -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 */
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'mkmf'
|
3
|
+
|
4
|
+
def have_builtin_func(name, check_expr, opt = "", &b)
|
5
|
+
checking_for checking_message(name.funcall_style, nil, opt) do
|
6
|
+
if try_compile(<<SRC, opt, &b)
|
7
|
+
int foo;
|
8
|
+
int main() { #{check_expr}; return 0; }
|
9
|
+
SRC
|
10
|
+
$defs.push(format("-DHAVE_BUILTIN_%s", name.tr_cpp))
|
11
|
+
true
|
12
|
+
else
|
13
|
+
false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
have_builtin_func("__builtin_clz", "__builtin_clz(0)")
|
19
|
+
have_builtin_func("__builtin_clzl", "__builtin_clzl(0)")
|
20
|
+
have_builtin_func("__builtin_clzll", "__builtin_clzll(0)")
|
21
|
+
|
22
|
+
have_header("float.h")
|
23
|
+
have_header("math.h")
|
24
|
+
have_header("stdbool.h")
|
25
|
+
have_header("stdlib.h")
|
26
|
+
|
27
|
+
if have_header("x86intrin.h")
|
28
|
+
have_func("_lzcnt_u32", "x86intrin.h")
|
29
|
+
have_func("_lzcnt_u64", "x86intrin.h")
|
30
|
+
end
|
31
|
+
|
32
|
+
if have_header("intrin.h")
|
33
|
+
have_func("__lzcnt", "intrin.h")
|
34
|
+
have_func("__lzcnt64", "intrin.h")
|
35
|
+
have_func("_BitScanReverse", "intrin.h")
|
36
|
+
have_func("_BitScanReverse64", "intrin.h")
|
37
|
+
end
|
38
|
+
|
39
|
+
have_header("ruby/atomic.h")
|
40
|
+
have_header("ruby/internal/has/builtin.h")
|
41
|
+
have_header("ruby/internal/static_assert.h")
|
42
|
+
|
43
|
+
have_func("rb_complex_real", "ruby.h")
|
44
|
+
have_func("rb_complex_imag", "ruby.h")
|
45
|
+
have_func("rb_opts_exception_p", "ruby.h")
|
46
|
+
have_func("rb_category_warn", "ruby.h")
|
47
|
+
have_const("RB_WARN_CATEGORY_DEPRECATED", "ruby.h")
|
48
|
+
|
49
|
+
if File.file?(File.expand_path('../lib/bigdecimal.rb', __FILE__))
|
50
|
+
bigdecimal_rb = "$(srcdir)/lib/bigdecimal.rb"
|
51
|
+
else
|
52
|
+
bigdecimal_rb = "$(srcdir)/../../lib/bigdecimal.rb"
|
53
|
+
end
|
54
|
+
|
55
|
+
$defs.push '-DBIGDECIMAL_USE_DECDIG_UINT16_T' if ENV['BIGDECIMAL_USE_DECDIG_UINT16_T'] == 'true'
|
56
|
+
$defs.push '-DBIGDECIMAL_USE_VP_TEST_METHODS' if ENV['BIGDECIMAL_USE_VP_TEST_METHODS'] == 'true'
|
57
|
+
|
58
|
+
create_makefile('bigdecimal') {|mf|
|
59
|
+
mf << "BIGDECIMAL_RB = #{bigdecimal_rb}\n"
|
60
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
#ifndef BIGDECIMAL_HAS_FEATURE_H
|
2
|
+
#define BIGDECIMAL_HAS_FEATURE_H
|
3
|
+
|
4
|
+
/* ======== __has_feature ======== */
|
5
|
+
|
6
|
+
#ifndef __has_feature
|
7
|
+
# define __has_feature(_) 0
|
8
|
+
#endif
|
9
|
+
|
10
|
+
/* ======== __has_extension ======== */
|
11
|
+
|
12
|
+
#ifndef __has_extension
|
13
|
+
# define __has_extension __has_feature
|
14
|
+
#endif
|
15
|
+
|
16
|
+
/* ======== __has_builtin ======== */
|
17
|
+
|
18
|
+
#ifdef HAVE_RUBY_INTERNAL_HAS_BUILTIN_H
|
19
|
+
# include <ruby/internal/has/builtin.h>
|
20
|
+
#endif
|
21
|
+
|
22
|
+
#ifdef RBIMPL_HAS_BUILTIN
|
23
|
+
# define BIGDECIMAL_HAS_BUILTIN(...) RBIMPL_HAS_BUILTIN(__VA_ARGS__)
|
24
|
+
|
25
|
+
#else
|
26
|
+
# /* The following section is copied from CRuby's builtin.h */
|
27
|
+
#
|
28
|
+
# ifdef __has_builtin
|
29
|
+
# if defined(__INTEL_COMPILER)
|
30
|
+
# /* :TODO: Intel C Compiler has __has_builtin (since 19.1 maybe?), and is
|
31
|
+
# * reportedly broken. We have to skip them. However the situation can
|
32
|
+
# * change. They might improve someday. We need to revisit here later. */
|
33
|
+
# elif defined(__GNUC__) && ! __has_builtin(__builtin_alloca)
|
34
|
+
# /* FreeBSD's <sys/cdefs.h> defines its own *broken* version of
|
35
|
+
# * __has_builtin. Cygwin copied that content to be a victim of the
|
36
|
+
# * broken-ness. We don't take them into account. */
|
37
|
+
# else
|
38
|
+
# define HAVE___HAS_BUILTIN 1
|
39
|
+
# endif
|
40
|
+
# endif
|
41
|
+
#
|
42
|
+
# if defined(HAVE___HAS_BUILTIN)
|
43
|
+
# define BIGDECIMAL_HAS_BUILTIN(_) __has_builtin(_)
|
44
|
+
#
|
45
|
+
# elif defined(__GNUC__)
|
46
|
+
# define BIGDECIMAL_HAS_BUILTIN(_) BIGDECIMAL_HAS_BUILTIN_ ## _
|
47
|
+
# if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 6))
|
48
|
+
# define BIGDECIMAL_HAS_BUILTIN___builtin_clz 1
|
49
|
+
# define BIGDECIMAL_HAS_BUILTIN___builtin_clzl 1
|
50
|
+
# else
|
51
|
+
# define BIGDECIMAL_HAS_BUILTIN___builtin_clz 0
|
52
|
+
# define BIGDECIMAL_HAS_BUILTIN___builtin_clzl 0
|
53
|
+
# endif
|
54
|
+
# elif defined(_MSC_VER)
|
55
|
+
# define BIGDECIMAL_HAS_BUILTIN(_) 0
|
56
|
+
#
|
57
|
+
# else
|
58
|
+
# define BIGDECIMAL_HAS_BUILTIN(_) BIGDECIMAL_HAS_BUILTIN_ ## _
|
59
|
+
# define BIGDECIMAL_HAS_BUILTIN___builtin_clz HAVE_BUILTIN___BUILTIN_CLZ
|
60
|
+
# define BIGDECIMAL_HAS_BUILTIN___builtin_clzl HAVE_BUILTIN___BUILTIN_CLZL
|
61
|
+
# endif
|
62
|
+
#endif /* RBIMPL_HAS_BUILTIN */
|
63
|
+
|
64
|
+
#ifndef __has_builtin
|
65
|
+
# define __has_builtin(...) BIGDECIMAL_HAS_BUILTIN(__VA_ARGS__)
|
66
|
+
#endif
|
67
|
+
|
68
|
+
#endif /* BIGDECIMAL_HAS_FEATURE_H */
|