]> gitweb.factorcode.org Git - factor.git/commitdiff
More accurate interval inference for mod, rem, and propagation can now infer interval...
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 9 Aug 2009 04:03:45 +0000 (23:03 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 9 Aug 2009 04:03:45 +0000 (23:03 -0500)
basis/compiler/tree/propagation/info/info-tests.factor
basis/compiler/tree/propagation/info/info.factor
basis/compiler/tree/propagation/known-words/known-words.factor
basis/compiler/tree/propagation/propagation-tests.factor
basis/math/intervals/intervals.factor

index 72c08dbf1c5f3cd92435e87f452eae28e1c78961..826131ab612525013b49a2c37c14488d238bbafe 100644 (file)
@@ -74,3 +74,13 @@ TUPLE: test-tuple { x read-only } ;
 [ t ] [
     null-info 3 <literal-info> value-info<=
 ] unit-test
+
+[ t t ] [
+    f <literal-info>
+    fixnum 0 40 [a,b] <class/interval-info>
+    value-info-union
+    \ f class-not <class-info>
+    value-info-intersect
+    [ class>> fixnum class= ]
+    [ interval>> 0 40 [a,b] = ] bi
+] unit-test
index a2dec1227942a2a97d220c656cb4a986f7e79296..98baba3e973431b20434b3910507c6837b4c7da0 100644 (file)
@@ -1,9 +1,10 @@
 ! Copyright (C) 2008, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: assocs classes classes.algebra classes.tuple
-classes.tuple.private kernel accessors math math.intervals
-namespaces sequences words combinators byte-arrays strings
-arrays layouts cpu.architecture compiler.tree.propagation.copy ;
+classes.tuple.private kernel accessors math math.intervals namespaces
+sequences words combinators combinators.short-circuit byte-arrays
+strings arrays layouts cpu.architecture compiler.tree.propagation.copy
+ ;
 IN: compiler.tree.propagation.info
 
 : false-class? ( class -- ? ) \ f class<= ;
@@ -69,7 +70,7 @@ DEFER: <literal-info>
 UNION: fixed-length array byte-array string ;
 
 : init-literal-info ( info -- info )
-    [-inf,inf] >>interval
+    empty-interval >>interval
     dup literal>> class >>class
     dup literal>> {
         { [ dup real? ] [ [a,a] >>interval ] }
@@ -78,11 +79,17 @@ UNION: fixed-length array byte-array string ;
         [ drop ]
     } cond ; inline
 
+: empty-set? ( info -- ? )
+    {
+        [ class>> null-class? ]
+        [ [ class>> real class<= ] [ interval>> empty-interval eq? ] bi and ]
+    } 1|| ;
+
 : init-value-info ( info -- info )
     dup literal?>> [
         init-literal-info
     ] [
-        dup [ class>> null-class? ] [ interval>> empty-interval eq? ] bi or [
+        dup empty-set? [
             null >>class
             empty-interval >>interval
         ] [
index f5ea64bc0a48348dce16161570f3baf6bc9f88e1..a2955ca699a9a136f9ac1d9450b146edc6ca3881 100644 (file)
@@ -173,7 +173,8 @@ generic-comparison-ops [
     [ object-info ] [ f <literal-info> ] if ;
 
 : info-intervals-intersect? ( info1 info2 -- ? )
-    [ interval>> ] bi@ intervals-intersect? ;
+    2dup [ class>> real class<= ] both?
+    [ [ interval>> ] bi@ intervals-intersect? ] [ 2drop t ] if ;
 
 { number= bignum= float= } [
     [
index e5cade415aefdbf0d670f438065c96887a1d499a..46d98c28b6c60673c3fa8c60676be447bdf74e5a 100644 (file)
@@ -155,6 +155,8 @@ IN: compiler.tree.propagation.tests
 
 [ V{ t } ] [ [ 40 rem 0 >= ] final-literals ] unit-test
 
+[ V{ t } ] [ [ abs 40 mod 0 >= ] final-literals ] unit-test
+
 [ V{ string } ] [
     [ dup string? not [ "Oops" throw ] [ ] if ] final-classes
 ] unit-test
@@ -638,6 +640,10 @@ MIXIN: empty-mixin
     [ { integer } declare 127 bitand ] final-info first interval>>
 ] unit-test
 
+[ V{ t } ] [
+    [ [ 123 bitand ] [ drop f ] if dup [ 0 >= ] [ not ] if ] final-literals
+] unit-test
+  
 [ V{ bignum } ] [
     [ { bignum } declare dup 1- bitxor ] final-classes
 ] unit-test
index 0c2540eb8b4933a002b74eb13df36f4678409f7e..e216b35d511bce34e073f2749c00e79a35d59fdc 100755 (executable)
@@ -269,22 +269,6 @@ TUPLE: interval { from read-only } { to read-only } ;
         [ (interval-abs) points>interval ]
     } cond ;
 
-: interval-mod ( i1 i2 -- i3 )
-    {
-        { [ over empty-interval eq? ] [ drop ] }
-        { [ dup empty-interval eq? ] [ nip ] }
-        { [ dup full-interval eq? ] [ nip ] }
-        [ nip interval-abs to>> first [ neg ] keep (a,b) ]
-    } cond ;
-
-: interval-rem ( i1 i2 -- i3 )
-    {
-        { [ over empty-interval eq? ] [ drop ] }
-        { [ dup empty-interval eq? ] [ nip ] }
-        { [ dup full-interval eq? ] [ nip ] }
-        [ nip interval-abs to>> first 0 swap [a,b) ]
-    } cond ;
-
 : interval-recip ( i1 -- i2 ) 1 [a,a] swap interval/ ;
 
 : interval-2/ ( i1 -- i2 ) -1 [a,a] interval-shift ;
@@ -335,6 +319,23 @@ SYMBOL: incomparable
 : interval>= ( i1 i2 -- ? )
     swap interval<= ;
 
+: interval-mod ( i1 i2 -- i3 )
+    {
+        { [ over empty-interval eq? ] [ swap ] }
+        { [ dup empty-interval eq? ] [ ] }
+        { [ dup full-interval eq? ] [ ] }
+        [ interval-abs to>> first [ neg ] keep (a,b) ]
+    } cond
+    swap 0 [a,a] interval>= t eq? [ [0,inf] interval-intersect ] when ;
+
+: interval-rem ( i1 i2 -- i3 )
+    {
+        { [ over empty-interval eq? ] [ drop ] }
+        { [ dup empty-interval eq? ] [ nip ] }
+        { [ dup full-interval eq? ] [ nip ] }
+        [ nip interval-abs to>> first 0 swap [a,b) ]
+    } cond ;
+
 : interval-bitand-pos ( i1 i2 -- ? )
     [ to>> first ] bi@ min 0 swap [a,b] ;