diff --git a/ChangeLog b/ChangeLog index 6845924348..7efa712e90 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Sun Jan 12 09:21:35 2014 Nobuyoshi Nakada + + * include/ruby/util.h (DECIMAL_SIZE_OF_BITS): a preprocessor + constant macro to approximate decimal representation size of n-bits + integer. + + * iseq.c (register_label): use DECIMAL_SIZE_OF_BITS for better + approximation. + + * ext/bigdecimal/bigdecimal.c (BigMath_s_log): ditto. + + * common.mk (iseq.o), ext/bigdecimal/depend (bigdecimal.o): add + dependency to ruby/util.h for DECIMAL_SIZE_OF_BITS. + Fri Jan 10 16:27:20 2014 Yuki Yugui Sonoda * vm_exec.c (cfp): Avoid generating invalid binary for diff --git a/common.mk b/common.mk index f30df0fee0..4f081c24f9 100644 --- a/common.mk +++ b/common.mk @@ -786,7 +786,9 @@ compile.$(OBJEXT): {$(VPATH)}compile.c {$(VPATH)}iseq.h \ {$(VPATH)}internal.h {$(VPATH)}vm_opts.h iseq.$(OBJEXT): {$(VPATH)}iseq.c {$(VPATH)}gc.h {$(VPATH)}iseq.h \ $(RUBY_H_INCLUDES) $(VM_CORE_H_INCLUDES) {$(VPATH)}insns.inc \ - {$(VPATH)}insns_info.inc {$(VPATH)}node_name.inc {$(VPATH)}internal.h {$(VPATH)}vm_opts.h {$(VPATH)}ruby_atomic.h {$(VPATH)}eval_intern.h + {$(VPATH)}insns_info.inc {$(VPATH)}node_name.inc {$(VPATH)}internal.h \ + {$(VPATH)}vm_opts.h {$(VPATH)}ruby_atomic.h {$(VPATH)}eval_intern.h \ + {$(VPATH)}util.h vm.$(OBJEXT): {$(VPATH)}vm.c {$(VPATH)}gc.h {$(VPATH)}iseq.h \ {$(VPATH)}eval_intern.h $(RUBY_H_INCLUDES) $(ENCODING_H_INCLUDES) \ $(VM_CORE_H_INCLUDES) {$(VPATH)}vm_method.c {$(VPATH)}vm_eval.c \ diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index c5a3326fb7..16fba2211f 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -18,6 +18,7 @@ # define BIGDECIMAL_ENABLE_VPRINT 1 #endif #include "bigdecimal.h" +#include "ruby/util.h" #ifndef BIGDECIMAL_DEBUG # define NDEBUG @@ -2919,7 +2920,7 @@ get_vp_value: RB_GC_GUARD(vn) = SSIZET2NUM(n); expo = VpExponent10(vx); if (expo < 0 || expo >= 3) { - char buf[SIZEOF_VALUE * CHAR_BIT / 3 + 4]; + char buf[DECIMAL_SIZE_OF_BITS(SIZEOF_VALUE * CHAR_BIT) + 4]; snprintf(buf, sizeof(buf), "1E%"PRIdVALUE, -expo); x = BigDecimal_mult2(x, ToValue(VpCreateRbObject(1, buf)), vn); } diff --git a/ext/bigdecimal/depend b/ext/bigdecimal/depend index a68128478c..0a83c1f17f 100644 --- a/ext/bigdecimal/depend +++ b/ext/bigdecimal/depend @@ -1 +1 @@ -bigdecimal.o: bigdecimal.c bigdecimal.h $(HDRS) $(ruby_headers) +bigdecimal.o: bigdecimal.c bigdecimal.h $(HDRS) $(ruby_headers) $(hdrdir)/ruby/util.h diff --git a/include/ruby/util.h b/include/ruby/util.h index 5be5f2e0b4..8443af74d9 100644 --- a/include/ruby/util.h +++ b/include/ruby/util.h @@ -47,6 +47,9 @@ extern "C" { RUBY_SYMBOL_EXPORT_BEGIN +#define DECIMAL_SIZE_OF_BITS(n) (((n) * 3010 + 9998) / 9999) +/* an approximation of ceil(n * log10(2)), upto 65536 at least */ + #define scan_oct(s,l,e) ((int)ruby_scan_oct((s),(l),(e))) unsigned long ruby_scan_oct(const char *, size_t, size_t *); #define scan_hex(s,l,e) ((int)ruby_scan_hex((s),(l),(e))) diff --git a/iseq.c b/iseq.c index a829089ea4..befa0364dc 100644 --- a/iseq.c +++ b/iseq.c @@ -10,6 +10,7 @@ **********************************************************************/ #include "ruby/ruby.h" +#include "ruby/util.h" #include "internal.h" #include "eval_intern.h" @@ -1612,7 +1613,7 @@ static VALUE register_label(struct st_table *table, unsigned long idx) { VALUE sym; - char buff[8 + (sizeof(idx) * CHAR_BIT * 32 / 100)]; + char buff[7 + DECIMAL_SIZE_OF_BITS(sizeof(idx) * CHAR_BIT)]; snprintf(buff, sizeof(buff), "label_%lu", idx); sym = ID2SYM(rb_intern(buff));