summaryrefslogtreecommitdiff
path: root/lib/math/fpmath.myr
blob: ea6426ed4cf90e05bb6e34987ff9bb17f9fc0078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std
use "impls"

pkg math =
	trait fpmath @f =

		/* atan-impl */
		atan : (x : @f -> @f)
		atan2 : (y : @f, x : @f -> @f)

		/* exp-impl */
		exp : (x : @f -> @f)
		expm1 : (x : @f -> @f)

		/* fma-impl */
		fma : (x : @f, y : @f, z : @f -> @f)

		/* log-impl */
		log : (x : @f -> @f)
		log1p : (x : @f -> @f)

		/* poly-impl */
		horner_poly : (x : @f, a : @f[:] -> @f)
		horner_polyu : (x : @f, a : @u[:] -> @f)

		/* pown-impl */
		pown : (x : @f, n : @i -> @f)
		rootn : (x : @f, n : @u -> @f)

		/* powr-impl */
		powr : (x : @f, y : @f -> @f)

		/* scale2-impl */
		scale2 : (x : @f, m : @i -> @f)

		/* sin-impl */
		sin : (x : @f -> @f)
		cos : (x : @f -> @f)
		sincos : (x : @f -> (@f, @f))

		/* sqrt-impl */
		sqrt : (x : @f -> @f)

		/* sum-impl */
		kahan_sum : (a : @f[:] -> @f)
		priest_sum : (a : @f[:] -> @f)

		/* tan-impl */
		tan : (x : @f -> @f)
		cot : (x : @f -> @f)

		/* trunc-impl */
		trunc : (x : @f -> @f)
		ceil  : (x : @f -> @f)
		floor : (x : @f -> @f)
	;;

	trait roundable @f -> @i =
		/* round-impl */
		rn : (x : @f -> @i)
	;;

	impl std.equatable flt32
	impl std.equatable flt64
	impl roundable flt64 -> int64
	impl roundable flt32 -> int32
	impl fpmath flt32
	impl fpmath flt64
;;

/*
   We consider two floating-point numbers equal if their bits are
   equal. This does not treat NaNs specially: two distinct NaNs may
   compare equal, or they may compare distinct (if they arise from
   different bit patterns).

   Additionally, +0.0 and -0.0 compare differently.
 */
impl std.equatable flt32 =
	eq = {a : flt32, b : flt32; -> std.flt32bits(a) == std.flt32bits(b)}
;;

impl std.equatable flt64 =
	eq = {a : flt64, b : flt64; -> std.flt64bits(a) == std.flt64bits(b)}
;;

impl roundable flt32 -> int32 =
	rn = {x : flt32; -> rn32(x) }
;;

impl roundable flt64 -> int64 =
	rn = {x : flt64; -> rn64(x) }
;;

impl fpmath flt32 =
	atan = {x; -> atan32(x)}
	atan2 = {y, x; -> atan232(y, x)}

	fma = {x, y, z; -> fma32(x, y, z)}

	exp = {x; -> exp32(x)}
	expm1 = {x; -> expm132(x)}

	log = {x; -> log32(x)}
	log1p = {x; -> log1p32(x)}

	horner_poly = {x, a; -> horner_poly32(x, a)}
	horner_polyu = {x, a; -> horner_polyu32(x, a)}

	pown = {x, n; -> pown32(x, n)}
	rootn = {x, q; -> rootn32(x, q)}

	powr = {x, y; -> powr32(x, y)}

	scale2 = {x, m; -> scale232(x, m)}

	sin = {x; -> sin32(x)}
	cos = {x; -> cos32(x)}
	sincos = {x; -> sincos32(x)}

	sqrt = {x; -> sqrt32(x)}

	kahan_sum = {l; -> kahan_sum32(l) }
	priest_sum = {l; -> priest_sum32(l) }

	tan = {x; -> tan32(x)}
	cot = {x; -> cot32(x)}

	trunc = {x; -> trunc32(x)}
	floor = {x; -> floor32(x)}
	ceil  = {x; -> ceil32(x)}

;;

impl fpmath flt64 =
	atan = {x; -> atan64(x)}
	atan2 = {y, x; -> atan264(y, x)}

	fma = {x, y, z; -> fma64(x, y, z)}

	exp = {x; -> exp64(x)}
	expm1 = {x; -> expm164(x)}

	log = {x; -> log64(x)}
	log1p = {x; -> log1p64(x)}

	horner_poly = {x, a; -> horner_poly64(x, a)}
	horner_polyu = {x, a; -> horner_polyu64(x, a)}

	pown = {x, n; -> pown64(x, n)}
	rootn = {x, q; -> rootn64(x, q)}

	powr = {x, y; -> powr64(x, y)}

	scale2 = {x, m; -> scale264(x, m)}

	sin = {x; -> sin64(x)}
	cos = {x; -> cos64(x)}
	sincos = {x; -> sincos64(x)}

	sqrt = {x; -> sqrt64(x)}

	kahan_sum = {l; -> kahan_sum64(l) }
	priest_sum = {l; -> priest_sum64(l) }

	tan = {x; -> tan64(x)}
	cot = {x; -> cot64(x)}

	trunc = {x; -> trunc64(x)}
	floor = {x; -> floor64(x)}
	ceil  = {x; -> ceil64(x)}
;;