interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / json / jsonf.om

710 lines · 13345 bytes Raw Blame History
1note seed/json/jsonf.om: OPTIONAL exact float conversion for the JSON units (omega-mv #788). Needs json.om and
2note jsonw.om before it. The reader never converts numbers; this unit is only for callers that want an f64.
3note js_f64 is decimal-to-binary64 with round-to-nearest-even, exact for every lexeme (overflow gives
4note +-infinity, underflow +-0). js_fmt64 / js_wf64 write the shortest digits that convert back to the same
5note f64, in Python repr layout. Method: the arbitrary-precision decimal of Go strconv (decimal.go,
6note atof.go floatBits, ftoa.go roundShortest): 800 digits plus a truncation flag, exact shifts by powers of
7note two. It is NOT the serde_json default float parser, which is not correctly rounded.
8note Scratch: a caller `long` array of jsfs cells. Cells 0..2 digit counts, 3..5 decimal points, 6..8
9note truncation flags of decimals 0..2, 9 sign, 10 status (0 exact path, 1 overflow to infinity, 2 not a
10note number lexeme), 11 formatted length; digits of decimal q at 16 + 800q; formatted bytes at 2416.
11
12const jsfs ulong 2464
13const jsfdig ulong 16
14const jsfout ulong 2416
15
16note ======== decimal digits ========
17
18fn js_dget fs edit long jsfs q ulong i ulong to ulong
19ret ulong get fs add add jsfdig mul q 800 i
20end
21
22note js_dput: store digit v at w, or remember a lost nonzero digit beyond 800
23fn js_dput fs edit long jsfs q ulong w ulong v ulong to void
24if lt w 800
25put fs add add jsfdig mul q 800 w long v
26ret
27end
28if ne v 0
29put fs add q 6 1
30end
31end
32
33fn js_dtrim fs edit long jsfs q ulong to void
34nd = ulong get fs q
35while gt nd 0 bound 1000
36if ne js_dget fs q sub nd 1 0
37break
38end
39nd = sub nd 1
40end
41put fs q long nd
42if eq nd 0
43put fs add q 3 0
44end
45end
46
47fn js_dassign fs edit long jsfs q ulong v ulong to void
48tmp = new long 20 0
49k = ulong 0
50x = v
51while gt x 0 bound 21
52put tmp k long rem x 10
53k = add k 1
54x = div x 10
55end
56for i ulong k
57js_dput fs q i ulong get tmp sub sub k 1 i
58end
59put fs q long k
60put fs add q 3 long k
61put fs add q 6 0
62js_dtrim fs q
63end
64
65note js_drsh: divide decimal q by 2^k, 1 <= k <= 60 (Go rightShift); 9 * 2^60 + 2^60 < 2^64
66fn js_drsh fs edit long jsfs q ulong k ulong to void
67nd = ulong get fs q
68r = ulong 0
69n = ulong 0
70while eq shr n k 0 bound 1000
71if ge r nd
72if eq n 0
73put fs q 0
74ret
75end
76while eq shr n k 0 bound 100
77n = mul n 10
78r = add r 1
79end
80break
81end
82n = add mul n 10 js_dget fs q r
83r = add r 1
84end
85put fs add q 3 sub get fs add q 3 sub long r 1
86js_drsh2 fs q k r n
87end
88
89fn js_drsh2 fs edit long jsfs q ulong k ulong r ulong n ulong to void
90nd = ulong get fs q
91mask = sub shl ulong 1 k 1
92w = ulong 0
93x = n
94y = r
95while lt y nd bound 1000
96c = js_dget fs q y
97js_dput fs q w shr x k
98w = add w 1
99x = add mul and x mask 10 c
100y = add y 1
101end
102while gt x 0 bound 1000
103js_dput fs q w shr x k
104if lt w 800
105w = add w 1
106end
107x = mul and x mask 10
108end
109put fs q long w
110js_dtrim fs q
111end
112
113note js_dlsh: multiply decimal q by 2^k, 1 <= k <= 60 (Go leftShift). The first pass finds the number of new
114note leading digits from the final carry instead of Go's leftcheats table.
115fn js_dlsh fs edit long jsfs q ulong k ulong to void
116nd = ulong get fs q
117n = ulong 0
118r = nd
119while gt r 0 bound 1000
120r = sub r 1
121n = div add n shl js_dget fs q r k 10
122end
123del = ulong 0
124while gt n 0 bound 100
125del = add del 1
126n = div n 10
127end
128js_dlsh2 fs q k del
129nd = add nd del
130if gt nd 800
131nd = 800
132end
133put fs q long nd
134put fs add q 3 add get fs add q 3 long del
135js_dtrim fs q
136end
137
138fn js_dlsh2 fs edit long jsfs q ulong k ulong del ulong to void
139r = ulong get fs q
140w = add r del
141n = ulong 0
142while gt r 0 bound 1000
143r = sub r 1
144n = add n shl js_dget fs q r k
145w = sub w 1
146js_dput fs q w rem n 10
147n = div n 10
148end
149while gt n 0 bound 100
150w = sub w 1
151js_dput fs q w rem n 10
152n = div n 10
153end
154end
155
156fn js_dshift fs edit long jsfs q ulong k long to void
157if eq get fs q 0
158ret
159end
160s = k
161while gt s 60 bound 100
162js_dlsh fs q 60
163s = sub s 60
164end
165if gt s 0
166js_dlsh fs q ulong s
167end
168while lt s -60 bound 100
169js_drsh fs q 60
170s = add s 60
171end
172if lt s 0
173js_drsh fs q ulong neg s
174end
175end
176
177note js_dsru: Go shouldRoundUp at digit index i; an exact half rounds to even unless digits were lost
178fn js_dsru fs edit long jsfs q ulong i long to bool
179nd = get fs q
180if or lt i 0 ge i nd
181ret false
182end
183d = js_dget fs q ulong i
184if and eq d 5 eq add i 1 nd
185if ne get fs add q 6 0
186ret true
187end
188if eq i 0
189ret false
190end
191ret eq rem js_dget fs q ulong sub i 1 2 1
192end
193ret ge d 5
194end
195
196fn js_drint fs edit long jsfs q ulong to ulong
197dp = get fs add q 3
198if gt dp 20
199ret 18446744073709551615
200end
201nd = get fs q
202n = ulong 0
203i = long 0
204while and lt i dp lt i nd bound 100
205n = add mul n 10 js_dget fs q ulong i
206i = add i 1
207end
208while lt i dp bound 100
209n = mul n 10
210i = add i 1
211end
212if js_dsru fs q dp
213n = add n 1
214end
215ret n
216end
217
218fn js_drdown fs edit long jsfs q ulong i long to void
219if or lt i 0 ge i get fs q
220ret
221end
222put fs q i
223js_dtrim fs q
224end
225
226fn js_drup fs edit long jsfs q ulong i long to void
227if or lt i 0 ge i get fs q
228ret
229end
230j = i
231while gt j 0 bound 1000
232j = sub j 1
233d = js_dget fs q ulong j
234if lt d 9
235js_dput fs q ulong j add d 1
236put fs q add j 1
237ret
238end
239end
240js_dput fs q 0 1
241put fs q 1
242put fs add q 3 add get fs add q 3 1
243end
244
245fn js_dround fs edit long jsfs q ulong i long to void
246if or lt i 0 ge i get fs q
247ret
248end
249if js_dsru fs q i
250js_drup fs q i
251ret
252end
253js_drdown fs q i
254end
255
256note ======== lexeme to f64 ========
257
258note js_dmant: digits of the mantissa into decimal 0. cnt counts every significant digit, also those past
259note the 800 kept, so the decimal point stays exact; a lost nonzero digit sets the truncation flag.
260fn js_dmant fs edit long jsfs src view uchar * p ulong n ulong to ulong
261q = p
262dot = false
263cnt = ulong 0
264dp = long 0
265while lt q n bound 9223372036854775807
266c = get src q
267if eq c 46
268if dot
269break
270end
271dot = true
272dp = long cnt
273q = add q 1
274continue
275end
276if or lt c 48 gt c 57
277break
278end
279if and eq c 48 eq cnt 0
280dp = sub dp 1
281q = add q 1
282continue
283end
284if lt cnt 800
285js_dput fs 0 cnt ulong sub c 48
286else
287if ne c 48
288put fs 6 1
289end
290end
291cnt = add cnt 1
292q = add q 1
293end
294if not dot
295dp = long cnt
296end
297nd = cnt
298if gt nd 800
299nd = 800
300end
301put fs 0 long nd
302put fs 3 dp
303ret q
304end
305
306fn js_dexp fs edit long jsfs src view uchar * p ulong n ulong to void
307q = p
308if not or js_isb src q n 101 js_isb src q n 69
309ret
310end
311q = add q 1
312ng = false
313if js_isb src q n 43
314q = add q 1
315else
316if js_isb src q n 45
317ng = true
318q = add q 1
319end
320end
321note The mantissa displacement is exact. Once |exponent| reaches |dp| + 331, its sign
322note decides the binary64 range even after cancellation (js_dbits uses dp > 310 or dp < -330).
323note Saturate only there, not at a fixed exponent; input span <= jsmaxn + 2 keeps this in long.
324lim = get fs 3
325if lt lim 0
326lim = neg lim
327end
328lim = add lim 331
329e = long 0
330while lt q n bound 9223372036854775807
331c = get src q
332if or lt c 48 gt c 57
333break
334end
335d = long sub c 48
336if gt e div sub lim d 10
337e = lim
338else
339e = add mul e 10 d
340end
341q = add q 1
342end
343if ng
344e = neg e
345end
346put fs 3 add get fs 3 e
347end
348
349fn js_fpack fs edit long jsfs m ulong f ulong to ulong
350b = or m shl f 52
351if ne get fs 9 0
352b = or b shl ulong 1 63
353end
354ret b
355end
356
357fn js_fovf fs edit long jsfs to ulong
358put fs 10 1
359ret js_fpack fs 0 2047
360end
361
362fn js_powtab n long to long
363if ge n 9
364ret 27
365end
366if eq n 0
367ret 1
368end
369if eq n 1
370ret 3
371end
372if eq n 2
373ret 6
374end
375if eq n 3
376ret 9
377end
378if eq n 4
379ret 13
380end
381if eq n 5
382ret 16
383end
384if eq n 6
385ret 19
386end
387if eq n 7
388ret 23
389end
390ret 26
391end
392
393fn js_dlow fs edit long jsfs to bool
394if eq get fs 0 0
395ret false
396end
397dp = get fs 3
398if lt dp 0
399ret true
400end
401ret and eq dp 0 lt get fs jsfdig 5
402end
403
404note js_dscale: scale decimal 0 into [0.5, 1) by powers of two; returns the binary exponent
405fn js_dscale fs edit long jsfs to long
406e = long 0
407while gt get fs 3 0 bound 1000
408s = js_powtab get fs 3
409js_dshift fs 0 neg s
410e = add e s
411end
412while js_dlow fs bound 1000
413s = js_powtab neg get fs 3
414js_dshift fs 0 s
415e = sub e s
416end
417ret e
418end
419
420note js_dbits: Go floatBits for binary64 (bias -1023, 52 mantissa bits, 11 exponent bits) on decimal 0
421fn js_dbits fs edit long jsfs to ulong
422if eq get fs 0 0
423ret js_fpack fs 0 0
424end
425dp = get fs 3
426if gt dp 310
427ret js_fovf fs
428end
429if lt dp -330
430ret js_fpack fs 0 0
431end
432e = sub js_dscale fs 1
433if lt e -1022
434js_dshift fs 0 add e 1022
435e = long -1022
436end
437if ge e 1024
438ret js_fovf fs
439end
440js_dshift fs 0 53
441m = js_drint fs 0
442if eq m 9007199254740992
443m = shr m 1
444e = add e 1
445if ge e 1024
446ret js_fovf fs
447end
448end
449if eq and m 4503599627370496 0
450ret js_fpack fs m 0
451end
452ret js_fpack fs and m 4503599627370495 ulong add e 1023
453end
454
455note js_f64: the f64 nearest the number lexeme at src[off] (ties to even). Status js_f64st: 0 finite, 1 the
456note magnitude overflowed to infinity, 2 no number lexeme at off (the result is then a quiet NaN).
457fn js_f64 src view uchar * off ulong fs edit long jsfs to f64
458for i ulong 12
459put fs i 0
460end
461n = js_slim src off
462if eq js_numend src off n 0
463put fs 10 2
464ret bitcast f64 ulong 9221120237041090560
465end
466p = off
467if js_atv src p 45
468put fs 9 1
469p = add p 1
470end
471p = js_dmant fs src p n
472js_dexp fs src p n
473js_dtrim fs 0
474ret bitcast f64 js_dbits fs
475end
476
477fn js_f64st fs edit long jsfs to ulong
478ret ulong get fs 10
479end
480
481note ======== shortest f64 text ========
482
483fn js_fdig fs edit long jsfs q ulong i long to long
484if or lt i 0 ge i get fs q
485ret 0
486end
487ret long js_dget fs q ulong i
488end
489
490fn js_fud ud ulong m long u long to ulong
491if and eq ud 0 lt add m 1 u
492ret 2
493end
494if and eq ud 0 ne m u
495ret 1
496end
497if and eq ud 1 or ne m 9 ne u 0
498ret 2
499end
500ret ud
501end
502
503fn js_fokd fs edit long jsfs inc bool ui long m long to bool
504li = add sub ui get fs 4 get fs 5
505if ne js_fdig fs 2 li m
506ret true
507end
508ret and inc eq add li 1 get fs 2
509end
510
511note js_fstep: one digit of Go roundShortest; returns the new upperdelta 0..2, or 3 when d is final
512fn js_fstep fs edit long jsfs inc bool ui long ud ulong to ulong
513mi = add sub ui get fs 4 get fs 3
514if ge mi get fs 0
515ret 3
516end
517m = js_fdig fs 0 mi
518d = js_fud ud m js_fdig fs 1 ui
519okd = js_fokd fs inc ui m
520oku = false
521if gt d 0
522oku = or inc or gt d 1 lt add ui 1 get fs 1
523end
524if and okd oku
525js_dround fs 0 add mi 1
526ret 3
527end
528if okd
529js_drdown fs 0 add mi 1
530ret 3
531end
532if oku
533js_drup fs 0 add mi 1
534ret 3
535end
536ret d
537end
538
539fn js_fwalk fs edit long jsfs inc bool to void
540ud = ulong 0
541ui = long 0
542for k ulong 2000
543r = js_fstep fs inc ui ud
544if ge r 3
545ret
546end
547ud = r
548ui = add ui 1
549end
550end
551
552note js_fshort: Go roundShortest on decimal 0 = mt * 2^(e-52); decimals 1 and 2 hold the upper and lower
553note halfway bounds
554fn js_fshort fs edit long jsfs mt ulong e long to void
555if eq mt 0
556put fs 0 0
557ret
558end
559if gt e -1022
560if ge mul 332 sub get fs 3 get fs 0 mul 100 sub e 52
561ret
562end
563end
564js_dassign fs 1 add mul mt 2 1
565js_dshift fs 1 sub e 53
566if or gt mt 4503599627370496 eq e -1022
567js_dassign fs 2 add mul sub mt 1 2 1
568js_dshift fs 2 sub e 53
569else
570js_dassign fs 2 add mul sub mul mt 2 1 2 1
571js_dshift fs 2 sub e 54
572end
573js_fwalk fs eq and mt 1 0
574end
575
576fn js_fput fs edit long jsfs k ulong c ulong to ulong
577put fs add jsfout k long c
578ret add k 1
579end
580
581fn js_ffix fs edit long jsfs k ulong to ulong
582w = k
583dp = get fs 3
584nd = get fs 0
585if le dp 0
586w = js_fput fs w 48
587w = js_fput fs w 46
588for i ulong ulong neg dp
589w = js_fput fs w 48
590end
591for i ulong ulong nd
592w = js_fput fs w add js_dget fs 0 i 48
593end
594ret w
595end
596if ge dp nd
597for i ulong ulong nd
598w = js_fput fs w add js_dget fs 0 i 48
599end
600for i ulong ulong sub dp nd
601w = js_fput fs w 48
602end
603w = js_fput fs w 46
604w = js_fput fs w 48
605ret w
606end
607for i ulong ulong dp
608w = js_fput fs w add js_dget fs 0 i 48
609end
610w = js_fput fs w 46
611for i ulong ulong sub nd dp
612w = js_fput fs w add js_dget fs 0 add ulong dp i 48
613end
614ret w
615end
616
617fn js_fsci fs edit long jsfs k ulong to ulong
618w = js_fput fs k add js_dget fs 0 0 48
619nd = ulong get fs 0
620if gt nd 1
621w = js_fput fs w 46
622for i ulong sub nd 1
623w = js_fput fs w add js_dget fs 0 add i 1 48
624end
625end
626w = js_fput fs w 101
627x = sub get fs 3 1
628if lt x 0
629w = js_fput fs w 45
630x = neg x
631else
632w = js_fput fs w 43
633end
634if lt x 10
635w = js_fput fs w 48
636end
637if ge x 100
638w = js_fput fs w add ulong div x 100 48
639end
640if ge x 10
641w = js_fput fs w add ulong rem div x 10 10 48
642end
643w = js_fput fs w add ulong rem x 10 48
644ret w
645end
646
647note js_frepr: Python repr layout: fixed when the decimal exponent x is -4 <= x < 16 (".0" added to whole
648note numbers), else d[.ddd]e+XX / e-XX with at least two exponent digits; zero is 0.0 or -0.0.
649fn js_frepr fs edit long jsfs sg bool to ulong
650k = ulong 0
651if sg
652k = js_fput fs k 45
653end
654if eq get fs 0 0
655k = js_fput fs k 48
656k = js_fput fs k 46
657ret js_fput fs k 48
658end
659x = sub get fs 3 1
660if and ge x -4 lt x 16
661ret js_ffix fs k
662end
663ret js_fsci fs k
664end
665
666note js_fmt64: shortest round-trip text of a finite x at fs[jsfout ..]; returns its length, -1 for inf or NaN
667fn js_fmt64 fs edit long jsfs x f64 to long
668bits = bitcast ulong x
669ex = and shr bits 52 2047
670mt = and bits 4503599627370495
671put fs 11 0
672if eq ex 2047
673ret -1
674end
675if eq ex 0
676ex = 1
677else
678mt = or mt 4503599627370496
679end
680e = sub long ex 1023
681js_dassign fs 0 mt
682js_dshift fs 0 sub e 52
683js_fshort fs mt e
684k = js_frepr fs ne shr bits 63 0
685put fs 11 long k
686ret long k
687end
688
689fn js_fmtch fs edit long jsfs i ulong to uchar
690ret uchar get fs add jsfout i
691end
692
693note js_wf64: write a finite f64 as its shortest text; inf and NaN are refused (jswenm), JSON has no form
694fn js_wf64 ob edit uchar * x f64 fs edit long jsfs to void
695if ne get ob jswerr 0
696ret
697end
698k = js_fmt64 fs x
699if lt k 0
700js_wfail ob jswenm
701ret
702end
703if not js_wpre ob
704ret
705end
706for i ulong ulong k
707js_wb ob js_fmtch fs i
708end
709js_wpost ob
710end