arithmetic identities

cvs
Slava Pestov 2005-08-14 05:17:25 +00:00
parent 633466f129
commit 1fa0bfc130
6 changed files with 49 additions and 43 deletions

View File

@ -73,13 +73,9 @@
- recursion is iffy; no base case needs to throw an error, and if the - recursion is iffy; no base case needs to throw an error, and if the
stack at the recursive call doesn't match up, throw an error stack at the recursive call doesn't match up, throw an error
+ sequences
- array sort
- specialized arrays
+ kernel: + kernel:
- specialized arrays
- clear "predicating" word prop when redefining words - clear "predicating" word prop when redefining words
- there is a problem with hashcodes of words and bootstrapping - there is a problem with hashcodes of words and bootstrapping
- delegating generic words with a non-standard picker - delegating generic words with a non-standard picker

View File

@ -60,3 +60,6 @@ M: compound (compile) ( word -- )
: recompile ( word -- ) : recompile ( word -- )
dup decompile compile ; dup decompile compile ;
: compile-1 ( quot -- word )
gensym [ swap define-compound ] keep dup compile execute ;

View File

@ -1,8 +1,8 @@
! Copyright (C) 2005 Slava Pestov. ! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: inference IN: inference
USING: errors hashtables kernel math math-internals sequences USING: errors generic hashtables kernel math math-internals
vectors words ; sequences vectors words ;
! A system for associating dataflow optimizers with words. ! A system for associating dataflow optimizers with words.
@ -48,15 +48,13 @@ vectors words ;
{ [ dup node-successor #ifte? ] [ node-successor dup flip-branches ] } { [ dup node-successor #ifte? ] [ node-successor dup flip-branches ] }
} define-optimizers } define-optimizers
M: #call optimize-node* ( node -- node/t ) : disjoint-eq? ( node -- ? )
{ dup node-classes swap node-in-d [ swap hash ] map-with
{ [ dup node-param not ] [ node-successor ] } 2unseq class-and null = ;
{ [ dup partial-eval? ] [ partial-eval ] }
{ [ dup optimizer-hooks ] [ optimize-hooks ] } \ eq? {
{ [ dup inlining-class ] [ inline-method ] } { [ dup disjoint-eq? ] [ [ f ] inline-literals ] }
{ [ dup optimize-predicate? ] [ optimize-predicate ] } } define-optimizers
{ [ t ] [ drop t ] }
} cond ;
! Arithmetic identities ! Arithmetic identities
SYMBOL: @ SYMBOL: @
@ -70,14 +68,21 @@ SYMBOL: @
] 2map conjunction ; ] 2map conjunction ;
: values-match? ( values template -- ? ) : values-match? ( values template -- ? )
[ @ = ] 2map [ ] subset [ eq? ] every? ; [ @ = [ drop f ] unless ] 2map [ ] subset [ eq? ] every? ;
: apply-identity? ( values identity -- ? ) : apply-identity? ( values identity -- ? )
first 2dup literals-match? >r values-match? r> and ; first 2dup literals-match? >r values-match? r> and ;
: apply-identities ( values identities -- node/f ) : find-identity ( node -- values identity )
dupd [ apply-identity? ] find-with nip dup dup node-in-d swap node-param "identities" word-prop
[ second swap dataflow-with ] [ 2drop f ] ifte ; [ dupd apply-identity? ] find nip ;
: apply-identities ( node -- node/f )
dup find-identity dup [
second swap dataflow-with [ subst-node ] keep
] [
3drop f
] ifte ;
[ + fixnum+ bignum+ float+ ] { [ + fixnum+ bignum+ float+ ] {
{ { @ 0 } [ drop ] } { { @ 0 } [ drop ] }
@ -92,8 +97,8 @@ SYMBOL: @
[ * fixnum* bignum* float* ] { [ * fixnum* bignum* float* ] {
{ { @ 1 } [ drop ] } { { @ 1 } [ drop ] }
{ { 1 @ } [ nip ] } { { 1 @ } [ nip ] }
{ { @ 0 } [ 2drop 0 ] } { { @ 0 } [ nip ] }
{ { 0 @ } [ 2drop 0 ] } { { 0 @ } [ drop ] }
{ { @ -1 } [ drop 0 swap - ] } { { @ -1 } [ drop 0 swap - ] }
{ { -1 @ } [ nip 0 swap - ] } { { -1 @ } [ nip 0 swap - ] }
} define-identities } define-identities
@ -116,19 +121,19 @@ SYMBOL: @
! } define-identities ! } define-identities
[ bitand fixnum-bitand bignum-bitand ] { [ bitand fixnum-bitand bignum-bitand ] {
{ { @ -1 } [ drop ] } { { @ -1 } [ drop ] }
{ { -1 @ } [ nip ] } { { -1 @ } [ nip ] }
{ { @ @ } [ drop ] } { { @ @ } [ drop ] }
{ { @ 0 } [ 2drop 0 ] } { { @ 0 } [ nip ] }
{ { 0 @ } [ 2drop 0 ] } { { 0 @ } [ drop ] }
} define-identities } define-identities
[ bitor fixnum-bitor bignum-bitor ] { [ bitor fixnum-bitor bignum-bitor ] {
{ { @ 0 } [ drop ] } { { @ 0 } [ drop ] }
{ { 0 @ } [ nip ] } { { 0 @ } [ nip ] }
{ { @ @ } [ drop ] } { { @ @ } [ drop ] }
{ { @ -1 } [ 2drop -1 ] } { { @ -1 } [ nip ] }
{ { -1 @ } [ 2drop -1 ] } { { -1 @ } [ drop ] }
} define-identities } define-identities
[ bitxor fixnum-bitxor bignum-bitxor ] { [ bitxor fixnum-bitxor bignum-bitxor ] {
@ -140,7 +145,7 @@ SYMBOL: @
} define-identities } define-identities
[ shift fixnum-shift bignum-shift ] { [ shift fixnum-shift bignum-shift ] {
{ { 0 @ } [ 2drop 0 ] } { { 0 @ } [ drop ] }
{ { @ 0 } [ drop ] } { { @ 0 } [ drop ] }
} define-identities } define-identities
@ -163,3 +168,14 @@ SYMBOL: @
[ eq? number= = ] { [ eq? number= = ] {
{ { @ @ } [ 2drop t ] } { { @ @ } [ 2drop t ] }
} define-identities } define-identities
M: #call optimize-node* ( node -- node/t )
{
{ [ dup node-param not ] [ node-successor ] }
{ [ dup partial-eval? ] [ partial-eval ] }
{ [ dup find-identity nip ] [ apply-identities ] }
{ [ dup optimizer-hooks ] [ optimize-hooks ] }
{ [ dup inlining-class ] [ inline-method ] }
{ [ dup optimize-predicate? ] [ optimize-predicate ] }
{ [ t ] [ drop t ] }
} cond ;

View File

@ -1,13 +1,7 @@
IN: temporary IN: temporary
USE: lists USING: compiler kernel math sequences test ;
USE: kernel
USE: math
USE: namespaces
USE: random
USE: test
USE: compiler
: sort-benchmark : sort-benchmark
[ 100000 [ 0 10000 random-int , ] times ] make-vector [ - ] sort drop ; compiled 100000 [ drop 0 10000 random-int ] map [ - ] sort drop ; compiled
[ ] [ sort-benchmark ] unit-test [ ] [ sort-benchmark ] unit-test

View File

@ -3,9 +3,6 @@ USING: compiler kernel kernel-internals lists math
math-internals test words ; math-internals test words ;
! Make sure that intrinsic ops compile to correct code. ! Make sure that intrinsic ops compile to correct code.
: compile-1 ( quot -- word )
gensym [ swap define-compound ] keep dup compile execute ;
[ 1 ] [ [[ 1 2 ]] [ 0 slot ] compile-1 ] unit-test [ 1 ] [ [[ 1 2 ]] [ 0 slot ] compile-1 ] unit-test
[ 1 ] [ [ [[ 1 2 ]] 0 slot ] compile-1 ] unit-test [ 1 ] [ [ [[ 1 2 ]] 0 slot ] compile-1 ] unit-test
[ 3 ] [ 3 1 2 cons [ [ 0 set-slot ] keep ] compile-1 car ] unit-test [ 3 ] [ 3 1 2 cons [ [ 0 set-slot ] keep ] compile-1 car ] unit-test

View File

@ -48,12 +48,12 @@ C: splitter ( first second split vector -- splitter )
dup splitter-split swap rectangle-dim dup splitter-split swap rectangle-dim
n*v divider-size 1/2 v*n v- ; n*v divider-size 1/2 v*n v- ;
: splitter-layout ( splitter -- [ a b c ] ) : splitter-layout ( splitter -- { a b c } )
[ [
dup splitter-part , dup splitter-part ,
divider-size , divider-size ,
dup rectangle-dim divider-size v- swap splitter-part v- , dup rectangle-dim divider-size v- swap splitter-part v- ,
] make-list ; ] make-vector ;
M: splitter layout* ( splitter -- ) M: splitter layout* ( splitter -- )
dup splitter-layout packed-layout ; dup splitter-layout packed-layout ;