vile / scheme / arithmetic_fixnum.v
249 lines · 238 sloc · 8.07 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import math.bits
4
5// These conversions never cast an out-of-range unsigned word to signed C.
6@[inline]
7fn arithmetic_signed(raw u64) i64 {
8 return if raw >> 63 == 0 { i64(raw) } else { -1 - i64(~raw) }
9}
10
11@[inline]
12fn arithmetic_sign_word(number i64) u64 {
13 return if number < 0 { ~u64(0) } else { u64(0) }
14}
15
16fn fixnum_overflow() IError { return failure('fixnum result exceeds supported range') }
17
18fn fixnum_left(number i64, count int) !i64 {
19 if count == 0 || number == 0 { return number }
20 if number > (exact_max >> count) || number < (exact_min >> count) { return fixnum_overflow() }
21 return arithmetic_signed(u64(number) << count)
22}
23
24fn fixnum_right(number i64, count int) i64 {
25 if count == 0 { return number }
26 return arithmetic_signed((u64(number) >> count) | (arithmetic_sign_word(number) << (64 - count)))
27}
28
29struct FixnumDivision {
30 quotient u64
31 negative bool
32 remainder i64
33}
34
35fn fixnum_division(a i64, b i64, centered bool) !FixnumDivision {
36 if b == 0 { return failure('fixnum division by zero') }
37 x, y := magnitude(a), magnitude(b)
38 mut q, mut r := x / y, x % y
39 mut negative := (a < 0) != (b < 0)
40 if a < 0 && r != 0 {
41 q++
42 r = y - r
43 }
44 // Euclidean quotient is ceil(x/y) for a negative divisor.
45 mut rem := i64(r)
46 if centered && r >= y - r {
47 rem = if y - r == u64(1) << 63 { exact_min } else { -i64(y - r) }
48 // The centered quotient differs by sign(b), including crossing zero.
49 if negative {
50 if b > 0 { q-- } else { q++ }
51 } else {
52 if b > 0 {
53 q++
54 } else if q == 0 {
55 q = 1
56 negative = true
57 } else {
58 q--
59 }
60 }
61 }
62 return FixnumDivision{q, negative, rem}
63}
64
65fn fixnum_division_quotient(result FixnumDivision) !i64 {
66 if result.negative {
67 if result.quotient > u64(1) << 63 { return fixnum_overflow() }
68 return if result.quotient == u64(1) << 63 { exact_min } else { -i64(result.quotient) }
69 }
70 if result.quotient > u64(exact_max) { return fixnum_overflow() }
71 return i64(result.quotient)
72}
73
74// Arithmetic kernels use two fixed words. Only numeric_values boxes the final
75// two-result Scheme value; no temporary arbitrary-precision integer is built.
76fn fixnum_carry(op FixnumOperation, a i64, b i64, c i64) (i64, i64) {
77 mut high, mut low := u64(0), u64(0)
78 if op == .multiply_carry {
79 h, l := bits.mul_64(u64(a), u64(b))
80 h0, _ := bits.sub_64(h, if a < 0 { u64(b) } else { u64(0) }, 0)
81 high, _ = bits.sub_64(h0, if b < 0 { u64(a) } else { u64(0) }, 0)
82 low = l
83 } else if op == .subtract_carry {
84 l, borrow := bits.sub_64(u64(a), u64(b), 0)
85 high, _ = bits.sub_64(arithmetic_sign_word(a), arithmetic_sign_word(b), borrow)
86 low = l
87 } else {
88 l, carry := bits.add_64(u64(a), u64(b), 0)
89 high, _ = bits.add_64(arithmetic_sign_word(a), arithmetic_sign_word(b), carry)
90 low = l
91 }
92 if op == .subtract_carry {
93 l, borrow := bits.sub_64(low, u64(c), 0)
94 high, _ = bits.sub_64(high, arithmetic_sign_word(c), borrow)
95 low = l
96 } else {
97 l, carry := bits.add_64(low, u64(c), 0)
98 high, _ = bits.add_64(high, arithmetic_sign_word(c), carry)
99 low = l
100 }
101 h, _ := bits.add_64(high, low >> 63, 0)
102 return arithmetic_signed(low), arithmetic_signed(h)
103}
104
105fn fixnum_call(op FixnumOperation, args []Value) !Value {
106 name := fixnum_native_descriptors[int(op)].name
107 minimum, maximum := match op {
108 .fixnum_width, .least_fixnum, .greatest_fixnum { 0, 0 }
109 .band, .bor, .bxor { 0, -1 }
110 .eq, .lt, .gt, .le, .ge { 2, -1 }
111 .max, .min { 1, -1 }
112 .subtract { 1, 2 }
113 .add, .multiply, .div_and_mod, .div, .mod, .div0_and_mod0, .div0, .mod0,
114 .bit_setp, .arithmetic_shift, .arithmetic_shift_left, .arithmetic_shift_right {
115 2, 2
116 }
117 .add_carry, .subtract_carry, .multiply_carry, .select, .copy_bit, .bit_field,
118 .reverse_bit_field {
119 3, 3
120 }
121 .copy_bit_field, .rotate_bit_field { 4, 4 }
122 else { 1, 1 }
123 }
124 check_arity(name, args.len, minimum, maximum)!
125 if op == .fixnump { return boolean(args[0].tag == .integer) }
126 for arg in args { if arg.tag != .integer { return failure('${name} requires fixnums') } }
127 if op == .fixnum_width { return integer(64) }
128 if op == .least_fixnum { return integer(exact_min) }
129 if op == .greatest_fixnum { return integer(exact_max) }
130 if op in [.eq, .lt, .gt, .le, .ge] {
131 mut result := true
132 for index in 1 .. args.len {
133 a, b := args[index - 1].whole(), args[index].whole()
134 result = result && match op {
135 .eq { a == b }
136 .lt { a < b }
137 .gt { a > b }
138 .le { a <= b }
139 else { a >= b }
140 }
141 }
142 return boolean(result)
143 }
144 if op in [.band, .bor, .bxor, .max, .min] {
145 mut result := if op == .band { i64(-1) } else { i64(0) }
146 if op in [.max, .min] { result = args[0].whole() }
147 for value in args {
148 n := value.whole()
149 result = match op {
150 .band { result & n }
151 .bor { result | n }
152 .bxor { result ^ n }
153 .max {
154 if n > result { n } else { result }
155 }
156 else {
157 if n < result { n } else { result }
158 }
159 }
160 }
161 return integer(result)
162 }
163 a := args[0].whole()
164 if op in [.zerop, .positivep, .negativep, .oddp, .evenp] {
165 return boolean(match op {
166 .zerop { a == 0 }
167 .positivep { a > 0 }
168 .negativep { a < 0 }
169 .oddp { a & 1 != 0 }
170 else { a & 1 == 0 }
171 })
172 }
173 if op == .complement { return integer(~a) }
174 if op == .bit_count {
175 return integer(if a >= 0 {
176 i64(bits.ones_count_64(u64(a)))
177 } else {
178 -1 - i64(bits.ones_count_64(u64(~a)))
179 })
180 }
181 if op == .length { return integer(bits.len_64(u64(if a < 0 { ~a } else { a }))) }
182 if op == .first_bit_set {
183 return integer(if a == 0 { -1 } else { bits.trailing_zeros_64(u64(a)) })
184 }
185 if op == .subtract && args.len == 1 {
186 return integer(small_subtract(0, a) or { return fixnum_overflow() })
187 }
188 b := args[1].whole()
189 if op == .add { return integer(small_add(a, b) or { return fixnum_overflow() }) }
190 if op == .subtract { return integer(small_subtract(a, b) or { return fixnum_overflow() }) }
191 if op == .multiply { return integer(small_multiply(a, b) or { return fixnum_overflow() }) }
192 if op in [.div_and_mod, .div, .mod, .div0_and_mod0, .div0, .mod0] {
193 parts := fixnum_division(a, b, op in [.div0_and_mod0, .div0, .mod0])!
194 if op in [.mod, .mod0] { return integer(parts.remainder) }
195 q := fixnum_division_quotient(parts)!
196 if op in [.div, .div0] { return integer(q) }
197 return numeric_values(integer(q), integer(parts.remainder))
198 }
199 if op == .bit_setp {
200 if b < 0 { return failure('${name} index must be nonnegative') }
201 return boolean(if b >= 63 { a < 0 } else { u64(a) & (u64(1) << int(b)) != 0 })
202 }
203 if op in [.arithmetic_shift, .arithmetic_shift_left, .arithmetic_shift_right] {
204 if b <= -64 || b >= 64 || (op != .arithmetic_shift && b < 0) {
205 return failure('${name} count is outside the fixnum width')
206 }
207 left := op != .arithmetic_shift_right && b >= 0
208 count := int(if b < 0 { -b } else { b })
209 return integer(if left { fixnum_left(a, count)! } else { fixnum_right(a, count) })
210 }
211 c := args[2].whole()
212 if op == .select { return integer((a & b) | (~a & c)) }
213 if op in [.add_carry, .subtract_carry, .multiply_carry] {
214 x, y := fixnum_carry(op, a, b, c)
215 return numeric_values(integer(x), integer(y))
216 }
217 if op == .copy_bit {
218 if b < 0 || b >= 63 || c !in [i64(0), i64(1)] {
219 return failure('${name} requires index below63 and bit0 or1')
220 }
221 mask := u64(1) << int(b)
222 return integer(arithmetic_signed(if c == 0 { u64(a) & ~mask } else { u64(a) | mask }))
223 }
224 if b < 0 || c < b || c >= 64 {
225 return failure('${name} field bounds must satisfy 0<=start<=end<64')
226 }
227 width, start := int(c - b), int(b)
228 mask := if width == 0 { u64(0) } else { (u64(1) << width) - 1 }
229 field := (u64(a) >> start) & mask
230 if op == .bit_field { return integer(i64(field)) }
231 mut replacement := u64(0)
232 if op == .reverse_bit_field {
233 replacement = if width == 0 { u64(0) } else { bits.reverse_64(field) >> (64 - width) }
234 }
235 if op == .copy_bit_field { replacement = u64(args[3].whole()) & mask }
236 if op == .rotate_bit_field {
237 d := args[3].whole()
238 if d < 0 || d > width {
239 return failure('${name} rotation count must be between0 and width')
240 }
241 count := int(d)
242 replacement = if width == 0 || count == 0 || count == width {
243 field
244 } else {
245 ((field << count) | (field >> (width - count))) & mask
246 }
247 }
248 return integer(arithmetic_signed((u64(a) & ~(mask << start)) | (replacement << start)))
249}
250