summaryrefslogtreecommitdiff
path: root/lib/std/fltparse.myr
blob: 8214f140c9b580d7620f56255095b3c7dec7e1a9 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use "alloc"
use "bigint"
use "chartype"
use "chomp"
use "extremum"
use "fltbits"
use "hasprefix"
use "intparse"
use "option"
use "utf"
use "striter"

pkg std =
	const flt64parse	: (str : byte[:] -> std.option(flt64))
	const flt32parse	: (str : byte[:] -> std.option(flt32))
;;

type parseresult = union
	`Flt (int, std.bigint#, int16)
	`Inf int
	`Nan int
	`Badflt
;;

const flt64parse = {str
	var val

	match fltparse(str)
	| `Badflt:	-> `std.None
	| `Inf sign:	-> `std.Some ((sign : flt64) * std.flt64inf())
	| `Nan sign:	-> `std.Some ((sign : flt64) * std.flt64nan())
	| `Flt (sign, mant, exp):
		val = toflt(sign, mant, exp, &lim64)
		std.bigfree(mant)
		-> `std.Some val
	;;
}

const flt32parse = {str
	var val

	match fltparse(str)
	| `Badflt:	-> `std.None
	| `Inf sign:	-> `std.Some ((sign : flt32) * std.flt32inf())
	| `Nan sign:	-> `std.Some ((sign : flt32) * std.flt32nan())
	| `Flt (sign, mant, exp):
		val = toflt(sign, mant, exp, &lim32)
		std.bigfree(mant)
		-> `std.Some (val : flt32)
	;;
}

const fltparse = {str
	var sign, exp, mant, expinc, i

	if std.chomp(&str, "-")
		sign = -1
	else
		sign = 1
		std.chomp(&str, "+")
	;;

	match str
	| "inf":	-> `Inf sign
	| "Inf":	-> `Inf sign
	| "nan":	-> `Nan sign
	| "NaN":	-> `Nan sign
	| "":	-> `Badflt
	| _:	/* nothing */
	;;

	i = 0
	exp = 0
	expinc = 0
	mant = std.mkbigint(0)
	for var c = std.decode(str[i:]); c == '0'; c = std.decode(str[i:])
		i++
	;;
	for var c = std.decode(str[i:]); fltchar(c); c = std.decode(str[i:])
		i++
		if c == '_'
			continue
		elif c == '.'
			/* fail if we've already seen the '.' */
			if expinc != 0
				goto error
			;;
			expinc = -1
		elif std.isdigit(c)
			exp += expinc
			std.bigmuli(mant, 10)
			std.bigaddi(mant, std.charval(c, 10))
		else
			goto error
		;;
	;;
	
	if std.hasprefix(str[i:], "e") || std.hasprefix(str[i:], "E")
		/* intparse doesn't accept '+', so if we have 'e+', consume the '+ */
		if std.hasprefix(str[i+1:], "+")
			i++
		;;
		match std.intparsebase(str[i+1:], 10)
		| `std.Some n:	exp += n
		| `std.None:	goto error
		;;
		i = str.len
	;;
	if i != str.len
		goto error
	;;

	-> `Flt (sign, mant, exp)

:error
	std.bigfree(mant)
	-> `Badflt
}

const fltchar = {c
	-> std.isdigit(c) || c == '.' || c == '_'
}

type lim = struct
	minsig	: uint64
	maxsig	: uint64
	loshift	: uint64
	nextinc	: uint64
	minexp	: int16
	maxexp	: int16
	sigbits	: int16
;;

const lim64 : lim = [
	.minsig=0x10000000000000ul,
	.maxsig=0x20000000000000ul,
	.minexp=-1022 - 52,
	.maxexp=1023 - 52,
	.sigbits=53,
	.nextinc=1,
	.loshift=0
]

const lim32 : lim = [
	.minsig=0x400000,
	.maxsig=0x800000,
	.minexp=-126-22,
	.maxexp=127-22,
	.sigbits=22,
	.nextinc=0x20000000,
	.loshift=30
]

const toflt = {sign, mant, exp, lim
	/* if it's all zero, just return 0 */
	if std.bigeqi(mant, 0)
		-> (sign : flt64) * 0.0
	/* todo: add fast parsing for common cases */
	else
		-> (sign : flt64) * fallback(mant, exp, lim)
	;;
}

const fallback = {mant, exp, lim
	var u, v, k : int16, eabs
	var x, r, xprime, rprime
	var f

	u = mant
	k = 0
	v = std.mkbigint(1)
	x = std.mkbigint(0)
	r = std.mkbigint(0)
	eabs = std.abs(exp)
	if exp >= 0
		/* can be optimized */
		mulexp(u, eabs, 10)
	else
		mulexp(v, eabs, 10)
	;;

	estimate(u, v, &k, lim)

	while true
		(xprime, rprime) = std.bigdivmod(u, v)
		std.bigsteal(x, xprime)
		std.bigsteal(r, rprime)
		if k == lim.minexp 
			if std.biggei(x, lim.minsig) && std.biglei(x, lim.maxsig)
				break
			else
				f = denormal(x, v, r, lim)
				goto done
			;;
		elif k > lim.maxexp
			f = std.flt64inf()
			goto done
		;;
		if std.biglti(x, lim.minsig)
			std.bigmuli(u, 2)
			k--
		elif std.biggti(x, lim.maxsig)
			std.bigmuli(v, 2)
			k++
		else
			break
		;;
	;;
	f = assemble(u, v, r, k, lim)
:done
	std.bigfree(v)
	std.bigfree(x)
	std.bigfree(r)
	-> f
}

const estimate = {u, v, k, lim
	var log2u, log2v, log2rat
	var ushift, vshift
	var targ

	targ = lim.sigbits
	log2u = std.bigbitcount(u)
	log2v = std.bigbitcount(v)
	ushift = 0
	vshift = 0
	log2rat = (log2u - log2v : int16)
	/* 
	if we deal with denormals, we just punt to the 'k == minexp' test
	and take the slow path.
	*/
	if log2rat < targ - 1
		ushift = std.clamp(targ - log2rat - 1, lim.minexp, lim.maxexp)
		k# -= ushift
	elif log2rat > targ + 1
		vshift = std.clamp(log2rat - targ + 1, lim.minexp, lim.maxexp)
		k# += vshift
	;;
	std.bigshli(u, ushift)
	std.bigshli(v, vshift)
}

const assemble = {u, v, r, k, lim
	var z, diff

	std.bigdiv(u, v)
	diff = std.bigdup(v)
	std.bigsub(diff, r)
	z = mkflt(u, k)

	match std.bigcmp(r, diff)
	| `std.Before:	/* nothing */
	| `std.After:	z = nextfloat(z, lim)
	| `std.Equal:
		if !std.bigiseven(u)
			z = nextfloat(z, lim)
		;;
	;;
	std.bigfree(diff)
	-> z
}

const nextfloat = {z, lim
	var sign, mant, exp
	var za

	(sign, exp, mant) = std.flt64explode(z)
	if std.abs((mant : int64) - (1l << 52) - 1) < (lim.nextinc : int64)
		mant = 0
		exp++
	else
		mant += lim.nextinc
	;;
	za = std.flt64assem(sign, exp, mant)
	-> za
}

const mulexp = {val, exp, pow
	while exp > 0
		exp--
		std.bigmuli(val, pow)
	;;
}

const denormal = {x, v, rem, lim
	var m, z

	if x.dig.len == 0
		m = 0
	elif x.dig.len == 2
		m = (x.dig[0] : uint64) 
		m |= (x.dig[1] : uint64) << 32
	else
		m = (x.dig[0] : uint64) << lim.loshift
	;;
	z = std.flt64frombits(m)

	match std.bigcmp(rem, v)
	| `std.Before:	/* nothing */
	| `std.After:	z = nextfloat(z, lim)
	| `std.Equal:
		if !std.bigiseven(x)
			z = nextfloat(z, lim)
		;;
	;;
	-> z
}

const mkflt = {mant, exp
	var m, e
	if mant.dig.len == 2
		/* flt64: guaranteed to be in the range 2^52..2^53 */
		m = (mant.dig[0] : uint64) 
		m |= (mant.dig[1] : uint64) << 32
	else
		/* flt32: 2^22..2^23, so adjust into flt64 range */
		m = (mant.dig[0] : uint64) << 30
		exp -= 30
	;;
	m &= (1<<52) - 1

	e = (exp : uint64)
	e += 1023 + 52 /* exponent bias */
	e &= 0x7ff
	e <<= 52

	-> std.flt64frombits(m | e)

}