module scheme import math.bits // These conversions never cast an out-of-range unsigned word to signed C. @[inline] fn arithmetic_signed(raw u64) i64 { return if raw >> 63 == 0 { i64(raw) } else { -1 - i64(~raw) } } @[inline] fn arithmetic_sign_word(number i64) u64 { return if number < 0 { ~u64(0) } else { u64(0) } } fn fixnum_overflow() IError { return failure('fixnum result exceeds supported range') } fn fixnum_left(number i64, count int) !i64 { if count == 0 || number == 0 { return number } if number > (exact_max >> count) || number < (exact_min >> count) { return fixnum_overflow() } return arithmetic_signed(u64(number) << count) } fn fixnum_right(number i64, count int) i64 { if count == 0 { return number } return arithmetic_signed((u64(number) >> count) | (arithmetic_sign_word(number) << (64 - count))) } struct FixnumDivision { quotient u64 negative bool remainder i64 } fn fixnum_division(a i64, b i64, centered bool) !FixnumDivision { if b == 0 { return failure('fixnum division by zero') } x, y := magnitude(a), magnitude(b) mut q, mut r := x / y, x % y mut negative := (a < 0) != (b < 0) if a < 0 && r != 0 { q++ r = y - r } // Euclidean quotient is ceil(x/y) for a negative divisor. mut rem := i64(r) if centered && r >= y - r { rem = if y - r == u64(1) << 63 { exact_min } else { -i64(y - r) } // The centered quotient differs by sign(b), including crossing zero. if negative { if b > 0 { q-- } else { q++ } } else { if b > 0 { q++ } else if q == 0 { q = 1 negative = true } else { q-- } } } return FixnumDivision{q, negative, rem} } fn fixnum_division_quotient(result FixnumDivision) !i64 { if result.negative { if result.quotient > u64(1) << 63 { return fixnum_overflow() } return if result.quotient == u64(1) << 63 { exact_min } else { -i64(result.quotient) } } if result.quotient > u64(exact_max) { return fixnum_overflow() } return i64(result.quotient) } // Arithmetic kernels use two fixed words. Only numeric_values boxes the final // two-result Scheme value; no temporary arbitrary-precision integer is built. fn fixnum_carry(op FixnumOperation, a i64, b i64, c i64) (i64, i64) { mut high, mut low := u64(0), u64(0) if op == .multiply_carry { h, l := bits.mul_64(u64(a), u64(b)) h0, _ := bits.sub_64(h, if a < 0 { u64(b) } else { u64(0) }, 0) high, _ = bits.sub_64(h0, if b < 0 { u64(a) } else { u64(0) }, 0) low = l } else if op == .subtract_carry { l, borrow := bits.sub_64(u64(a), u64(b), 0) high, _ = bits.sub_64(arithmetic_sign_word(a), arithmetic_sign_word(b), borrow) low = l } else { l, carry := bits.add_64(u64(a), u64(b), 0) high, _ = bits.add_64(arithmetic_sign_word(a), arithmetic_sign_word(b), carry) low = l } if op == .subtract_carry { l, borrow := bits.sub_64(low, u64(c), 0) high, _ = bits.sub_64(high, arithmetic_sign_word(c), borrow) low = l } else { l, carry := bits.add_64(low, u64(c), 0) high, _ = bits.add_64(high, arithmetic_sign_word(c), carry) low = l } h, _ := bits.add_64(high, low >> 63, 0) return arithmetic_signed(low), arithmetic_signed(h) } fn fixnum_call(op FixnumOperation, args []Value) !Value { name := fixnum_native_descriptors[int(op)].name minimum, maximum := match op { .fixnum_width, .least_fixnum, .greatest_fixnum { 0, 0 } .band, .bor, .bxor { 0, -1 } .eq, .lt, .gt, .le, .ge { 2, -1 } .max, .min { 1, -1 } .subtract { 1, 2 } .add, .multiply, .div_and_mod, .div, .mod, .div0_and_mod0, .div0, .mod0, .bit_setp, .arithmetic_shift, .arithmetic_shift_left, .arithmetic_shift_right { 2, 2 } .add_carry, .subtract_carry, .multiply_carry, .select, .copy_bit, .bit_field, .reverse_bit_field { 3, 3 } .copy_bit_field, .rotate_bit_field { 4, 4 } else { 1, 1 } } check_arity(name, args.len, minimum, maximum)! if op == .fixnump { return boolean(args[0].tag == .integer) } for arg in args { if arg.tag != .integer { return failure('${name} requires fixnums') } } if op == .fixnum_width { return integer(64) } if op == .least_fixnum { return integer(exact_min) } if op == .greatest_fixnum { return integer(exact_max) } if op in [.eq, .lt, .gt, .le, .ge] { mut result := true for index in 1 .. args.len { a, b := args[index - 1].whole(), args[index].whole() result = result && match op { .eq { a == b } .lt { a < b } .gt { a > b } .le { a <= b } else { a >= b } } } return boolean(result) } if op in [.band, .bor, .bxor, .max, .min] { mut result := if op == .band { i64(-1) } else { i64(0) } if op in [.max, .min] { result = args[0].whole() } for value in args { n := value.whole() result = match op { .band { result & n } .bor { result | n } .bxor { result ^ n } .max { if n > result { n } else { result } } else { if n < result { n } else { result } } } } return integer(result) } a := args[0].whole() if op in [.zerop, .positivep, .negativep, .oddp, .evenp] { return boolean(match op { .zerop { a == 0 } .positivep { a > 0 } .negativep { a < 0 } .oddp { a & 1 != 0 } else { a & 1 == 0 } }) } if op == .complement { return integer(~a) } if op == .bit_count { return integer(if a >= 0 { i64(bits.ones_count_64(u64(a))) } else { -1 - i64(bits.ones_count_64(u64(~a))) }) } if op == .length { return integer(bits.len_64(u64(if a < 0 { ~a } else { a }))) } if op == .first_bit_set { return integer(if a == 0 { -1 } else { bits.trailing_zeros_64(u64(a)) }) } if op == .subtract && args.len == 1 { return integer(small_subtract(0, a) or { return fixnum_overflow() }) } b := args[1].whole() if op == .add { return integer(small_add(a, b) or { return fixnum_overflow() }) } if op == .subtract { return integer(small_subtract(a, b) or { return fixnum_overflow() }) } if op == .multiply { return integer(small_multiply(a, b) or { return fixnum_overflow() }) } if op in [.div_and_mod, .div, .mod, .div0_and_mod0, .div0, .mod0] { parts := fixnum_division(a, b, op in [.div0_and_mod0, .div0, .mod0])! if op in [.mod, .mod0] { return integer(parts.remainder) } q := fixnum_division_quotient(parts)! if op in [.div, .div0] { return integer(q) } return numeric_values(integer(q), integer(parts.remainder)) } if op == .bit_setp { if b < 0 { return failure('${name} index must be nonnegative') } return boolean(if b >= 63 { a < 0 } else { u64(a) & (u64(1) << int(b)) != 0 }) } if op in [.arithmetic_shift, .arithmetic_shift_left, .arithmetic_shift_right] { if b <= -64 || b >= 64 || (op != .arithmetic_shift && b < 0) { return failure('${name} count is outside the fixnum width') } left := op != .arithmetic_shift_right && b >= 0 count := int(if b < 0 { -b } else { b }) return integer(if left { fixnum_left(a, count)! } else { fixnum_right(a, count) }) } c := args[2].whole() if op == .select { return integer((a & b) | (~a & c)) } if op in [.add_carry, .subtract_carry, .multiply_carry] { x, y := fixnum_carry(op, a, b, c) return numeric_values(integer(x), integer(y)) } if op == .copy_bit { if b < 0 || b >= 63 || c !in [i64(0), i64(1)] { return failure('${name} requires index below63 and bit0 or1') } mask := u64(1) << int(b) return integer(arithmetic_signed(if c == 0 { u64(a) & ~mask } else { u64(a) | mask })) } if b < 0 || c < b || c >= 64 { return failure('${name} field bounds must satisfy 0<=start<=end<64') } width, start := int(c - b), int(b) mask := if width == 0 { u64(0) } else { (u64(1) << width) - 1 } field := (u64(a) >> start) & mask if op == .bit_field { return integer(i64(field)) } mut replacement := u64(0) if op == .reverse_bit_field { replacement = if width == 0 { u64(0) } else { bits.reverse_64(field) >> (64 - width) } } if op == .copy_bit_field { replacement = u64(args[3].whole()) & mask } if op == .rotate_bit_field { d := args[3].whole() if d < 0 || d > width { return failure('${name} rotation count must be between0 and width') } count := int(d) replacement = if width == 0 || count == 0 || count == width { field } else { ((field << count) | (field >> (width - count))) & mask } } return integer(arithmetic_signed((u64(a) & ~(mask << start)) | (replacement << start))) }