ruby-changes:45079
From: nobu <ko1@a...>
Date: Fri, 23 Dec 2016 05:59:00 +0900 (JST)
Subject: [ruby-changes:45079] nobu:r57152 (trunk): get rid of implicit signedness conversions
nobu 2016-12-23 05:58:55 +0900 (Fri, 23 Dec 2016) New Revision: 57152 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57152 Log: get rid of implicit signedness conversions Modified files: trunk/include/ruby/ruby.h trunk/internal.h Index: internal.h =================================================================== --- internal.h (revision 57151) +++ internal.h (revision 57152) @@ -133,20 +133,20 @@ extern "C" { https://github.com/ruby/ruby/blob/trunk/internal.h#L133 # endif #endif -static inline int +static inline unsigned int nlz_int(unsigned int x) { #if defined(HAVE_BUILTIN___BUILTIN_CLZ) if (x == 0) return SIZEOF_INT * CHAR_BIT; - return __builtin_clz(x); + return (unsigned int)__builtin_clz(x); #else unsigned int y; # if 64 < SIZEOF_INT * CHAR_BIT - int n = 128; + unsigned int n = 128; # elif 32 < SIZEOF_INT * CHAR_BIT - int n = 64; + unsigned int n = 64; # else - int n = 32; + unsigned int n = 32; # endif # if 64 < SIZEOF_INT * CHAR_BIT y = x >> 64; if (y) {n -= 64; x = y;} @@ -159,24 +159,24 @@ nlz_int(unsigned int x) https://github.com/ruby/ruby/blob/trunk/internal.h#L159 y = x >> 4; if (y) {n -= 4; x = y;} y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 1; if (y) {return n - 2;} - return (int)(n - x); + return (unsigned int)(n - x); #endif } -static inline int +static inline unsigned int nlz_long(unsigned long x) { #if defined(HAVE_BUILTIN___BUILTIN_CLZL) if (x == 0) return SIZEOF_LONG * CHAR_BIT; - return __builtin_clzl(x); + return (unsigned int)__builtin_clzl(x); #else unsigned long y; # if 64 < SIZEOF_LONG * CHAR_BIT - int n = 128; + unsigned int n = 128; # elif 32 < SIZEOF_LONG * CHAR_BIT - int n = 64; + unsigned int n = 64; # else - int n = 32; + unsigned int n = 32; # endif # if 64 < SIZEOF_LONG * CHAR_BIT y = x >> 64; if (y) {n -= 64; x = y;} @@ -189,25 +189,25 @@ nlz_long(unsigned long x) https://github.com/ruby/ruby/blob/trunk/internal.h#L189 y = x >> 4; if (y) {n -= 4; x = y;} y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 1; if (y) {return n - 2;} - return (int)(n - x); + return (unsigned int)(n - x); #endif } #ifdef HAVE_LONG_LONG -static inline int +static inline unsigned int nlz_long_long(unsigned LONG_LONG x) { #if defined(HAVE_BUILTIN___BUILTIN_CLZLL) if (x == 0) return SIZEOF_LONG_LONG * CHAR_BIT; - return __builtin_clzll(x); + return (unsigned int)__builtin_clzll(x); #else unsigned LONG_LONG y; # if 64 < SIZEOF_LONG_LONG * CHAR_BIT - int n = 128; + unsigned int n = 128; # elif 32 < SIZEOF_LONG_LONG * CHAR_BIT - int n = 64; + unsigned int n = 64; # else - int n = 32; + unsigned int n = 32; # endif # if 64 < SIZEOF_LONG_LONG * CHAR_BIT y = x >> 64; if (y) {n -= 64; x = y;} @@ -220,17 +220,17 @@ nlz_long_long(unsigned LONG_LONG x) https://github.com/ruby/ruby/blob/trunk/internal.h#L220 y = x >> 4; if (y) {n -= 4; x = y;} y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 1; if (y) {return n - 2;} - return (int)(n - x); + return (unsigned int)(n - x); #endif } #endif #ifdef HAVE_UINT128_T -static inline int +static inline unsigned int nlz_int128(uint128_t x) { uint128_t y; - int n = 128; + unsigned int n = 128; y = x >> 64; if (y) {n -= 64; x = y;} y = x >> 32; if (y) {n -= 32; x = y;} y = x >> 16; if (y) {n -= 16; x = y;} @@ -238,12 +238,13 @@ nlz_int128(uint128_t x) https://github.com/ruby/ruby/blob/trunk/internal.h#L238 y = x >> 4; if (y) {n -= 4; x = y;} y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 1; if (y) {return n - 2;} - return (int)(n - x); + return (unsigned int)(n - x); } #endif -static inline int -nlz_intptr(uintptr_t x) { +static inline unsigned int +nlz_intptr(uintptr_t x) +{ #if SIZEOF_VOIDP == 8 return nlz_long_long(x); #elif SIZEOF_VOIDP == 4 @@ -251,10 +252,11 @@ nlz_intptr(uintptr_t x) { https://github.com/ruby/ruby/blob/trunk/internal.h#L252 #endif } -static inline int -rb_popcount32(uint32_t x) { +static inline unsigned int +rb_popcount32(uint32_t x) +{ #ifdef HAVE_BUILTIN___BUILTIN_POPCOUNT - return __builtin_popcount(x); + return (unsigned int)__builtin_popcount(x); #else x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); @@ -265,7 +267,8 @@ rb_popcount32(uint32_t x) { https://github.com/ruby/ruby/blob/trunk/internal.h#L267 } static inline int -rb_popcount64(uint64_t x) { +rb_popcount64(uint64_t x) +{ #ifdef HAVE_BUILTIN___BUILTIN_POPCOUNT return __builtin_popcountll(x); #else @@ -279,7 +282,8 @@ rb_popcount64(uint64_t x) { https://github.com/ruby/ruby/blob/trunk/internal.h#L282 } static inline int -rb_popcount_intptr(uintptr_t x) { +rb_popcount_intptr(uintptr_t x) +{ #if SIZEOF_VOIDP == 8 return rb_popcount64(x); #elif SIZEOF_VOIDP == 4 @@ -288,7 +292,8 @@ rb_popcount_intptr(uintptr_t x) { https://github.com/ruby/ruby/blob/trunk/internal.h#L292 } static inline int -ntz_int32(uint32_t x) { +ntz_int32(uint32_t x) +{ #ifdef HAVE_BUILTIN___BUILTIN_CTZ return __builtin_ctz(x); #else @@ -297,7 +302,8 @@ ntz_int32(uint32_t x) { https://github.com/ruby/ruby/blob/trunk/internal.h#L302 } static inline int -ntz_int64(uint64_t x) { +ntz_int64(uint64_t x) +{ #ifdef HAVE_BUILTIN___BUILTIN_CTZLL return __builtin_ctzll(x); #else @@ -306,7 +312,8 @@ ntz_int64(uint64_t x) { https://github.com/ruby/ruby/blob/trunk/internal.h#L312 } static inline int -ntz_intptr(uintptr_t x) { +ntz_intptr(uintptr_t x) +{ #if SIZEOF_VOIDP == 8 return ntz_int64(x); #elif SIZEOF_VOIDP == 4 @@ -397,17 +404,20 @@ rb_fix_mod_fix(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/internal.h#L404 #if defined(HAVE_UINT128_T) # define bit_length(x) \ + (unsigned int) \ (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int((unsigned int)(x)) : \ sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long((unsigned long)(x)) : \ sizeof(x) <= SIZEOF_LONG_LONG ? SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long((unsigned LONG_LONG)(x)) : \ SIZEOF_INT128_T * CHAR_BIT - nlz_int128((uint128_t)(x))) #elif defined(HAVE_LONG_LONG) # define bit_length(x) \ + (unsigned int) \ (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int((unsigned int)(x)) : \ sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long((unsigned long)(x)) : \ SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long((unsigned LONG_LONG)(x))) #else # define bit_length(x) \ + (unsigned int) \ (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int((unsigned int)(x)) : \ SIZEOF_LONG * CHAR_BIT - nlz_long((unsigned long)(x))) #endif @@ -485,7 +495,7 @@ struct RBignum { https://github.com/ruby/ruby/blob/trunk/internal.h#L495 BDIGIT ary[BIGNUM_EMBED_LEN_MAX]; } as; }; -#define BIGNUM_SIGN_BIT FL_USER1 +#define BIGNUM_SIGN_BIT ((VALUE)FL_USER1) /* sign: positive:1, negative:0 */ #define BIGNUM_SIGN(b) ((RBASIC(b)->flags & BIGNUM_SIGN_BIT) != 0) #define BIGNUM_SET_SIGN(b,sign) \ @@ -495,13 +505,13 @@ struct RBignum { https://github.com/ruby/ruby/blob/trunk/internal.h#L505 #define BIGNUM_NEGATIVE_P(b) (!BIGNUM_SIGN(b)) #define BIGNUM_NEGATE(b) (RBASIC(b)->flags ^= BIGNUM_SIGN_BIT) -#define BIGNUM_EMBED_FLAG FL_USER2 -#define BIGNUM_EMBED_LEN_MASK (FL_USER5|FL_USER4|FL_USER3) +#define BIGNUM_EMBED_FLAG ((VALUE)FL_USER2) +#define BIGNUM_EMBED_LEN_MASK ((VALUE)(FL_USER5|FL_USER4|FL_USER3)) #define BIGNUM_EMBED_LEN_SHIFT (FL_USHIFT+BIGNUM_EMBED_LEN_NUMBITS) #define BIGNUM_LEN(b) \ ((RBASIC(b)->flags & BIGNUM_EMBED_FLAG) ? \ - (long)((RBASIC(b)->flags >> BIGNUM_EMBED_LEN_SHIFT) & \ - (BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT)) : \ + (size_t)((RBASIC(b)->flags >> BIGNUM_EMBED_LEN_SHIFT) & \ + (BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT)) : \ RBIGNUM(b)->as.heap.len) /* LSB:BIGNUM_DIGITS(b)[0], MSB:BIGNUM_DIGITS(b)[BIGNUM_LEN(b)-1] */ #define BIGNUM_DIGITS(b) \ @@ -559,7 +569,7 @@ struct RHash { https://github.com/ruby/ruby/blob/trunk/internal.h#L569 #undef RHASH_SIZE #define RHASH_ITER_LEV(h) (RHASH(h)->iter_lev) #define RHASH_IFNONE(h) (RHASH(h)->ifnone) -#define RHASH_SIZE(h) (RHASH(h)->ntbl ? (st_index_t)RHASH(h)->ntbl->num_entries : 0) +#define RHASH_SIZE(h) (RHASH(h)->ntbl ? RHASH(h)->ntbl->num_entries : (st_index_t)0) #endif /* missing/setproctitle.c */ @@ -834,8 +844,8 @@ enum { https://github.com/ruby/ruby/blob/trunk/internal.h#L844 }; struct cmp_opt_data { - int opt_methods; - int opt_inited; + unsigned int opt_methods; + unsigned int opt_inited; }; #define NEW_CMP_OPT_MEMO(type, value) \ @@ -1214,7 +1224,7 @@ rb_float_flonum_value(VALUE v) https://github.com/ruby/ruby/blob/trunk/internal.h#L1224 /* e: xx1... -> 011... */ /* xx0... -> 100... */ /* ^b63 */ - t.v = RUBY_BIT_ROTR((2 - b63) | (v & ~0x03), 3); + t.v = RUBY_BIT_ROTR((2 - b63) | (v & ~(VALUE)0x03), 3); return t.d; } #endif Index: include/ruby/ruby.h =================================================================== --- include/ruby/ruby.h (revision 57151) +++ include/ruby/ruby.h (revision 57152) @@ -429,10 +429,14 @@ enum ruby_special_consts { https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L429 RUBY_SPECIAL_SHIFT = 8 }; -#define Qfalse ((VALUE)RUBY_Qfalse) -#define Qtrue ((VALUE)RUBY_Qtrue) -#define Qnil ((VALUE)RUBY_Qnil) -#define Qundef ((VALUE)RUBY_Qundef) /* undefined value for placeholder */ +#define RUBY_Qfalse ((VALUE)RUBY_Qfalse) +#define RUBY_Qtrue ((VALUE)RUBY_Qtrue) +#define RUBY_Qnil ((VALUE)RUBY_Qnil) +#define RUBY_Qundef ((VALUE)RUBY_Qundef) /* undefined value for placeholder */ +#define Qfalse RUBY_Qfalse +#define Qtrue RUBY_Qtrue +#define Qnil RUBY_Qnil +#define Qundef RUBY_Qundef #define IMMEDIATE_MASK RUBY_IMMEDIATE_MASK #define FIXNUM_FLAG RUBY_FIXNUM_FLAG #if USE_FLONUM @@ -985,10 +989,6 @@ struct RString { https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L989 ((ptrvar) = RSTRING(str)->as.ary, (lenvar) = RSTRING_EMBED_LEN(str)) : \ ((ptrvar) = RSTRING(str)->as.heap.ptr, (lenvar) = RSTRING(str)->as.heap.len)) -#define RARRAY_EMBED_FLAG RARRAY_EMBED_FLAG -#define RARRAY_EMBED_LEN_MASK RARRAY_EMBED_LEN_MASK -#define RARRAY_EMBED_LEN_MAX RARRAY_EMBED_LEN_MAX -#define RARRAY_EMBED_LEN_SHIFT RARRAY_EMBED_LEN_SHIFT enum { RARRAY_EMBED_LEN_MAX = 3, RARRAY_EMBED_FLAG = RUBY_FL_USER1, @@ -998,6 +998,10 @@ enum { https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L998 RARRAY_ENUM_END }; +#define RARRAY_EMBED_FLAG (VALUE)RARRAY_EMBED_FLAG +#define RARRAY_EMBED_LEN_MASK (VALUE)RARRAY_EMBED_LEN_MASK +#define RARRAY_EMBED_LEN_MAX RARRAY_EMBED_LEN_MAX +#define RARRAY_EMBED_LEN_SHIFT RARRAY_EMBED_LEN_SHIFT struct RArray { struct RBasic basic; union { @@ -1204,38 +1208,38 @@ int rb_big_sign(VALUE); https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1208 #define RTYPEDDATA(obj) (R_CAST(RTypedData)(obj)) #define RFILE(obj) (R_CAST(RFile)(obj)) -#define FL_SINGLETON RUBY_FL_SINGLETON -#define FL_WB_PROTECTED RUBY_FL_WB_PROTECTED -#define FL_PROMOTED0 RUBY_FL_PROMOTED0 -#define FL_PROMOTED1 RUBY_FL_PROMOTED1 -#define FL_FINALIZE RUBY_FL_FINALIZE -#define FL_TAINT RUBY_FL_TAINT -#define FL_UNTRUSTED RUBY_FL_UNTRUSTED -#define FL_EXIVAR RUBY_FL_EXIVAR -#define FL_FREEZE RUBY_FL_FREEZE - -#define FL_USHIFT RUBY_FL_USHIFT - -#define FL_USER0 RUBY_FL_USER0 -#define FL_USER1 RUBY_FL_USER1 -#define FL_USER2 RUBY_FL_USER2 -#define FL_USER3 RUBY_FL_USER3 -#define FL_USER4 RUBY_FL_USER4 -#define FL_USER5 RUBY_FL_USER5 -#define FL_USER6 RUBY_FL_USER6 -#define FL_USER7 RUBY_FL_USER7 -#define FL_USER8 RUBY_FL_USER8 -#define FL_USER9 RUBY_FL_USER9 -#define FL_USER10 RUBY_FL_USER10 -#define FL_USER11 RUBY_FL_USER11 -#define FL_USER12 RUBY_FL_USER12 -#define FL_USER13 RUBY_FL_USER13 -#define FL_USER14 RUBY_FL_USER14 -#define FL_USER15 RUBY_FL_USER15 -#define FL_USER16 RUBY_FL_USER16 -#define FL_USER17 RUBY_FL_USER17 -#define FL_USER18 RUBY_FL_USER18 -#define FL_USER19 RUBY_FL_USER19 +#define FL_SINGLETON ((VALUE)RUBY_FL_SINGLETON) +#define FL_WB_PROTECTED ((VALUE)RUBY_FL_WB_PROTECTED) +#define FL_PROMOTED0 ((VALUE)RUBY_FL_PROMOTED0) +#define FL_PROMOTED1 ((VALUE)RUBY_FL_PROMOTED1) +#define FL_FINALIZE ((VALUE)RUBY_FL_FINALIZE) +#define FL_TAINT ((VALUE)RUBY_FL_TAINT) +#define FL_UNTRUSTED ((VALUE)RUBY_FL_UNTRUSTED) +#define FL_EXIVAR ((VALUE)RUBY_FL_EXIVAR) +#define FL_FREEZE ((VALUE)RUBY_FL_FREEZE) + +#define FL_USHIFT ((VALUE)RUBY_FL_USHIFT) + +#define FL_USER0 ((VALUE)RUBY_FL_USER0) +#define FL_USER1 ((VALUE)RUBY_FL_USER1) +#define FL_USER2 ((VALUE)RUBY_FL_USER2) +#define FL_USER3 ((VALUE)RUBY_FL_USER3) +#define FL_USER4 ((VALUE)RUBY_FL_USER4) +#define FL_USER5 ((VALUE)RUBY_FL_USER5) +#define FL_USER6 ((VALUE)RUBY_FL_USER6) +#define FL_USER7 ((VALUE)RUBY_FL_USER7) +#define FL_USER8 ((VALUE)RUBY_FL_USER8) +#define FL_USER9 ((VALUE)RUBY_FL_USER9) +#define FL_USER10 ((VALUE)RUBY_FL_USER10) +#define FL_USER11 ((VALUE)RUBY_FL_USER11) +#define FL_USER12 ((VALUE)RUBY_FL_USER12) +#define FL_USER13 ((VALUE)RUBY_FL_USER13) +#define FL_USER14 ((VALUE)RUBY_FL_USER14) +#define FL_USER15 ((VALUE)RUBY_FL_USER15) +#define FL_USER16 ((VALUE)RUBY_FL_USER16) +#define FL_USER17 ((VALUE)RUBY_FL_USER17) +#define FL_USER18 ((VALUE)RUBY_FL_USER18) +#define FL_USER19 ((VALUE)RUBY_FL_USER19) #define RB_SPECIAL_CONST_P(x) (RB_IMMEDIATE_P(x) || !RB_TEST(x)) #define SPECIAL_CONST_P(x) RB_SPECIAL_CONST_P(x) @@ -1249,7 +1253,7 @@ int rb_big_sign(VALUE); https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1253 #define RB_FL_ALL(x,f) (RB_FL_TEST((x),(f)) == (f)) #define RB_FL_SET_RAW(x,f) (void)(RBASIC(x)->flags |= (f)) #define RB_FL_SET(x,f) (RB_FL_ABLE(x) ? RB_FL_SET_RAW(x, f) : (void)0) -#define RB_FL_UNSET_RAW(x,f) (void)(RBASIC(x)->flags &= ~(f)) +#define RB_FL_UNSET_RAW(x,f) (void)(RBASIC(x)->flags &= ~(VALUE)(f)) #define RB_FL_UNSET(x,f) (RB_FL_ABLE(x) ? RB_FL_UNSET_RAW(x, f) : (void)0) #define RB_FL_REVERSE_RAW(x,f) (void)(RBASIC(x)->flags ^= (f)) #define RB_FL_REVERSE(x,f) (RB_FL_ABLE(x) ? RB_FL_REVERSE_RAW(x, f) : (void)0) @@ -1567,11 +1571,11 @@ rb_num2char_inline(VALUE x) https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1571 #define NUM2CHR(x) RB_NUM2CHR(x) #define CHR2FIX(x) RB_CHR2FIX(x) -#define RB_ALLOC_N(type,n) ((type*)ruby_xmalloc2((n),sizeof(type))) +#define RB_ALLOC_N(type,n) ((type*)ruby_xmalloc2((size_t)(n),sizeof(type))) #define RB_ALLOC(type) ((type*)ruby_xmalloc(sizeof(type))) -#define RB_ZALLOC_N(type,n) ((type*)ruby_xcalloc((n),sizeof(type))) +#define RB_ZALLOC_N(type,n) ((type*)ruby_xcalloc((size_t)(n),sizeof(type))) #define RB_ZALLOC(type) (RB_ZALLOC_N(type,1)) -#define RB_REALLOC_N(var,type,n) ((var)=(type*)ruby_xrealloc2((char*)(var),(n),sizeof(type))) +#define RB_REALLOC_N(var,type,n) ((var)=(type*)ruby_xrealloc2((char*)(var),(size_t)(n),sizeof(type))) #define ALLOC_N(type,n) RB_ALLOC_N(type,n) #define ALLOC(type) RB_ALLOC(type) @@ -1637,8 +1641,8 @@ rb_alloc_tmp_buffer2(volatile VALUE *sto https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1641 rb_alloc_tmp_buffer(&(v), (n))) # define RB_ALLOCV_N(type, v, n) \ ((type*)(((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \ - (RB_GC_GUARD(v) = 0, alloca((n) * sizeof(type))) : \ - rb_alloc_tmp_buffer2(&(v), (n), sizeof(type)))) + (RB_GC_GUARD(v) = 0, alloca((size_t)(n) * sizeof(type))) : \ + rb_alloc_tmp_buffer2(&(v), (long)(n), sizeof(type)))) #endif #define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v)) @@ -1646,10 +1650,10 @@ rb_alloc_tmp_buffer2(volatile VALUE *sto https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1650 #define ALLOCV_N(type, v, n) RB_ALLOCV_N(type, v, n) #define ALLOCV_END(v) RB_ALLOCV_END(v) -#define MEMZERO(p,type,n) memset((p), 0, sizeof(type)*(n)) -#define MEMCPY(p1,p2,type,n) memcpy((p1), (p2), sizeof(type)*(n)) -#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), sizeof(type)*(n)) -#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), sizeof(type)*(n)) +#define MEMZERO(p,type,n) memset((p), 0, sizeof(type)*(size_t)(n)) +#define MEMCPY(p1,p2,type,n) memcpy((p1), (p2), sizeof(type)*(size_t)(n)) +#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), sizeof(type)*(size_t)(n)) +#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), sizeof(type)*(size_t)(n)) void rb_obj_infect(VALUE,VALUE); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/