GENERIC: optimize-modular-arithmetic* ( node -- nodes )
M: #push optimize-modular-arithmetic*
- dup out-d>> first modular-value? [
- [ >fixnum ] change-literal
- ] when ;
+ dup [ out-d>> first modular-value? ] [ literal>> real? ] bi and
+ [ [ >fixnum ] change-literal ] when ;
-: input-will-be-fixnum? ( #call -- ? )
+: redundant->fixnum? ( #call -- ? )
in-d>> first actually-defined-by
[ value>> { [ modular-value? ] [ fixnum-value? ] } 1&& ] all? ;
-: output-will-be-coerced? ( #call -- ? )
- out-d>> first modular-value? ;
-
-: redundant->fixnum? ( #call -- ? )
- {
- [ input-will-be-fixnum? ]
- [ output-will-be-coerced? ]
- } 1|| ;
-
: optimize->fixnum ( #call -- nodes )
dup redundant->fixnum? [ drop f ] when ;
] when ;
: optimize-low-order-op ( #call -- nodes )
- dup in-d>> first modular-value? [
+ dup in-d>> first fixnum-value? [
[ ] [ in-d>> first ] [ info>> ] tri
[ drop fixnum <class-info> ] change-at
] when ;
-USING: tools.test byte-arrays sequences kernel ;\r
+USING: tools.test byte-arrays sequences kernel math ;\r
IN: byte-arrays.tests\r
\r
[ 6 B{ 1 2 3 } ] [\r
[ -10 B{ } resize-byte-array ] must-fail\r
\r
[ B{ 123 } ] [ 123 1byte-array ] unit-test\r
+\r
+[ B{ 123 } ] [ 123 0 B{ 0 } [ set-nth ] keep ] unit-test\r
+\r
+[ B{ 123 } ] [ 123 >bignum 0 B{ 0 } [ set-nth ] keep ] unit-test
\ No newline at end of file
USING: tools.test io.streams.byte-array io.encodings.binary
-io.encodings.utf8 io kernel arrays strings namespaces ;
+io.encodings.utf8 io kernel arrays strings namespaces math ;
[ B{ } ] [ B{ } binary [ contents ] with-byte-reader ] unit-test
[ B{ 1 2 3 } ] [ binary [ B{ 1 2 3 } write ] with-byte-writer ] unit-test
read1
] with-byte-reader
] unit-test
+
+! Overly aggressive compiler optimizations
+[ B{ 123 } ] [
+ binary [ 123 >bignum write1 ] with-byte-writer
+] unit-test
\ No newline at end of file