summaryrefslogtreecommitdiff
path: root/lib/std/fltfmt.myr
blob: ee035520ec7f42ad96f5b4c1080882af45d9e907 (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
use "alloc"
use "bigint"
use "die"
use "extremum"
use "fltbits"
use "slpush"
use "strbuf"
use "types"
use "utf"
use "memops"

pkg std =
	pkglocal const MNormal	= 0
	pkglocal const MAbsolute = 1
	pkglocal const MRelative = 2

	pkglocal type fltparams = struct
		mode	: int
		prec	: int
	;;

	pkglocal const flt64bfmt	: (sb : strbuf#, opts : fltparams, val : flt64 -> void)
	pkglocal const flt32bfmt	: (sb : strbuf#, opts : fltparams, val : flt32 -> void)
;;

const Dblbias = 1023
const Fltbias = 127

const flt64bfmt = {sb, opts, val
	var isneg, exp, mant

	if isnan(val)
		sbputs(sb, "NaN")
		-> void
	;;

	(isneg, exp, mant) = flt64explode(val)

	if exp > Dblbias
		if isneg
			std.sbputs(sb, "-Inf")
		else
			std.sbputs(sb, "Inf")
		;;
		-> void
	;;

	exp = max(exp, 1 - Dblbias)
	dragon4(sb, isneg, mant, exp - 52, Dblbias, opts.mode, opts.prec)
}

const flt32bfmt = {sb, opts, val
	var isneg, exp, mant

	if isnan(val)
		sbputs(sb, "NaN")
		-> void
	;;

	(isneg, exp, mant) = flt32explode(val)

	if (exp : int64) > Fltbias
		if isneg
			std.sbputs(sb, "-Inf")
		else
			std.sbputs(sb, "Inf")
		;;
		-> void
	;;

	exp = (max((exp : int64), 1 - Fltbias) : int32)
	dragon4(sb, isneg, (mant : uint64), (exp - 23 : int64), Fltbias, opts.mode, opts.prec)
}

/*
sb: output buffer
e: exponent
p: precision
f: mantissa

floating value: x = f^(e - p)
*/
const dragon4 = {sb, isneg, f, e, p, mode, cutoff
	var r, s, t, u, v, y
	var udig
	var mm, mp	/* margins above and below */
	var roundup
	var low, high
	var k
	var a, i

	if isneg
		sbputs(sb, "-")
	;;
	/* if we have zero for the mantissa, we can return early */
	if f == 0
		sbputs(sb, "0.0")
		-> void
	;;

	/* initialize */
	roundup = false
	low = false
	high = false
	u = mkbigint(0)
	r = bigshli(mkbigint(f), max(e, 0))
	s = bigshli(mkbigint(1), max(0, -e))
	mm = bigshli(mkbigint(1), max(e, 0))
	mp = bigdup(mm)

	/* fixup: unequal gaps */
	t = mkbigint(1)
	bigshli(t, p - 1)
	if bigeqi(t, f)
		bigshli(mp, 1)
		bigshli(r, 1)
		bigshli(s, 1)
	;;
	bigfree(t)

	k = 0
	while true
		/* r < ceil(s/b) */
		t = bigdup(s)
		bigaddi(t, 9)
		bigdivi(t, 10)
		match bigcmp(r, t)
		| `Before:
			k--
			bigmuli(r, 10)
			bigmuli(mm, 10)
			bigmuli(mp, 10)
		| _:
			bigfree(t)
			break
		;;
		bigfree(t)
	;;

	while true
		t = bigdup(r)
		bigshli(t, 1)
		bigadd(t, mp)
		while true
			u = bigdup(s)
			bigshli(u, 1)
			match bigcmp(t, u)
			| `Before:
				bigfree(u)
				break
			| _:
				k++
				bigmuli(s, 10)
				bigfree(u)
			;;
		;;
		if mode == MNormal
			cutoff = k
		else
			if mode == MRelative
				cutoff += k - 1
			;;
			/* common between relative and absolute */
			a = cutoff - k - 1
			y = bigdup(s)
			if a < 0
				for i = 0; i < a; i++
					bigmuli(y, 10)
				;;
			else
				for i = 0; i < -a; i++
					bigaddi(y, 9)
					bigdivi(y, 10)
				;;
			;;
			match bigcmp(y, mm)
			| `Before:	/* nothing */
			| _:
				bigfree(mm)
				mm = y
			;;
			match bigcmp(y, mp)
			| `Before:	/* nothing */
			| _:
				bigfree(mp)
				mp = y
				roundup = true
			;;
		;;
		u = bigdup(s)
		bigshli(u, 1)
		match bigcmp(t, u)
		| `Before:
			bigfree(t)
			bigfree(u)
			break
		| _:
		;;
	;;

	if k <= 0
		sbputs(sb, "0.")
		for var i = 0; i < -k; i++
			sbputs(sb, "0")
		;;
	;;
	while true
		k--

		bigmuli(r, 10)
		u = bigdup(r);
		bigdiv(u, s)

		bigmod(r, s)
		bigmuli(mm, 10)
		bigmuli(mp, 10)

		low = false
		t = bigdup(r)
		bigshli(t, 1)
		match bigcmp(t, mm)
		| `Before:	low = true
		| _:
		;;
		bigfree(t)

		/* v = 2*r */
		v = bigdup(r)
		bigshli(v, 1)

		/* t = 2*s - mp */
		t = bigdup(s)
		bigshli(t, 1)
		bigsub(t, mp)

		match bigcmp(v, t)
		| `Before: high = false;
		| `Equal: high = roundup;
		| `After: high = true;
		;;
		bigfree(v)
		bigfree(t)
		if low || high || k == cutoff
			break
		;;
		format(sb, lowdig(u), k)
		bigfree(u)
	;;

	/* format the last digit */
	udig = lowdig(u)
	if low && !high
		format(sb, udig, k)
	elif high && !low
		format(sb, udig + 1, k)
	else
		bigmuli(r, 2)
		match bigcmp(r, s)
		| `Before:	format(sb, udig, k)
		| `Equal:	format(sb, udig, k)
		| `After:	format(sb, udig + 1, k)
		;;
	;;
	k--
	while k >= -1
		format(sb, 0, k--)
	;;

	bigfree(u)
	bigfree(r)
	bigfree(s)
	bigfree(mm)
	bigfree(mp)

}

const lowdig = {u
	if u.dig.len > 0
		-> u.dig[0]
	;;
	-> 0
}

const format = {sb, d, k
	const dig = "0123456789"

	sbputb(sb, dig[d])
	if k == 0
		sbputs(sb, ".")
	;;
}