[ f ] [ \ broken-declaration optimized? ] unit-test
-[ ] [ [ \ broken-declaration forget ] with-compilation-unit ] unit-test
\ No newline at end of file
+[ ] [ [ \ broken-declaration forget ] with-compilation-unit ] unit-test
+
+! Modular arithmetic bug
+: modular-arithmetic-bug ( a -- b ) >integer 256 mod ;
+
+[ 1 ] [ 257 modular-arithmetic-bug ] unit-test
+[ -10 ] [ -10 modular-arithmetic-bug ] unit-test
\ No newline at end of file
] { mod fixnum-mod } inlined?
] unit-test
-
[ f ] [
[
256 mod
] { mod fixnum-mod } inlined?
] unit-test
+[ f ] [
+ [
+ >fixnum 256 mod
+ ] { mod fixnum-mod } inlined?
+] unit-test
+
[ f ] [
[
dup 0 >= [ 256 mod ] when
{ integer } declare [ 256 rem ] map
] { mod fixnum-mod rem } inlined?
] unit-test
+
+[ [ >fixnum 255 fixnum-bitand ] ]
+[ [ >integer 256 rem ] test-modular-arithmetic ] unit-test
\ No newline at end of file
! See http://factorcode.org/license.txt for BSD license.
USING: math math.partial-dispatch namespaces sequences sets
accessors assocs words kernel memoize fry combinators
+combinators.short-circuit
compiler.tree
compiler.tree.combinators
compiler.tree.def-use
: optimize->fixnum ( #call -- nodes )
dup redundant->fixnum? [ drop f ] when ;
+: optimize->integer ( #call -- nodes )
+ dup out-d>> first actually-used-by dup length 1 = [
+ first node>> { [ #call? ] [ word>> \ >fixnum eq? ] } 1&&
+ [ drop { } ] when
+ ] [ drop ] if ;
+
MEMO: fixnum-coercion ( flags -- nodes )
[ [ ] [ >fixnum ] ? ] map '[ _ spread ] splice-quot ;
M: #call optimize-modular-arithmetic*
dup word>> {
{ [ dup \ >fixnum eq? ] [ drop optimize->fixnum ] }
+ { [ dup \ >integer eq? ] [ drop optimize->integer ] }
{ [ dup "modular-arithmetic" word-prop ] [ drop optimize-modular-op ] }
[ drop ]
} cond ;