| 1 | module scheme |
| 2 | |
| 3 | import math |
| 4 | |
| 5 | struct NativeFloatingInput { |
| 6 | a Value |
| 7 | b Value |
| 8 | } |
| 9 | |
| 10 | fn native_floating_inputs() []NativeFloatingInput { |
| 11 | values := [integer(0), integer(3), integer(-7), integer(exact_min), integer(exact_max), |
| 12 | integer(9007199254740993), real(0.0), real(-0.0), real(0.1), real(-0.5), |
| 13 | real(math.f64_from_bits(1)), real(math.inf(1)), real(math.inf(-1)), real(math.nan()), |
| 14 | real(9223372036854775808.0), real(-9223372036854775808.0)] |
| 15 | mut inputs := []NativeFloatingInput{} |
| 16 | for a in values { |
| 17 | for b in values { |
| 18 | inputs << NativeFloatingInput{a: a, b: b} |
| 19 | } |
| 20 | } |
| 21 | return inputs |
| 22 | } |
| 23 | |
| 24 | fn native_floating_same(actual Value, expected Value) ! { |
| 25 | assert actual.tag == expected.tag |
| 26 | if actual.tag == .real { |
| 27 | if math.is_nan(expected.floating()) { assert math.is_nan(actual.floating()) } |
| 28 | else { assert math.f64_bits(actual.floating()) == math.f64_bits(expected.floating()) } |
| 29 | } else { assert write(actual)! == write(expected)! } |
| 30 | } |
| 31 | |
| 32 | fn test_native_floating_arithmetic_preserves_rounding_order_and_scalar_bits() { |
| 33 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 34 | vm.eval('(define(add a b)(+ a b)) (define(sub a b)(- a b)) (define(mul a b)(* a b)) |
| 35 | (define(nary x)(+ 9007199254740992 1 -9007199254740992 x)) |
| 36 | (define(product x)(* 3 5 x)) (define(literal x)(+ 0.1 x)) |
| 37 | (define(neg x)(- x)) (define(positive x)(+ x)) (define(times x)(* x)) |
| 38 | (define(round-product a b c)(+ (* a b) c))')! |
| 39 | roots := [vm.lookup('add')!, vm.lookup('sub')!, vm.lookup('mul')!] |
| 40 | inputs := native_floating_inputs().filter(it.a.tag != .integer || it.b.tag != .integer |
| 41 | || (it.a.whole() >= -7 && it.a.whole() <= 3 && it.b.whole() >= -7 && it.b.whole() <= 3)) |
| 42 | mut expected := [][]Value{} |
| 43 | for root in roots { |
| 44 | mut values := []Value{} |
| 45 | for input in inputs { values << vm.call(root, [input.a, input.b])! } |
| 46 | expected << values |
| 47 | } |
| 48 | unary := ['nary', 'product', 'literal', 'neg', 'positive', 'times'] |
| 49 | args := [real(0.0), real(0.1), real(0.2), real(-0.0), real(-0.0), real(-0.0)] |
| 50 | mut unary_expected := []Value{} |
| 51 | for i, name in unary { unary_expected << vm.call(vm.lookup(name)!, [args[i]])! } |
| 52 | product := vm.lookup('round-product')! |
| 53 | product_args := [real(1.0000000074505806), real(0.9999999925494194), real(-1.0)] |
| 54 | product_expected := vm.call(product, product_args)! |
| 55 | assert product_expected.floating() == 0.0 |
| 56 | vm.with_native_execution(fn [roots, inputs, expected, unary, args, unary_expected, product, product_args, product_expected] (mut vm VM) !Value { |
| 57 | for op, root in roots { |
| 58 | for i, input in inputs { native_floating_same(vm.call(root, [input.a, input.b])!, expected[op][i])! } |
| 59 | } |
| 60 | for i, name in unary { native_floating_same(vm.call(vm.lookup(name)!, [args[i]])!, unary_expected[i])! } |
| 61 | native_floating_same(vm.call(product, product_args)!, product_expected)! |
| 62 | hits, deopts := vm.native_execution_stats() |
| 63 | assert hits == u64(roots.len * inputs.len + unary.len + 1) && deopts == 0, '${hits} hits, ${deopts} deopts' |
| 64 | return unspecified() |
| 65 | })! |
| 66 | } |
| 67 | |
| 68 | fn test_native_floating_comparisons_keep_exact_integer_boundaries_and_nan_rules() { |
| 69 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 70 | vm.eval('(define(eq a b)(= a b)) (define(lt a b)(< a b)) (define(gt a b)(> a b)) |
| 71 | (define(le a b)(<= a b)) (define(ge a b)(>= a b))')! |
| 72 | roots := [vm.lookup('eq')!, vm.lookup('lt')!, vm.lookup('gt')!, vm.lookup('le')!, vm.lookup('ge')!] |
| 73 | inputs := native_floating_inputs() |
| 74 | mut expected := [][]Value{} |
| 75 | for root in roots { |
| 76 | mut values := []Value{} |
| 77 | for input in inputs { values << vm.call(root, [input.a, input.b])! } |
| 78 | expected << values |
| 79 | } |
| 80 | vm.with_native_execution(fn [roots, inputs, expected] (mut vm VM) !Value { |
| 81 | for op, root in roots { |
| 82 | for i, input in inputs { native_floating_same(vm.call(root, [input.a, input.b])!, expected[op][i])! } |
| 83 | } |
| 84 | hits, deopts := vm.native_execution_stats() |
| 85 | assert hits == u64(roots.len * inputs.len) && deopts == 0, '${hits} hits, ${deopts} deopts' |
| 86 | return unspecified() |
| 87 | })! |
| 88 | } |
| 89 | |
| 90 | fn test_native_floating_captures_refresh_and_revalidate_bindings() { |
| 91 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 92 | vm.eval('(define change #f) (define original-plus +) |
| 93 | (define(make n)(set! change (lambda(x)(set! n x))) (lambda(x)(+ x n))) |
| 94 | (define f(make 0.25))')! |
| 95 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 96 | root := vm.lookup('f')! |
| 97 | native_floating_same(vm.call(root, [integer(2)])!, real(2.25))! |
| 98 | vm.eval('(change 0.5)')! |
| 99 | $if gcboehm ? { gc_collect() } |
| 100 | native_floating_same(vm.call(root, [integer(2)])!, real(2.5))! |
| 101 | vm.eval('(change 1/2)')! |
| 102 | native_floating_same(vm.call(root, [real(1.0)])!, real(1.5))! |
| 103 | vm.eval('(change -0.0)')! |
| 104 | // + folds from exact zero, so two negative zeros produce positive zero. |
| 105 | native_floating_same(vm.call(root, [real(-0.0)])!, real(0.0))! |
| 106 | vm.eval('(change 4)')! |
| 107 | native_floating_same(vm.call(root, [integer(3)])!, integer(7))! |
| 108 | hits, deopts := vm.native_execution_stats() |
| 109 | assert hits >= 4 && deopts == 0 |
| 110 | vm.eval('(set! + (lambda(a b) 42))')! |
| 111 | assert vm.call(root, [real(1.0)])!.whole() == 42 |
| 112 | vm.eval('(set! + original-plus)')! |
| 113 | native_floating_same(vm.call(root, [real(1.0)])!, real(5.0))! |
| 114 | return unspecified() |
| 115 | })! |
| 116 | } |
| 117 | |
| 118 | fn test_native_exact_float_conversion_falls_back_for_fractions_large_values_and_errors() { |
| 119 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 120 | vm.eval('(define(f x)(inexact->exact x)) (define(compare a b c)(< a b c))')! |
| 121 | roots := [vm.lookup('f')!, vm.lookup('compare')!] |
| 122 | inputs := [real(-9223372036854775808.0), real(9007199254740992.0), real(-0.0), real(999.0)] |
| 123 | vm.with_native_execution(fn [roots, inputs] (mut vm VM) !Value { |
| 124 | for input in inputs { assert vm.call(roots[0], [input])!.whole() == i64(input.floating()) } |
| 125 | hits, deopts := vm.native_execution_stats() |
| 126 | assert hits == u64(inputs.len) && deopts == 0 |
| 127 | return unspecified() |
| 128 | })! |
| 129 | for input in [real(0.5), real(0.1), real(9223372036854775808.0)] { |
| 130 | expected := vm.call(roots[0], [input])! |
| 131 | vm.with_native_execution(fn [roots, input, expected] (mut vm VM) !Value { |
| 132 | native_floating_same(vm.call(roots[0], [input])!, expected)! |
| 133 | _, deopts := vm.native_execution_stats() |
| 134 | assert deopts == 1 |
| 135 | return unspecified() |
| 136 | })! |
| 137 | } |
| 138 | for args in [[real(math.nan())], [real(math.inf(1))], [real(math.inf(-1))], |
| 139 | [real(2.0), real(1.0), boolean(false)]] { |
| 140 | root := roots[if args.len == 1 { 0 } else { 1 }] |
| 141 | mut expected := '' |
| 142 | if _ := vm.call(root, args) { assert false } else { expected = err.msg().clone() } |
| 143 | vm.with_native_execution(fn [root, args, expected] (mut vm VM) !Value { |
| 144 | if _ := vm.call(root, args) { return error('invalid numeric call accepted') } |
| 145 | else { assert err.msg() == expected } |
| 146 | _, deopts := vm.native_execution_stats() |
| 147 | assert deopts == 1 |
| 148 | return unspecified() |
| 149 | })! |
| 150 | } |
| 151 | } |
| 152 | |