module scheme import math struct NativeFloatingInput { a Value b Value } fn native_floating_inputs() []NativeFloatingInput { values := [integer(0), integer(3), integer(-7), integer(exact_min), integer(exact_max), integer(9007199254740993), real(0.0), real(-0.0), real(0.1), real(-0.5), real(math.f64_from_bits(1)), real(math.inf(1)), real(math.inf(-1)), real(math.nan()), real(9223372036854775808.0), real(-9223372036854775808.0)] mut inputs := []NativeFloatingInput{} for a in values { for b in values { inputs << NativeFloatingInput{a: a, b: b} } } return inputs } fn native_floating_same(actual Value, expected Value) ! { assert actual.tag == expected.tag if actual.tag == .real { if math.is_nan(expected.floating()) { assert math.is_nan(actual.floating()) } else { assert math.f64_bits(actual.floating()) == math.f64_bits(expected.floating()) } } else { assert write(actual)! == write(expected)! } } fn test_native_floating_arithmetic_preserves_rounding_order_and_scalar_bits() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define(add a b)(+ a b)) (define(sub a b)(- a b)) (define(mul a b)(* a b)) (define(nary x)(+ 9007199254740992 1 -9007199254740992 x)) (define(product x)(* 3 5 x)) (define(literal x)(+ 0.1 x)) (define(neg x)(- x)) (define(positive x)(+ x)) (define(times x)(* x)) (define(round-product a b c)(+ (* a b) c))')! roots := [vm.lookup('add')!, vm.lookup('sub')!, vm.lookup('mul')!] inputs := native_floating_inputs().filter(it.a.tag != .integer || it.b.tag != .integer || (it.a.whole() >= -7 && it.a.whole() <= 3 && it.b.whole() >= -7 && it.b.whole() <= 3)) mut expected := [][]Value{} for root in roots { mut values := []Value{} for input in inputs { values << vm.call(root, [input.a, input.b])! } expected << values } unary := ['nary', 'product', 'literal', 'neg', 'positive', 'times'] args := [real(0.0), real(0.1), real(0.2), real(-0.0), real(-0.0), real(-0.0)] mut unary_expected := []Value{} for i, name in unary { unary_expected << vm.call(vm.lookup(name)!, [args[i]])! } product := vm.lookup('round-product')! product_args := [real(1.0000000074505806), real(0.9999999925494194), real(-1.0)] product_expected := vm.call(product, product_args)! assert product_expected.floating() == 0.0 vm.with_native_execution(fn [roots, inputs, expected, unary, args, unary_expected, product, product_args, product_expected] (mut vm VM) !Value { for op, root in roots { for i, input in inputs { native_floating_same(vm.call(root, [input.a, input.b])!, expected[op][i])! } } for i, name in unary { native_floating_same(vm.call(vm.lookup(name)!, [args[i]])!, unary_expected[i])! } native_floating_same(vm.call(product, product_args)!, product_expected)! hits, deopts := vm.native_execution_stats() assert hits == u64(roots.len * inputs.len + unary.len + 1) && deopts == 0, '${hits} hits, ${deopts} deopts' return unspecified() })! } fn test_native_floating_comparisons_keep_exact_integer_boundaries_and_nan_rules() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define(eq a b)(= a b)) (define(lt a b)(< a b)) (define(gt a b)(> a b)) (define(le a b)(<= a b)) (define(ge a b)(>= a b))')! roots := [vm.lookup('eq')!, vm.lookup('lt')!, vm.lookup('gt')!, vm.lookup('le')!, vm.lookup('ge')!] inputs := native_floating_inputs() mut expected := [][]Value{} for root in roots { mut values := []Value{} for input in inputs { values << vm.call(root, [input.a, input.b])! } expected << values } vm.with_native_execution(fn [roots, inputs, expected] (mut vm VM) !Value { for op, root in roots { for i, input in inputs { native_floating_same(vm.call(root, [input.a, input.b])!, expected[op][i])! } } hits, deopts := vm.native_execution_stats() assert hits == u64(roots.len * inputs.len) && deopts == 0, '${hits} hits, ${deopts} deopts' return unspecified() })! } fn test_native_floating_captures_refresh_and_revalidate_bindings() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define change #f) (define original-plus +) (define(make n)(set! change (lambda(x)(set! n x))) (lambda(x)(+ x n))) (define f(make 0.25))')! vm.with_native_execution(fn (mut vm VM) !Value { root := vm.lookup('f')! native_floating_same(vm.call(root, [integer(2)])!, real(2.25))! vm.eval('(change 0.5)')! $if gcboehm ? { gc_collect() } native_floating_same(vm.call(root, [integer(2)])!, real(2.5))! vm.eval('(change 1/2)')! native_floating_same(vm.call(root, [real(1.0)])!, real(1.5))! vm.eval('(change -0.0)')! // + folds from exact zero, so two negative zeros produce positive zero. native_floating_same(vm.call(root, [real(-0.0)])!, real(0.0))! vm.eval('(change 4)')! native_floating_same(vm.call(root, [integer(3)])!, integer(7))! hits, deopts := vm.native_execution_stats() assert hits >= 4 && deopts == 0 vm.eval('(set! + (lambda(a b) 42))')! assert vm.call(root, [real(1.0)])!.whole() == 42 vm.eval('(set! + original-plus)')! native_floating_same(vm.call(root, [real(1.0)])!, real(5.0))! return unspecified() })! } fn test_native_exact_float_conversion_falls_back_for_fractions_large_values_and_errors() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define(f x)(inexact->exact x)) (define(compare a b c)(< a b c))')! roots := [vm.lookup('f')!, vm.lookup('compare')!] inputs := [real(-9223372036854775808.0), real(9007199254740992.0), real(-0.0), real(999.0)] vm.with_native_execution(fn [roots, inputs] (mut vm VM) !Value { for input in inputs { assert vm.call(roots[0], [input])!.whole() == i64(input.floating()) } hits, deopts := vm.native_execution_stats() assert hits == u64(inputs.len) && deopts == 0 return unspecified() })! for input in [real(0.5), real(0.1), real(9223372036854775808.0)] { expected := vm.call(roots[0], [input])! vm.with_native_execution(fn [roots, input, expected] (mut vm VM) !Value { native_floating_same(vm.call(roots[0], [input])!, expected)! _, deopts := vm.native_execution_stats() assert deopts == 1 return unspecified() })! } for args in [[real(math.nan())], [real(math.inf(1))], [real(math.inf(-1))], [real(2.0), real(1.0), boolean(false)]] { root := roots[if args.len == 1 { 0 } else { 1 }] mut expected := '' if _ := vm.call(root, args) { assert false } else { expected = err.msg().clone() } vm.with_native_execution(fn [root, args, expected] (mut vm VM) !Value { if _ := vm.call(root, args) { return error('invalid numeric call accepted') } else { assert err.msg() == expected } _, deopts := vm.native_execution_stats() assert deopts == 1 return unspecified() })! } }