Skip to content

Commit 2703410

Browse files
mrknmatzbot
authored andcommitted
[ruby/bigdecimal] Twak GetPrecisionInt and rename it to check_int_precision
ruby/bigdecimal@69d0588a3b
1 parent ef1c610 commit 2703410

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,17 @@ GetAddSubPrec(Real *a, Real *b)
928928
return mx;
929929
}
930930

931-
static SIGNED_VALUE
932-
GetPrecisionInt(VALUE v)
931+
static inline SIGNED_VALUE
932+
check_int_precision(VALUE v)
933933
{
934934
SIGNED_VALUE n;
935-
n = NUM2INT(v);
935+
#if SIZEOF_VALUE <= SIZEOF_LONG
936+
n = (SIGNED_VALUE)NUM2LONG(v);
937+
#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
938+
n = (SIGNED_VALUE)NUM2LL(v);
939+
#else
940+
# error SIZEOF_VALUE is too large
941+
#endif
936942
if (n < 0) {
937943
rb_raise(rb_eArgError, "negative precision");
938944
}
@@ -1716,7 +1722,7 @@ BigDecimal_quo(int argc, VALUE *argv, VALUE self)
17161722

17171723
argc = rb_scan_args(argc, argv, "11", &value, &digits);
17181724
if (argc > 1) {
1719-
n = GetPrecisionInt(digits);
1725+
n = check_int_precision(digits);
17201726
}
17211727

17221728
if (n > 0) {
@@ -1981,7 +1987,7 @@ BigDecimal_div2(VALUE self, VALUE b, VALUE n)
19811987
}
19821988

19831989
/* div in BigDecimal sense */
1984-
ix = GetPrecisionInt(n);
1990+
ix = check_int_precision(n);
19851991
if (ix == 0) {
19861992
return BigDecimal_div(self, b);
19871993
}
@@ -2086,7 +2092,7 @@ BigDecimal_add2(VALUE self, VALUE b, VALUE n)
20862092
{
20872093
ENTER(2);
20882094
Real *cv;
2089-
SIGNED_VALUE mx = GetPrecisionInt(n);
2095+
SIGNED_VALUE mx = check_int_precision(n);
20902096
if (mx == 0) return BigDecimal_add(self, b);
20912097
else {
20922098
size_t pl = VpSetPrecLimit(0);
@@ -2116,7 +2122,7 @@ BigDecimal_sub2(VALUE self, VALUE b, VALUE n)
21162122
{
21172123
ENTER(2);
21182124
Real *cv;
2119-
SIGNED_VALUE mx = GetPrecisionInt(n);
2125+
SIGNED_VALUE mx = check_int_precision(n);
21202126
if (mx == 0) return BigDecimal_sub(self, b);
21212127
else {
21222128
size_t pl = VpSetPrecLimit(0);
@@ -2159,7 +2165,7 @@ BigDecimal_mult2(VALUE self, VALUE b, VALUE n)
21592165
{
21602166
ENTER(2);
21612167
Real *cv;
2162-
SIGNED_VALUE mx = GetPrecisionInt(n);
2168+
SIGNED_VALUE mx = check_int_precision(n);
21632169
if (mx == 0) return BigDecimal_mult(self, b);
21642170
else {
21652171
size_t pl = VpSetPrecLimit(0);
@@ -2214,7 +2220,8 @@ BigDecimal_sqrt(VALUE self, VALUE nFig)
22142220
GUARD_OBJ(a, GetVpValue(self, 1));
22152221
mx = a->Prec * (VpBaseFig() + 1);
22162222

2217-
n = GetPrecisionInt(nFig) + VpDblFig() + BASE_FIG;
2223+
n = check_int_precision(nFig);
2224+
n += VpDblFig() + VpBaseFig();
22182225
if (mx <= n) mx = n;
22192226
GUARD_OBJ(c, VpCreateRbObject(mx, "0", true));
22202227
VpSqrt(c, a);

0 commit comments

Comments
 (0)