compiler.tree.modular-arithmetic: convert >integer >fixnum into >fixnum
							parent
							
								
									d7b40d72a0
								
							
						
					
					
						commit
						75d9946bd7
					
				| 
						 | 
					@ -389,4 +389,10 @@ DEFER: loop-bbb
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[ f ] [ \ broken-declaration optimized? ] unit-test
 | 
					[ f ] [ \ broken-declaration optimized? ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[ ] [ [ \ broken-declaration forget ] with-compilation-unit ] unit-test
 | 
					[ ] [ [ \ 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
 | 
				
			||||||
| 
						 | 
					@ -98,13 +98,18 @@ TUPLE: declared-fixnum { x fixnum } ;
 | 
				
			||||||
    ] { mod fixnum-mod } inlined?
 | 
					    ] { mod fixnum-mod } inlined?
 | 
				
			||||||
] unit-test
 | 
					] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
[ f ] [
 | 
					[ f ] [
 | 
				
			||||||
    [
 | 
					    [
 | 
				
			||||||
        256 mod
 | 
					        256 mod
 | 
				
			||||||
    ] { mod fixnum-mod } inlined?
 | 
					    ] { mod fixnum-mod } inlined?
 | 
				
			||||||
] unit-test
 | 
					] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[ f ] [
 | 
				
			||||||
 | 
					    [
 | 
				
			||||||
 | 
					        >fixnum 256 mod
 | 
				
			||||||
 | 
					    ] { mod fixnum-mod } inlined?
 | 
				
			||||||
 | 
					] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[ f ] [
 | 
					[ f ] [
 | 
				
			||||||
    [
 | 
					    [
 | 
				
			||||||
        dup 0 >= [ 256 mod ] when
 | 
					        dup 0 >= [ 256 mod ] when
 | 
				
			||||||
| 
						 | 
					@ -128,3 +133,6 @@ TUPLE: declared-fixnum { x fixnum } ;
 | 
				
			||||||
        { integer } declare [ 256 rem ] map
 | 
					        { integer } declare [ 256 rem ] map
 | 
				
			||||||
    ] { mod fixnum-mod rem } inlined?
 | 
					    ] { mod fixnum-mod rem } inlined?
 | 
				
			||||||
] unit-test
 | 
					] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[ [ >fixnum 255 fixnum-bitand ] ]
 | 
				
			||||||
 | 
					[ [ >integer 256 rem ] test-modular-arithmetic ] unit-test
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@
 | 
				
			||||||
! See http://factorcode.org/license.txt for BSD license.
 | 
					! See http://factorcode.org/license.txt for BSD license.
 | 
				
			||||||
USING: math math.partial-dispatch namespaces sequences sets
 | 
					USING: math math.partial-dispatch namespaces sequences sets
 | 
				
			||||||
accessors assocs words kernel memoize fry combinators
 | 
					accessors assocs words kernel memoize fry combinators
 | 
				
			||||||
 | 
					combinators.short-circuit
 | 
				
			||||||
compiler.tree
 | 
					compiler.tree
 | 
				
			||||||
compiler.tree.combinators
 | 
					compiler.tree.combinators
 | 
				
			||||||
compiler.tree.def-use
 | 
					compiler.tree.def-use
 | 
				
			||||||
| 
						 | 
					@ -69,6 +70,12 @@ GENERIC: optimize-modular-arithmetic* ( node -- nodes )
 | 
				
			||||||
: optimize->fixnum ( #call -- nodes )
 | 
					: optimize->fixnum ( #call -- nodes )
 | 
				
			||||||
    dup redundant->fixnum? [ drop f ] when ;
 | 
					    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 )
 | 
					MEMO: fixnum-coercion ( flags -- nodes )
 | 
				
			||||||
    [ [ ] [ >fixnum ] ? ] map '[ _ spread ] splice-quot ;
 | 
					    [ [ ] [ >fixnum ] ? ] map '[ _ spread ] splice-quot ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -87,6 +94,7 @@ MEMO: fixnum-coercion ( flags -- nodes )
 | 
				
			||||||
M: #call optimize-modular-arithmetic*
 | 
					M: #call optimize-modular-arithmetic*
 | 
				
			||||||
    dup word>> {
 | 
					    dup word>> {
 | 
				
			||||||
        { [ dup \ >fixnum eq? ] [ drop optimize->fixnum ] }
 | 
					        { [ dup \ >fixnum eq? ] [ drop optimize->fixnum ] }
 | 
				
			||||||
 | 
					        { [ dup \ >integer eq? ] [ drop optimize->integer ] }
 | 
				
			||||||
        { [ dup "modular-arithmetic" word-prop ] [ drop optimize-modular-op ] }
 | 
					        { [ dup "modular-arithmetic" word-prop ] [ drop optimize-modular-op ] }
 | 
				
			||||||
        [ drop ]
 | 
					        [ drop ]
 | 
				
			||||||
    } cond ;
 | 
					    } cond ;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue