Files
mercury/benchmarks/progs/icfp2000_par/examples/util.ins
Paul Bone ea06fd8cde Add the benchmarks directory into the main Mercury repository.
This was a seperate repository in CVS and so it missed the conversion.

benchmarks/
    As above.
2013-01-04 12:13:53 +11:00

96 lines
1.8 KiB
TeX

/* util.ins
*
* COPYRIGHT (c) 2000 Bell Labs, Lucent Techbnologies.
*
* Various GML utility functions.
*/
#ifndef _UTIL_INS_
#define _UTIL_INS_
%%%%%%%%%% include util.ins %%%%%%%%%%
{ /x } /pop % pop a stack item
{ /x x x } /dup % duplicate a stack item
% pop n items off the stack
{ /n
{ /self /i
i 1 lessi
{ }
{ /x i 1 subi self self apply }
if
} /loop
n loop loop apply
} /popn
% dot product
% ... v2 v1 dot ==> ... r
{ /v1 /v2
v1 getx v2 getx mulf
v1 gety v2 gety mulf addf
v1 getz v2 getz mulf addf
} /dot
% integer absolute value
{ /i i 0 lessi { i negi } { i } if } /absi
% floating-point absolute value
{ /f f 0.0 lessf { f negf } { f } if } /absf
% normalize
% ... v1 normalize ==> ... v2
{ /v
1.0 v v dot apply sqrt divf /s % s = sqrt(1.0/v dot v)
s v getx mulf % push s*x
s v gety mulf % push s*y
s v getz mulf % push s*z
point % make normalized vector
} /normalize
% addp
{ /v2 /v1
v1 getx v2 getx addf
v1 gety v2 gety addf
v1 getz v2 getz addf point
} /addp
% subp
{ /v2 /v1
v1 getx v2 getx subf
v1 gety v2 gety subf
v1 getz v2 getz subf point
} /subp
% mulp
{ /v2 /v1
v1 getx v2 getx mulf
v1 gety v2 gety mulf
v1 getz v2 getz mulf point
} /mulp
% negp
{ /v
v getx negf
v gety negf
v getz negf point
} /negp
% A simple pseudo-random number generator (from Graphics Gems II; p. 137)
% We have to do the computation in FP, since it overflows in integer arithmetic.
{ real 25173.0 mulf 13849.0 addf 65536.0 divf frac 65536.0 mulf floor } /random
% A random number in [0..1].
%
% seed randomf ==> f seed
{
random apply /seed
seed real 65535.0 divf seed
} /randomf
%%%%%%%%%% util.ins %%%%%%%%%%
#endif /* !_UTIL_INS_ */