3c5cd78fbdcf — Leonard Ritter a month ago
* work on numbers API
1 files changed, 109 insertions(+), 9 deletions(-)

M testing/test_uvm.sc
M testing/test_uvm.sc +109 -9
@@ 2,6 2,7 @@ using import struct
 using import enum
 using import Map
 using import Array
+using import String
 
 import ..lib.tukan.use
 using import tukan.ustore

          
@@ 15,16 16,16 @@ fn urealloc (opaque ptr size)
 global bf_ctx : bf_context_t
 bf_context_init &bf_ctx urealloc null
 
-
 struct Number plain
     sign : i64
     expn : i64
     len : u64
     tab : (array u64)
 
+    let DEFAULT_PREC = 56
+
     fn... from_bf (n : &bf_t)
         bufsize := (sizeof this-type) + (sizeof u64) * n.len
-        print n.sign n.expn n.len bufsize
         let buf =
             ptrtoref
                 bitcast

          
@@ 34,12 35,36 @@ struct Number plain
         buf.expn = n.expn
         buf.len = n.len
         for i in (range n.len)
-            #print i (n.tab @ i)
             buf.tab @ i = n.tab @ i
         local uref = (URef.store &buf bufsize)
         uref.kind = URef.Kind.Number
         uref
 
+fn bftostr (x)
+    local sz : u64
+    let s =
+        bf_ftoa &sz x 10 Number.DEFAULT_PREC BF_FTOA_FORMAT_FREE_MIN
+    String s sz
+
+fn numbertobf (r ref)
+    let num =
+        bitcast ('load ref) (pointer Number)
+    local n =
+        bf_t
+            sign = (num.sign as i32)
+            expn = num.expn
+            len = num.len
+            tab = (bitcast (& (num.tab @ 0)) (mutable pointer u64))
+    bf_set r &n
+
+fn numbertostr (ref)
+    local n : bf_t
+    bf_init &bf_ctx &n
+    numbertobf &n ref
+    let str = (bftostr &n)
+    bf_delete &n
+    str
+
 inline gen-number (f value)
     local n : bf_t
     bf_init &bf_ctx &n

          
@@ 55,12 80,87 @@ case (value : u64)
 case (value : f64)
     gen-number bf_set_float64 value
 
-print
-    number -1
-    number 0
-    number 1
-    number 2
-    number 3.5
+inline number_op0 (f ...)
+    fn ()
+        local res : bf_t
+        bf_init &bf_ctx &res
+        f &res ...
+        let result = (Number.from_bf res)
+        bf_delete &res
+        result
+
+inline number_op1 (f ...)
+    fn (x)
+        local bfx : bf_t
+        bf_init &bf_ctx &bfx
+        numbertobf &bfx x
+        local res : bf_t
+        bf_init &bf_ctx &res
+        f &res &bfx ...
+        let result = (Number.from_bf res)
+        bf_delete &bfx
+        bf_delete &res
+        result
+
+inline number_op2 (f ...)
+    fn (a b)
+        local bfa : bf_t
+        local bfb : bf_t
+        bf_init &bf_ctx &bfa
+        bf_init &bf_ctx &bfb
+        numbertobf &bfa a
+        numbertobf &bfb b
+        local res : bf_t
+        bf_init &bf_ctx &res
+        f &res &bfa &bfb ...
+        let result = (Number.from_bf res)
+        bf_delete &bfa
+        bf_delete &bfb
+        bf_delete &res
+        result
 
+let number-const-log2 = (number_op0 bf_const_log2 Number.DEFAULT_PREC 0)
+let number-const-pi = (number_op0 bf_const_pi Number.DEFAULT_PREC 0)
+
+let number-add = (number_op2 bf_add Number.DEFAULT_PREC 0)
+let number-sub = (number_op2 bf_sub Number.DEFAULT_PREC 0)
+let number-mul = (number_op2 bf_mul Number.DEFAULT_PREC 0)
+let number-div = (number_op2 bf_div Number.DEFAULT_PREC 0)
+let number-rem = (number_op2 bf_rem Number.DEFAULT_PREC 0 bf_rnd_t.BF_RNDZ)
+
+let number-pow = (number_op2 bf_pow Number.DEFAULT_PREC 0)
+let number-atan2 = (number_op2 bf_atan2 Number.DEFAULT_PREC 0)
+
+let number-or = (number_op2 bf_logic_or)
+let number-xor = (number_op2 bf_logic_xor)
+let number-and = (number_op2 bf_logic_and)
+
+let number-sqrt = (number_op1 bf_sqrt Number.DEFAULT_PREC 0)
+let number-exp = (number_op1 bf_exp Number.DEFAULT_PREC 0)
+let number-log = (number_op1 bf_log Number.DEFAULT_PREC 0)
+let number-pow = (number_op1 bf_pow Number.DEFAULT_PREC 0)
+let number-cos = (number_op1 bf_cos Number.DEFAULT_PREC 0)
+let number-sin = (number_op1 bf_sin Number.DEFAULT_PREC 0)
+let number-tan = (number_op1 bf_tan Number.DEFAULT_PREC 0)
+let number-atan = (number_op1 bf_atan Number.DEFAULT_PREC 0)
+let number-asin = (number_op1 bf_asin Number.DEFAULT_PREC 0)
+let number-acos = (number_op1 bf_acos Number.DEFAULT_PREC 0)
+
+try 
+    print
+        number -1
+        number 0
+        number 1
+        number 2
+    print
+        numbertostr (number-const-pi)
+    print
+        numbertostr
+            number-sqrt
+                number-or
+                    number 41
+                    number 10
+else
+    print "failed"
 
 ;
  No newline at end of file