File tree 4 files changed +55
-8
lines changed
4 files changed +55
-8
lines changed Original file line number Diff line number Diff line change
1
+ Thu Sep 9 22:34:48 2010 wanabe <s.wanabe@gmail.com>
2
+
3
+ * compile.c (case_when_optimizable_literal): When float value can be
4
+ treated as integer, add to table hash of case that way.
5
+ based on a patch from Ikuo KOBORI. [ruby-dev:42038]
6
+
7
+ * insnf.def (opt_case_dispatch): ditto.
8
+
9
+ * test/ruby/test_case.rb: add tests.
10
+
1
11
Thu Sep 9 17:15:15 2010 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
2
12
3
13
* test/net/http/test_https.rb (test_identity_verify_failure): follows
Original file line number Diff line number Diff line change @@ -2303,6 +2303,11 @@ case_when_optimizable_literal(NODE * node)
2303
2303
switch (nd_type (node )) {
2304
2304
case NODE_LIT : {
2305
2305
VALUE v = node -> nd_lit ;
2306
+ double ival ;
2307
+ if (TYPE (v ) == T_FLOAT &&
2308
+ modf (RFLOAT_VALUE (v ), & ival ) == 0.0 ) {
2309
+ return FIXABLE (ival ) ? LONG2FIX ((long )ival ) : rb_dbl2big (ival );
2310
+ }
2306
2311
if (SYMBOL_P (v ) || rb_obj_is_kind_of (v , rb_cNumeric )) {
2307
2312
return v ;
2308
2313
}
Original file line number Diff line number Diff line change @@ -1260,16 +1260,28 @@ opt_case_dispatch
1260
1260
(..., VALUE key )
1261
1261
() // inc += -1;
1262
1262
{
1263
- if (BASIC_OP_UNREDEFINED_P (BOP_EQQ )) {
1264
- VALUE val ;
1265
- if (st_lookup (RHASH_TBL (hash ), key , & val )) {
1266
- JUMP (FIX2INT (val ));
1263
+ switch (TYPE (key )) {
1264
+ case T_FLOAT : {
1265
+ double ival ;
1266
+ if (modf (RFLOAT_VALUE (key ), & ival ) == 0.0 ) {
1267
+ key = FIXABLE (ival ) ? LONG2FIX ((long )ival ) : rb_dbl2big (ival );
1267
1268
}
1268
- else {
1269
- JUMP (else_offset );
1269
+ }
1270
+ case T_SYMBOL : /* fall through */
1271
+ case T_FIXNUM :
1272
+ case T_BIGNUM :
1273
+ case T_STRING :
1274
+ if (BASIC_OP_UNREDEFINED_P (BOP_EQQ )) {
1275
+ VALUE val ;
1276
+ if (st_lookup (RHASH_TBL (hash ), key , & val )) {
1277
+ JUMP (FIX2INT (val ));
1278
+ }
1279
+ else {
1280
+ JUMP (else_offset );
1281
+ }
1282
+ break ;
1270
1283
}
1271
- }
1272
- else {
1284
+ default : { /* fall through (else) */
1273
1285
struct opt_case_dispatch_i_arg arg ;
1274
1286
1275
1287
arg .obj = key ;
@@ -1282,6 +1294,7 @@ opt_case_dispatch
1282
1294
else {
1283
1295
JUMP (else_offset );
1284
1296
}
1297
+ }
1285
1298
}
1286
1299
}
1287
1300
Original file line number Diff line number Diff line change @@ -84,4 +84,23 @@ class Symbol; undef ===; def ===(o); p 42; true; end; end; case :foo; when :foo;
84
84
class Fixnum; undef ===; def ===(o); p 42; true; end; end; case 1; when 1; end
85
85
EOS
86
86
end
87
+
88
+ def test_optimization
89
+ case 1
90
+ when 0.9 , 1.1
91
+ assert ( false )
92
+ when 1.0
93
+ assert ( true )
94
+ else
95
+ assert ( false )
96
+ end
97
+ case 536870912
98
+ when 536870911.9 , 536870912.1
99
+ assert ( false )
100
+ when 536870912.0
101
+ assert ( true )
102
+ else
103
+ assert ( false )
104
+ end
105
+ end
87
106
end
You can’t perform that action at this time.
0 commit comments