(* Roots, powers, logarithms, rounding and trigonometry, for F64: C's math.h, called through the run time. *) module Math; const pi* = 3.141592653589793; proc sqrt*(x: F64): F64; code (c-call "sqrt" double-float double-float x); end; (* x to the power y *) proc pow*(x, y: F64): F64; code (c-call "pow" double-float double-float x double-float y); end; proc floor*(x: F64): F64; code (c-call "floor" double-float double-float x); end; proc ceil*(x: F64): F64; code (c-call "ceil" double-float double-float x); end; (* to the nearest whole number; a half away from 0 *) proc round*(x: F64): F64; code (c-call "round" double-float double-float x); end; proc exp*(x: F64): F64; code (c-call "exp" double-float double-float x); end; proc ln*(x: F64): F64; code (c-call "log" double-float double-float x); end; proc log2*(x: F64): F64; code (c-call "log2" double-float double-float x); end; proc log10*(x: F64): F64; code (c-call "log10" double-float double-float x); end; proc sin*(x: F64): F64; code (c-call "sin" double-float double-float x); end; proc cos*(x: F64): F64; code (c-call "cos" double-float double-float x); end; proc tan*(x: F64): F64; code (c-call "tan" double-float double-float x); end; (* from -pi/2 to pi/2 *) proc asin*(x: F64): F64; code (c-call "asin" double-float double-float x); end; (* from 0 to pi *) proc acos*(x: F64): F64; code (c-call "acos" double-float double-float x); end; (* from -pi/2 to pi/2 *) proc atan*(x: F64): F64; code (c-call "atan" double-float double-float x); end; (* the angle of (x, y) from the x axis, from -pi to pi *) proc atan2*(y, x: F64): F64; code (c-call "atan2" double-float double-float y double-float x); end; (* the bits of x, as IEEE 754 lays them out *) proc bits*(x: F64): U64; begin return ^U64(ADR(x))^; end; (* the F64 whose bits are u *) proc fromBits*(u: U64): F64; begin return ^F64(ADR(u))^; end; (* a times b: the low 64 bits of the product, and the high *) proc mul*(a, b: U64): (U64, U64); var al, ah, bl, bh, lo, mid1, mid2, hi, carry: U64; begin (* by halves of 32 bits, as on paper *) al := a & 0FFFFFFFFH; ah := a shr 32; bl := b & 0FFFFFFFFH; bh := b shr 32; lo := al * bl; mid1 := ah * bl; mid2 := al * bh; hi := ah * bh; carry := ((lo shr 32) + (mid1 & 0FFFFFFFFH) + (mid2 & 0FFFFFFFFH)) shr 32; return a * b, hi + (mid1 shr 32) + (mid2 shr 32) + carry; end; end Math.