Merge branch 'master' of git://factorcode.org/git/factor into bags

db4
Daniel Ehrenberg 2010-02-15 20:48:23 -06:00
commit 99500ceb92
7 changed files with 87 additions and 75 deletions

View File

@ -329,3 +329,18 @@ TUPLE: empty-tuple ;
[ { vector } declare length>> ] [ { vector } declare length>> ]
count-unboxed-allocations count-unboxed-allocations
] unit-test ] unit-test
! Bug found while tweaking benchmark.raytracer-simd
TUPLE: point-2d { x read-only } { y read-only } ;
TUPLE: point-3d < point-2d { z read-only } ;
[ 0 ] [
[ { point-2d } declare dup point-3d? [ z>> ] [ x>> ] if ]
count-unboxed-allocations
] unit-test
[ 0 ] [
[ point-2d boa dup point-3d? [ z>> ] [ x>> ] if ]
count-unboxed-allocations
] unit-test

View File

@ -61,22 +61,28 @@ M: #push escape-analysis*
: record-tuple-allocation ( #call -- ) : record-tuple-allocation ( #call -- )
dup immutable-tuple-boa? dup immutable-tuple-boa?
[ [ in-d>> but-last ] [ out-d>> first ] bi record-allocation ] [ [ in-d>> but-last { } like ] [ out-d>> first ] bi record-allocation ]
[ record-unknown-allocation ] [ record-unknown-allocation ]
if ; if ;
: slot-offset ( #call -- n/f ) : slot-offset ( #call -- n/f )
dup in-d>> dup in-d>> second node-value-info literal>> dup [ 2 - ] when ;
[ second node-value-info literal>> ]
[ first node-value-info class>> ] 2bi : valid-slot-offset? ( slot# in -- ? )
2dup [ fixnum? ] [ tuple class<= ] bi* and [ over [
over 2 >= [ drop 2 - ] [ 2drop f ] if allocation dup [
dup array? [ bounds-check? ] [ 2drop f ] if
] [ 2drop t ] if
] [ 2drop f ] if ; ] [ 2drop f ] if ;
: unknown-slot-call ( out slot# in -- )
[ unknown-allocation ] [ drop ] [ add-escaping-value ] tri* ;
: record-slot-call ( #call -- ) : record-slot-call ( #call -- )
[ out-d>> first ] [ slot-offset ] [ in-d>> first ] tri over [ out-d>> first ] [ slot-offset ] [ in-d>> first ] tri
2dup valid-slot-offset?
[ [ record-slot-access ] [ copy-slot-value ] 3bi ] [ [ record-slot-access ] [ copy-slot-value ] 3bi ]
[ [ unknown-allocation ] [ drop ] [ add-escaping-value ] tri* ] [ unknown-slot-call ]
if ; if ;
M: #call escape-analysis* M: #call escape-analysis*

View File

@ -1,4 +1,4 @@
! Copyright (C) 2005, 2009 Slava Pestov. ! Copyright (C) 2005, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel math sorting words parser io summary USING: accessors kernel math sorting words parser io summary
quotations sequences prettyprint continuations effects quotations sequences prettyprint continuations effects
@ -108,5 +108,5 @@ PRIVATE>
: word-timing. ( -- ) : word-timing. ( -- )
word-timing get word-timing get
>alist [ 1000000 /f ] assoc-map sort-values >alist [ 1,000,000,000 /f ] assoc-map sort-values
simple-table. ; simple-table. ;

View File

@ -1,7 +1,8 @@
! Copyright (C) 2007, 2009 Daniel Ehrenberg, Slava Pestov, and Doug Coleman ! Copyright (C) 2007, 2009 Daniel Ehrenberg, Slava Pestov, and Doug Coleman
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: help.markup help.syntax kernel sequences USING: help.markup help.syntax kernel sequences
sequences.private namespaces math quotations assocs.private ; sequences.private namespaces math quotations assocs.private
sets ;
IN: assocs IN: assocs
ARTICLE: "alists" "Association lists" ARTICLE: "alists" "Association lists"
@ -90,6 +91,8 @@ ARTICLE: "assocs-values" "Transposed assoc operations"
ARTICLE: "assocs-sets" "Set-theoretic operations on assocs" ARTICLE: "assocs-sets" "Set-theoretic operations on assocs"
"It is often useful to use the keys of an associative mapping as a set, exploiting the constant or logarithmic lookup time of most implementations (" { $link "alists" } " being a notable exception)." "It is often useful to use the keys of an associative mapping as a set, exploiting the constant or logarithmic lookup time of most implementations (" { $link "alists" } " being a notable exception)."
$nl
"Set-theoretic operations:"
{ $subsections { $subsections
assoc-subset? assoc-subset?
assoc-intersect assoc-intersect
@ -98,6 +101,11 @@ ARTICLE: "assocs-sets" "Set-theoretic operations on assocs"
substitute substitute
extract-keys extract-keys
} }
"Adding elements to sets:"
{ $subsections
conjoin
conjoin-at
}
"Destructive operations:" "Destructive operations:"
{ $subsections { $subsections
assoc-union! assoc-union!

View File

@ -29,8 +29,6 @@ $nl
"Adding elements to sets:" "Adding elements to sets:"
{ $subsections { $subsections
adjoin adjoin
conjoin
conjoin-at
} }
{ $see-also member? member-eq? any? all? "assocs-sets" } ; { $see-also member? member-eq? any? all? "assocs-sets" } ;

View File

@ -1,13 +1,14 @@
! Factor port of the raytracer benchmark from ! Factor port of the raytracer benchmark from
! http://www.ffconsultancy.com/free/ray_tracer/languages.html ! http://www.ffconsultancy.com/languages/ray_tracer/index.html
USING: arrays accessors io io.files io.files.temp USING: arrays accessors io io.files io.files.temp
io.encodings.binary kernel math math.constants math.functions io.encodings.binary kernel math math.constants math.functions
math.vectors math.vectors.simd math.vectors.simd.cords math.parser math.vectors math.vectors.simd math.vectors.simd.cords
make sequences sequences.private words hints classes.struct ; math.parser make sequences words combinators ;
QUALIFIED-WITH: alien.c-types c
IN: benchmark.raytracer-simd IN: benchmark.raytracer-simd
<< SYNTAX: no-compile word t "no-compile" set-word-prop ; >>
! parameters ! parameters
! Normalized { -1 -3 2 }. ! Normalized { -1 -3 2 }.
@ -25,7 +26,7 @@ CONSTANT: levels 3
CONSTANT: size 200 CONSTANT: size 200
: delta ( -- n ) epsilon sqrt ; inline : delta ( -- n ) epsilon sqrt ; inline no-compile
TUPLE: ray { orig double-4 read-only } { dir double-4 read-only } ; TUPLE: ray { orig double-4 read-only } { dir double-4 read-only } ;
@ -35,80 +36,69 @@ TUPLE: hit { normal double-4 read-only } { lambda float read-only } ;
C: <hit> hit C: <hit> hit
GENERIC: intersect-scene ( hit ray scene -- hit )
TUPLE: sphere { center double-4 read-only } { radius float read-only } ; TUPLE: sphere { center double-4 read-only } { radius float read-only } ;
C: <sphere> sphere C: <sphere> sphere
: sphere-v ( sphere ray -- v ) : sphere-v ( sphere ray -- v ) [ center>> ] [ orig>> ] bi* v- ; inline no-compile
[ center>> ] [ orig>> ] bi* v- ; inline
: sphere-b ( v ray -- b ) : sphere-b ( v ray -- b ) dir>> v. ; inline no-compile
dir>> v. ; inline
: sphere-d ( sphere b v -- d ) : sphere-d ( sphere b v -- d ) [ radius>> sq ] [ sq ] [ norm-sq ] tri* - + ; inline no-compile
[ radius>> sq ] [ sq ] [ norm-sq ] tri* - + ; inline
: -+ ( x y -- x-y x+y ) : -+ ( x y -- x-y x+y ) [ - ] [ + ] 2bi ; inline no-compile
[ - ] [ + ] 2bi ; inline
: sphere-t ( b d -- t ) : sphere-t ( b d -- t )
-+ dup 0.0 < -+ dup 0.0 <
[ 2drop 1/0. ] [ [ [ 0.0 > ] keep ] dip ? ] if ; inline [ 2drop 1/0. ] [ [ [ 0.0 > ] keep ] dip ? ] if ; inline no-compile
: sphere-b&v ( sphere ray -- b v ) : sphere-b&v ( sphere ray -- b v )
[ sphere-v ] [ nip ] 2bi [ sphere-v ] [ nip ] 2bi
[ sphere-b ] [ drop ] 2bi ; inline [ sphere-b ] [ drop ] 2bi ; inline no-compile
: ray-sphere ( sphere ray -- t ) : ray-sphere ( sphere ray -- t )
[ drop ] [ sphere-b&v ] 2bi [ drop ] [ sphere-b&v ] 2bi
[ drop ] [ sphere-d ] 3bi [ drop ] [ sphere-d ] 3bi
dup 0.0 < [ 3drop 1/0. ] [ sqrt sphere-t nip ] if ; inline dup 0.0 < [ 3drop 1/0. ] [ sqrt sphere-t nip ] if ; inline no-compile
: if-ray-sphere ( hit ray sphere quot -- hit ) : if-ray-sphere ( hit ray sphere quot: ( hit ray sphere l -- hit ) -- hit )
#! quot: hit ray sphere l -- hit
[ [
[ ] [ swap ray-sphere nip ] [ 2drop lambda>> ] 3tri [ ] [ swap ray-sphere nip ] [ 2drop lambda>> ] 3tri
[ drop ] [ < ] 2bi [ drop ] [ < ] 2bi
] dip [ 3drop ] if ; inline ] dip [ 3drop ] if ; inline no-compile
: sphere-n ( ray sphere l -- n ) : sphere-n ( ray sphere l -- n )
[ [ orig>> ] [ dir>> ] bi ] [ center>> ] [ ] tri* [ [ orig>> ] [ dir>> ] bi ] [ center>> ] [ ] tri*
swap [ v*n ] dip v- v+ ; inline swap [ v*n ] dip v- v+ ; inline no-compile
M: sphere intersect-scene ( hit ray sphere -- hit )
[ [ sphere-n normalize ] keep <hit> nip ] if-ray-sphere ;
HINTS: M\ sphere intersect-scene { hit ray sphere } ;
TUPLE: group < sphere { objs array read-only } ; TUPLE: group < sphere { objs array read-only } ;
: <group> ( objs bound -- group ) : <group> ( objs bound -- group )
[ center>> ] [ radius>> ] bi rot group boa ; inline swap [ [ center>> ] [ radius>> ] bi ] dip group boa ; inline no-compile
: make-group ( bound quot -- ) : make-group ( bound quot -- )
swap [ { } make ] dip <group> ; inline swap [ { } make ] dip <group> ; inline no-compile
M: group intersect-scene ( hit ray group -- hit ) : intersect-scene ( hit ray scene -- hit )
[ drop objs>> [ intersect-scene ] with each ] if-ray-sphere ; {
{ [ dup group? ] [ [ drop objs>> [ intersect-scene ] with each ] if-ray-sphere ] }
HINTS: M\ group intersect-scene { hit ray group } ; { [ dup sphere? ] [ [ [ sphere-n normalize ] keep <hit> nip ] if-ray-sphere ] }
} cond ; inline recursive no-compile
CONSTANT: initial-hit T{ hit f double-4{ 0.0 0.0 0.0 0.0 } 1/0. } CONSTANT: initial-hit T{ hit f double-4{ 0.0 0.0 0.0 0.0 } 1/0. }
: initial-intersect ( ray scene -- hit ) : initial-intersect ( ray scene -- hit )
[ initial-hit ] 2dip intersect-scene ; inline [ initial-hit ] 2dip intersect-scene ; inline no-compile
: ray-o ( ray hit -- o ) : ray-o ( ray hit -- o )
[ [ orig>> ] [ normal>> delta v*n ] bi* ] [ [ orig>> ] [ normal>> delta v*n ] bi* ]
[ [ dir>> ] [ lambda>> ] bi* v*n ] [ [ dir>> ] [ lambda>> ] bi* v*n ]
2bi v+ v+ ; inline 2bi v+ v+ ; inline no-compile
: sray-intersect ( ray scene hit -- ray ) : sray-intersect ( ray scene hit -- ray )
swap [ ray-o light vneg <ray> ] dip initial-intersect ; inline swap [ ray-o light vneg <ray> ] dip initial-intersect ; inline no-compile
: ray-g ( hit -- g ) normal>> light v. ; inline : ray-g ( hit -- g ) normal>> light v. ; inline no-compile
: cast-ray ( ray scene -- g ) : cast-ray ( ray scene -- g )
2dup initial-intersect dup lambda>> 1/0. = [ 2dup initial-intersect dup lambda>> 1/0. = [
@ -116,66 +106,61 @@ CONSTANT: initial-hit T{ hit f double-4{ 0.0 0.0 0.0 0.0 } 1/0. }
] [ ] [
[ sray-intersect lambda>> 1/0. = ] keep swap [ sray-intersect lambda>> 1/0. = ] keep swap
[ ray-g neg ] [ drop 0.0 ] if [ ray-g neg ] [ drop 0.0 ] if
] if ; inline ] if ; inline no-compile
: create-center ( c r d -- c2 ) : create-center ( c r d -- c2 )
[ 3.0 12.0 sqrt / * ] dip n*v v+ ; inline [ 3.0 12.0 sqrt / * ] dip n*v v+ ; inline no-compile
DEFER: create ( level c r -- scene ) DEFER: create ( level c r -- scene )
: create-step ( level c r d -- scene ) : create-step ( level c r d -- scene )
over [ create-center ] dip 2.0 / [ 1 - ] 2dip create ; over [ create-center ] dip 2.0 / [ 1 - ] 2dip create ;
: create-offsets ( quot -- ) CONSTANT: create-offsets
{ {
double-4{ -1.0 1.0 -1.0 0.0 } double-4{ -1.0 1.0 -1.0 0.0 }
double-4{ 1.0 1.0 -1.0 0.0 } double-4{ 1.0 1.0 -1.0 0.0 }
double-4{ -1.0 1.0 1.0 0.0 } double-4{ -1.0 1.0 1.0 0.0 }
double-4{ 1.0 1.0 1.0 0.0 } double-4{ 1.0 1.0 1.0 0.0 }
} swap each ; inline }
: create-bound ( c r -- sphere ) 3.0 * <sphere> ; : create-bound ( c r -- sphere ) 3.0 * <sphere> ;
: create-group ( level c r -- scene ) : create-group ( level c r -- scene )
2dup create-bound [ 2dup create-bound [
2dup <sphere> , 2dup <sphere> ,
[ [ 3dup ] dip create-step , ] create-offsets 3drop create-offsets [ create-step , ] with with with each
] make-group ; ] make-group ;
: create ( level c r -- scene ) : create ( level c r -- scene )
pick 1 = [ <sphere> nip ] [ create-group ] if ; pick 1 = [ <sphere> nip ] [ create-group ] if ;
: ss-point ( dx dy -- point ) : ss-point ( dx dy -- point )
[ oversampling /f ] bi@ 0.0 0.0 double-4-boa ; [ oversampling /f ] bi@ 0.0 0.0 double-4-boa ; inline no-compile
: ss-grid ( -- ss-grid ) : ray-pixel ( scene point -- ray-grid )
oversampling iota [ oversampling iota [ ss-point ] with map ] map ; [ 0.0 ] 2dip
oversampling iota [
oversampling iota [
ss-point v+ normalize
double-4{ 0.0 0.0 -4.0 0.0 } swap <ray>
swap cast-ray +
] with with with each
] with with each ; inline no-compile
: ray-grid ( point ss-grid -- ray-grid ) : ray-trace ( scene -- grid )
[ size iota <reversed> [
[ v+ normalize double-4{ 0.0 0.0 -4.0 0.0 } swap <ray> ] with map
] with map ;
: ray-pixel ( scene point -- n )
ss-grid ray-grid [ 0.0 ] 2dip
[ [ swap cast-ray + ] with each ] with each ;
: pixel-grid ( -- grid )
size iota reverse [
size iota [ size iota [
[ size 0.5 * - ] bi@ swap size [ size 0.5 * - ] bi@ swap size
0.0 double-4-boa 0.0 double-4-boa ray-pixel
] with map ] with with map
] map ; ] with map ;
: pgm-header ( w h -- ) : pgm-header ( w h -- )
"P5\n" % swap # " " % # "\n255\n" % ; "P5\n" % swap # " " % # "\n255\n" % ;
: pgm-pixel ( n -- ) 255 * 0.5 + >fixnum , ; : pgm-pixel ( n -- ) 255 * 0.5 + >fixnum , ;
: ray-trace ( scene -- pixels )
pixel-grid [ [ ray-pixel ] with map ] with map ;
: run ( -- string ) : run ( -- string )
levels double-4{ 0.0 -1.0 0.0 0.0 } 1.0 create ray-trace [ levels double-4{ 0.0 -1.0 0.0 0.0 } 1.0 create ray-trace [
size size pgm-header size size pgm-header