| 1 | (* Roots, powers, logarithms, rounding and trigonometry, for F64: C's |
| 2 | math.h, called through the run time. *) |
| 3 | module Math; |
| 4 | |
| 5 | const pi* = 3.141592653589793; |
| 6 | |
| 7 | proc sqrt*(x: F64): F64; |
| 8 | code |
| 9 | (c-call "sqrt" double-float double-float x); |
| 10 | end; |
| 11 | |
| 12 | (* x to the power y *) |
| 13 | proc pow*(x, y: F64): F64; |
| 14 | code |
| 15 | (c-call "pow" double-float double-float x double-float y); |
| 16 | end; |
| 17 | |
| 18 | proc floor*(x: F64): F64; |
| 19 | code |
| 20 | (c-call "floor" double-float double-float x); |
| 21 | end; |
| 22 | |
| 23 | proc ceil*(x: F64): F64; |
| 24 | code |
| 25 | (c-call "ceil" double-float double-float x); |
| 26 | end; |
| 27 | |
| 28 | (* to the nearest whole number; a half away from 0 *) |
| 29 | proc round*(x: F64): F64; |
| 30 | code |
| 31 | (c-call "round" double-float double-float x); |
| 32 | end; |
| 33 | |
| 34 | proc exp*(x: F64): F64; |
| 35 | code |
| 36 | (c-call "exp" double-float double-float x); |
| 37 | end; |
| 38 | |
| 39 | proc ln*(x: F64): F64; |
| 40 | code |
| 41 | (c-call "log" double-float double-float x); |
| 42 | end; |
| 43 | |
| 44 | proc log2*(x: F64): F64; |
| 45 | code |
| 46 | (c-call "log2" double-float double-float x); |
| 47 | end; |
| 48 | |
| 49 | proc log10*(x: F64): F64; |
| 50 | code |
| 51 | (c-call "log10" double-float double-float x); |
| 52 | end; |
| 53 | |
| 54 | proc sin*(x: F64): F64; |
| 55 | code |
| 56 | (c-call "sin" double-float double-float x); |
| 57 | end; |
| 58 | |
| 59 | proc cos*(x: F64): F64; |
| 60 | code |
| 61 | (c-call "cos" double-float double-float x); |
| 62 | end; |
| 63 | |
| 64 | proc tan*(x: F64): F64; |
| 65 | code |
| 66 | (c-call "tan" double-float double-float x); |
| 67 | end; |
| 68 | |
| 69 | (* from -pi/2 to pi/2 *) |
| 70 | proc asin*(x: F64): F64; |
| 71 | code |
| 72 | (c-call "asin" double-float double-float x); |
| 73 | end; |
| 74 | |
| 75 | (* from 0 to pi *) |
| 76 | proc acos*(x: F64): F64; |
| 77 | code |
| 78 | (c-call "acos" double-float double-float x); |
| 79 | end; |
| 80 | |
| 81 | (* from -pi/2 to pi/2 *) |
| 82 | proc atan*(x: F64): F64; |
| 83 | code |
| 84 | (c-call "atan" double-float double-float x); |
| 85 | end; |
| 86 | |
| 87 | (* the angle of (x, y) from the x axis, from -pi to pi *) |
| 88 | proc atan2*(y, x: F64): F64; |
| 89 | code |
| 90 | (c-call "atan2" double-float double-float y double-float x); |
| 91 | end; |
| 92 | |
| 93 | (* the bits of x, as IEEE 754 lays them out *) |
| 94 | proc bits*(x: F64): U64; |
| 95 | begin |
| 96 | return ^U64(ADR(x))^; |
| 97 | end; |
| 98 | |
| 99 | (* the F64 whose bits are u *) |
| 100 | proc fromBits*(u: U64): F64; |
| 101 | begin |
| 102 | return ^F64(ADR(u))^; |
| 103 | end; |
| 104 | |
| 105 | (* a times b: the low 64 bits of the product, and the high *) |
| 106 | proc mul*(a, b: U64): (U64, U64); |
| 107 | var al, ah, bl, bh, lo, mid1, mid2, hi, carry: U64; |
| 108 | begin |
| 109 | (* by halves of 32 bits, as on paper *) |
| 110 | al := a & 0FFFFFFFFH; ah := a shr 32; |
| 111 | bl := b & 0FFFFFFFFH; bh := b shr 32; |
| 112 | lo := al * bl; |
| 113 | mid1 := ah * bl; |
| 114 | mid2 := al * bh; |
| 115 | hi := ah * bh; |
| 116 | carry := ((lo shr 32) + (mid1 & 0FFFFFFFFH) + (mid2 & 0FFFFFFFFH)) shr 32; |
| 117 | return a * b, hi + (mid1 shr 32) + (mid2 shr 32) + carry; |
| 118 | end; |
| 119 | |
| 120 | end Math. |