math: cleaner "2/" speedup by using custom inlining.

db4
John Benediktsson 2012-09-10 17:59:03 -07:00
parent 36fb890be2
commit a4f264509d
2 changed files with 29 additions and 17 deletions

View File

@ -110,17 +110,35 @@ IN: compiler.tree.propagation.transforms
: 2^? ( #call -- ? ) : 2^? ( #call -- ? )
in-d>> first value-info literal>> 1 eq? ; in-d>> first value-info literal>> 1 eq? ;
\ shift [ : shift-2^ ( -- quot )
2^? [ cell-bits tag-bits get - 1 -
cell-bits tag-bits get - 1 - '[
'[ integer>fixnum dup 0 < [ 2drop 0 ] [
integer>fixnum dup 0 < [ 2drop 0 ] [ dup _ < [ fixnum-shift ] [
dup _ < [ fixnum-shift ] [ fixnum-shift
fixnum-shift
] if
] if ] if
] ] if
] [ f ] if ] ;
! Speeds up 2/
: 2/? ( #call -- ? )
in-d>> second value-info literal>> -1 eq? ;
: shift-2/ ( -- quot )
[
{
{ [ over fixnum? ] [ fixnum-shift ] }
{ [ over bignum? ] [ bignum-shift ] }
[ drop \ shift no-method ]
} cond
] ;
\ shift [
{
{ [ dup 2^? ] [ drop shift-2^ ] }
{ [ dup 2/? ] [ drop shift-2/ ] }
[ drop f ]
} cond
] "custom-inlining" set-word-prop ] "custom-inlining" set-word-prop
{ /i fixnum/i fixnum/i-fast bignum/i } [ { /i fixnum/i fixnum/i-fast bignum/i } [

View File

@ -66,13 +66,7 @@ ERROR: log2-expects-positive x ;
dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
: zero? ( x -- ? ) 0 number= ; inline : zero? ( x -- ? ) 0 number= ; inline
: 2/ ( x -- y ) -1 shift ; inline
! the following lines are necessary because the "-1 shift"
! definition doesn't (yet) compile as nicely...
GENERIC: 2/ ( x -- y ) foldable
M: bignum 2/ -1 bignum-shift ; inline
M: fixnum 2/ -1 fixnum-shift ; inline
: sq ( x -- y ) dup * ; inline : sq ( x -- y ) dup * ; inline
: neg ( x -- -x ) -1 * ; inline : neg ( x -- -x ) -1 * ; inline
: sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline