ext/numo/narray/array.c in numo-narray-0.9.1.6 vs ext/numo/narray/array.c in numo-narray-0.9.1.7

- old
+ new

@@ -1,9 +1,9 @@ /* array.c Ruby/Numo::NArray - Numerical Array class for Ruby - Copyright (C) 1999-2019 Masahiro TANAKA + Copyright (C) 1999-2020 Masahiro TANAKA */ #include <ruby.h> #include "numo/narray.h" // mdai: Multi-Dimensional Array Investigation @@ -29,19 +29,16 @@ static ID id_step; static ID id_abs; static ID id_cast; static ID id_le; static ID id_Complex; +static VALUE int32_max = Qnil; +static VALUE int32_min = Qnil; - static VALUE na_object_type(int type, VALUE v) { - static VALUE int32_max = Qnil; - if (NIL_P(int32_max)) - int32_max = ULONG2NUM(2147483647); - switch(TYPE(v)) { case T_TRUE: case T_FALSE: if (type<NA_BIT) @@ -53,12 +50,12 @@ if (type<NA_INT32) return NA_INT32; return type; case T_BIGNUM: if (type<NA_INT64) { - v = rb_funcall(v,id_abs,0); - if (RTEST(rb_funcall(v,id_le,1,int32_max))) { + if (RTEST(rb_funcall(v,id_le,1,int32_max)) && + RTEST(rb_funcall(v,id_ge,1,int32_min))) { if (type<NA_INT32) return NA_INT32; } else { return NA_INT64; } @@ -67,12 +64,11 @@ #elif SIZEOF_LONG == 8 case T_FIXNUM: if (type<NA_INT64) { long x = NUM2LONG(v); - if (x<0) x=-x; - if (x<=2147483647) { + if (x<=2147483647 && x>=-2147483648) { if (type<NA_INT32) return NA_INT32; } else { return NA_INT64; } @@ -84,12 +80,12 @@ return type; #else case T_FIXNUM: case T_BIGNUM: if (type<NA_INT64) { - v = rb_funcall(v,id_abs,0); - if (RTEST(rb_funcall(v,id_le,1,int32_max))) { + if (RTEST(rb_funcall(v,id_le,1,int32_max)) && + RTEST(rb_funcall(v,id_ge,1,int32_min))) { if (type<NA_INT32) return NA_INT32; } else { return NA_INT64; } @@ -648,6 +644,11 @@ id_step = rb_intern("step"); id_cast = rb_intern("cast"); id_abs = rb_intern("abs"); id_le = rb_intern("<="); id_Complex = rb_intern("Complex"); + + rb_global_variable(&int32_max); + int32_max = INT2NUM(2147483647); + rb_global_variable(&int32_min); + int32_min = INT2NUM(-2147483648); }