math.intervals: improve interval-bitand.

clean-macosx-x86-64
John Benediktsson 2019-10-31 10:27:17 -07:00
parent 29ebfe5129
commit bccdb5419b
2 changed files with 37 additions and 31 deletions

View File

@ -398,6 +398,12 @@ commutative-ops [
{ t } [ -1/0. 1/0. [ full-interval interval-contains? ] bi@ and ] unit-test { t } [ -1/0. 1/0. [ full-interval interval-contains? ] bi@ and ] unit-test
! Interval bitand
${ 0 0xaf [a,b] } [ 0 0xff [a,b] 0 0xaf [a,b] interval-bitand ] unit-test
${ -0x100 -10 [a,b] } [ -0xff -1 [a,b] -0xaf -10 [a,b] interval-bitand ] unit-test
${ -0x100 10 [a,b] } [ -0xff 1 [a,b] -0xaf 10 [a,b] interval-bitand ] unit-test
${ 0 0xff [a,b] } [ -0xff -1 [a,b] 0 0xff [a,b] interval-bitand ] unit-test
! Interval bitor ! Interval bitor
{ 1/0. } [ 1/0. bit-weight ] unit-test { 1/0. } [ 1/0. bit-weight ] unit-test
{ 1/0. } [ -1/0. bit-weight ] unit-test { 1/0. } [ -1/0. bit-weight ] unit-test

View File

@ -374,48 +374,23 @@ SYMBOL: incomparable
[ nip (rem-range) ] [ nip (rem-range) ]
} cond ; } cond ;
: interval-bitand-pos ( i1 i2 -- ? )
[ to>> first ] bi@ min 0 swap [a,b] ;
: interval-bitand-neg ( i1 i2 -- ? )
dup from>> first 0 < [ drop ] [ nip ] if
0 swap to>> first [a,b] ;
: interval-nonnegative? ( i -- ? ) : interval-nonnegative? ( i -- ? )
from>> first 0 >= ; from>> first 0 >= ;
: interval-negative? ( interval -- ? ) : interval-negative? ( interval -- ? )
to>> first 0 < ; to>> first 0 < ;
: interval-bitand ( i1 i2 -- i3 )
! Inaccurate.
[
{
{
[ 2dup [ interval-nonnegative? ] both? ]
[ interval-bitand-pos ]
}
{
[ 2dup [ interval-nonnegative? ] either? ]
[ interval-bitand-neg ]
}
[ 2drop [-inf,inf] ]
} cond
] do-empty-interval ;
<PRIVATE <PRIVATE
! Return the weight of the MSB. For signed numbers, this does not mean the sign ! Return the weight of the MSB. For signed numbers, this does
! bit. ! not mean the sign bit.
: bit-weight ( n -- m ) : bit-weight ( n -- m )
dup [ -1/0. = ] [ 1/0. = ] bi or dup [ -1/0. = ] [ 1/0. = ] bi or
[ drop 1/0. ] [ drop 1/0. ]
[ dup 0 > [ 1 + ] [ neg ] if next-power-of-2 ] if ; [ dup 0 > [ 1 + ] [ neg ] if next-power-of-2 ] if ;
: bounds ( interval -- lower upper ) GENERIC: interval-bounds ( interval -- lower upper )
{ M: full-interval interval-bounds drop -1/0. 1/0. ;
{ full-interval [ -1/0. 1/0. ] } M: interval interval-bounds interval>points [ first ] bi@ ;
[ interval>points [ first ] bi@ ]
} case ;
: min-lower-bound ( i1 i2 -- n ) : min-lower-bound ( i1 i2 -- n )
[ from>> first ] bi@ min ; [ from>> first ] bi@ min ;
@ -423,13 +398,38 @@ SYMBOL: incomparable
: max-lower-bound ( i1 i2 -- n ) : max-lower-bound ( i1 i2 -- n )
[ from>> first ] bi@ max ; [ from>> first ] bi@ max ;
: min-upper-bound ( i1 i2 -- n )
[ to>> first ] bi@ min ;
: max-upper-bound ( i1 i2 -- n ) : max-upper-bound ( i1 i2 -- n )
[ to>> first ] bi@ max ; [ to>> first ] bi@ max ;
: interval-bit-weight ( i1 -- n ) : interval-bit-weight ( i1 -- n )
bounds [ bit-weight ] bi@ max ; interval-bounds [ bit-weight ] bi@ max ;
PRIVATE> PRIVATE>
: interval-bitand ( i1 i2 -- i3 )
[
{
{
[ 2dup [ interval-nonnegative? ] both? ]
[ min-upper-bound 0 swap [a,b] ]
}
{
[ 2dup [ interval-nonnegative? ] either? ]
[
dup interval-nonnegative? [ nip ] [ drop ] if
to>> first 0 swap [a,b]
]
}
{
[ 2dup [ interval-negative? ] both? ]
[ [ min-lower-bound bit-weight neg ] [ min-upper-bound ] 2bi [a,b] ]
}
[ [ min-lower-bound bit-weight neg ] [ max-upper-bound ] 2bi [a,b] ]
} cond
] do-empty-interval ;
! Basic Property of bitor: bits can never be taken away. For both signed and ! Basic Property of bitor: bits can never be taken away. For both signed and
! unsigned integers this means that the number can only grow towards positive ! unsigned integers this means that the number can only grow towards positive
! infinity. Also, the significant bit range can never be larger than either of ! infinity. Also, the significant bit range can never be larger than either of