summaryrefslogtreecommitdiff
path: root/lib/std/fltfmt.myr
blob: f047cb6618f838a5d473f131699988f15889a8b6 (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
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 const flt64bfmt	: (sb : strbuf#, val : flt64, mode : int, precision : int -> void)
	pkglocal const flt32bfmt	: (sb : strbuf#, val : flt32, mode : int, precision : int -> void)
;;

const Dblbias = 1023
const Fltbias = 127

const flt64bfmt = {sb, val, mode, precision
	var isneg, exp, mant

	(isneg, mant, exp) = flt64explode(val)
	dragon4(sb, isneg, mant, (exp - 52 : int64), Dblbias, mode, precision)
}

const flt32bfmt = {sb, val, mode, precision
	var isneg, exp, mant

	(isneg, mant, exp) = flt32explode(val)
	dragon4(sb, isneg, (mant : int64), (exp - 52 : int64), Fltbias, mode, precision)
}

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

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

	/* 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)
		;;
	;;
}

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, ".")
	;;
}