| 1 | module scheme |
| 2 | |
| 3 | import math.big |
| 4 | import math.bits |
| 5 | |
| 6 | // A portable signed31-bit storage ceiling includes every constructed temporary. |
| 7 | // Reserve one bit for mask/complement intermediates before boxing results. |
| 8 | const arithmetic_max_bits = 2147483646 |
| 9 | |
| 10 | fn bitwise_supported(value Value) ! { |
| 11 | if bitwise_magnitude_bits(value) > arithmetic_max_bits { |
| 12 | return failure('bitwise operand exceeds supported bit range') |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | fn bitwise_growth(width int, count int) ! { |
| 17 | if width < 0 || count < 0 || width > arithmetic_max_bits || count > arithmetic_max_bits - width { |
| 18 | return failure('bitwise result exceeds supported bit range') |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | fn bitwise_owned(value big.Integer) !Value { |
| 23 | if value.bit_len() > arithmetic_max_bits { |
| 24 | return failure('bitwise result exceeds supported bit range') |
| 25 | } |
| 26 | return owned_big_integer(value) |
| 27 | } |
| 28 | |
| 29 | fn bitwise_index(value Value) ! { |
| 30 | if !is_exact_integer(value) || exact_sign(value) < 0 { |
| 31 | return failure('bitwise index must be a nonnegative exact integer') |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn bitwise_bounded_index(value Value) !int { |
| 36 | bitwise_index(value)! |
| 37 | if value.tag != .integer || value.whole() > arithmetic_max_bits { |
| 38 | return failure('bitwise result exceeds supported bit range') |
| 39 | } |
| 40 | return int(value.whole()) |
| 41 | } |
| 42 | |
| 43 | fn bitwise_magnitude_bits(value Value) int { |
| 44 | return if value.tag == .integer { |
| 45 | bits.len_64(magnitude(value.whole())) |
| 46 | } else { |
| 47 | value.big_integer_pointer().value.bit_len() |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn bitwise_complement(value Value) !Value { |
| 52 | if value.tag == .integer { return integer(~value.whole()) } |
| 53 | bitwise_supported(value)! |
| 54 | return bitwise_owned(big_neg_view(value.big_integer_pointer().value + big.one_int)) |
| 55 | } |
| 56 | |
| 57 | // The stdlib bitwise operations are magnitude-only. Complemented nonnegative |
| 58 | // operands reduce all infinite two's-complement sign combinations to those APIs. |
| 59 | fn bitwise_binary(op BitwiseOperation, a Value, b Value) !Value { |
| 60 | if a.tag == .integer && b.tag == .integer { |
| 61 | return integer(match op { |
| 62 | .band { a.whole() & b.whole() } |
| 63 | .bor { a.whole() | b.whole() } |
| 64 | else { a.whole() ^ b.whole() } |
| 65 | }) |
| 66 | } |
| 67 | if a.tag == .integer { |
| 68 | n := a.whole() |
| 69 | if op == .band && n == 0 { return a } |
| 70 | if op == .bor && n == -1 { return a } |
| 71 | if (op == .band && n == -1) || (op in [.bor, .bxor] && n == 0) { return b } |
| 72 | if op == .bxor && n == -1 { return bitwise_complement(b) } |
| 73 | } else if b.tag == .integer { |
| 74 | return bitwise_binary(op, b, a) |
| 75 | } |
| 76 | bitwise_supported(a)! |
| 77 | bitwise_supported(b)! |
| 78 | x, y := big_integer_data(a)!, big_integer_data(b)! |
| 79 | xn, yn := x.signum < 0, y.signum < 0 |
| 80 | left := if xn { big_abs_view(x) - big.one_int } else { x } |
| 81 | right := if yn { big_abs_view(y) - big.one_int } else { y } |
| 82 | mut result := big.zero_int |
| 83 | mut complement := false |
| 84 | if op == .bxor { |
| 85 | result = left.bitwise_xor(right) |
| 86 | complement = xn != yn |
| 87 | } else if op == .band { |
| 88 | if xn && yn { |
| 89 | result = left.bitwise_or(right) |
| 90 | complement = true |
| 91 | } else if xn { |
| 92 | result = right - left.bitwise_and(right) |
| 93 | } else if yn { |
| 94 | result = left - left.bitwise_and(right) |
| 95 | } else { |
| 96 | result = left.bitwise_and(right) |
| 97 | } |
| 98 | } else { |
| 99 | if xn && yn { |
| 100 | result = left.bitwise_and(right) |
| 101 | complement = true |
| 102 | } else if xn { |
| 103 | result = left - left.bitwise_and(right) |
| 104 | complement = true |
| 105 | } else if yn { |
| 106 | result = right - left.bitwise_and(right) |
| 107 | complement = true |
| 108 | } else { |
| 109 | result = left.bitwise_or(right) |
| 110 | } |
| 111 | } |
| 112 | return bitwise_owned(if complement { big_neg_view(result + big.one_int) } else { result }) |
| 113 | } |
| 114 | |
| 115 | fn bitwise_select(mask Value, yes Value, no Value) !Value { |
| 116 | return bitwise_binary(.bor, bitwise_binary(.band, mask, yes)!, bitwise_binary(.band, bitwise_complement(mask)!, no)!) |
| 117 | } |
| 118 | |
| 119 | fn bitwise_bit(value Value, index Value) !bool { |
| 120 | bitwise_index(index)! |
| 121 | width := bitwise_magnitude_bits(value) |
| 122 | if index.tag == .big_integer || index.whole() >= width { return exact_sign(value) < 0 } |
| 123 | position := int(index.whole()) |
| 124 | if value.tag == .integer { return u64(value.whole()) & (u64(1) << position) != 0 } |
| 125 | return value.big_integer_pointer().value.get_bit(u32(position)) |
| 126 | } |
| 127 | |
| 128 | fn bitwise_shift(value Value, index Value, left bool) !Value { |
| 129 | bitwise_index(index)! |
| 130 | if exact_sign(value) == 0 { return value } |
| 131 | width := bitwise_magnitude_bits(value) |
| 132 | if !left && (index.tag == .big_integer || index.whole() >= width) { |
| 133 | return integer(if exact_sign(value) < 0 { -1 } else { 0 }) |
| 134 | } |
| 135 | count := bitwise_bounded_index(index)! |
| 136 | if count == 0 { return value } |
| 137 | if value.tag == .integer { |
| 138 | if !left { return integer(fixnum_right(value.whole(), count)) } |
| 139 | if count < 64 { |
| 140 | if shifted := fixnum_left(value.whole(), count) { return integer(shifted) } |
| 141 | } |
| 142 | } |
| 143 | bitwise_supported(value)! |
| 144 | if left { bitwise_growth(width, count)! } |
| 145 | x := big_integer_data(value)! |
| 146 | if left { return bitwise_owned(x.left_shift(u32(count))) } |
| 147 | if x.signum >= 0 { return bitwise_owned(x.right_shift(u32(count))) } |
| 148 | positive := (big_abs_view(x) - big.one_int).right_shift(u32(count)) |
| 149 | return bitwise_owned(big_neg_view(positive + big.one_int)) |
| 150 | } |
| 151 | |
| 152 | fn bitwise_mask(width int) !Value { |
| 153 | bitwise_growth(0, width)! |
| 154 | if width < 63 { return integer(i64((u64(1) << width) - 1)) } |
| 155 | return bitwise_owned(big.one_int.left_shift(u32(width)) - big.one_int) |
| 156 | } |
| 157 | |
| 158 | fn bitwise_field(value Value, start int, end int) !Value { |
| 159 | if start == end { return integer(0) } |
| 160 | return bitwise_binary(.band, bitwise_shift(value, integer(start), false)!, bitwise_mask(end - start)!) |
| 161 | } |
| 162 | |
| 163 | fn bitwise_copy_field(value Value, start int, end int, source Value) !Value { |
| 164 | if start == end { return value } |
| 165 | mask := bitwise_mask(end - start)! |
| 166 | from := bitwise_binary(.band, source, mask)! |
| 167 | return bitwise_select(bitwise_shift(mask, integer(start), true)!, bitwise_shift(from, integer(start), true)!, value) |
| 168 | } |
| 169 | |
| 170 | // Magnitude access avoids the negative get_bit lower-digit borrow scan for each |
| 171 | // bit. These public APIs perform no copies or temporary large-number arithmetic. |
| 172 | fn bitwise_stat(op BitwiseOperation, value Value) !Value { |
| 173 | if value.tag == .integer { |
| 174 | n := value.whole() |
| 175 | return integer(match op { |
| 176 | .bit_count { |
| 177 | if n < 0 { |
| 178 | -1 - i64(bits.ones_count_64(u64(~n))) |
| 179 | } else { |
| 180 | i64(bits.ones_count_64(u64(n))) |
| 181 | } |
| 182 | } |
| 183 | .length { bits.len_64(u64(if n < 0 { ~n } else { n })) } |
| 184 | else { |
| 185 | if n == 0 { -1 } else { bits.trailing_zeros_64(u64(n)) } |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | x := value.big_integer_pointer().value |
| 190 | absolute := big_abs_view(x) |
| 191 | length := absolute.bit_len() |
| 192 | if op == .length { return integer(length - int(x.signum < 0 && absolute.is_power_of_2())) } |
| 193 | bitwise_supported(value)! |
| 194 | mut count := i64(0) |
| 195 | mut first := -1 |
| 196 | for offset := 0; offset < length; { |
| 197 | amount := if length - offset < 32 { length - offset } else { 32 } |
| 198 | mut word := u64(0) |
| 199 | for bit in 0 .. amount { if absolute.get_bit(u32(offset + bit)) { word |= u64(1) << bit } } |
| 200 | if first < 0 && word != 0 { |
| 201 | first = offset + bits.trailing_zeros_64(word) |
| 202 | if op == .first_bit_set { return integer(first) } |
| 203 | } |
| 204 | count += bits.ones_count_64(word) |
| 205 | offset += amount |
| 206 | } |
| 207 | if op == .first_bit_set { return integer(-1) } |
| 208 | return integer(if x.signum < 0 { -count - first } else { count }) |
| 209 | } |
| 210 | |
| 211 | fn bitwise_call(op BitwiseOperation, args []Value) !Value { |
| 212 | name := bitwise_native_descriptors[int(op)].name |
| 213 | minimum, maximum := match op { |
| 214 | .band, .bor, .bxor { 0, -1 } |
| 215 | .bit_setp, .arithmetic_shift, .arithmetic_shift_left, .arithmetic_shift_right { 2, 2 } |
| 216 | .select, .copy_bit, .bit_field, .reverse_bit_field { 3, 3 } |
| 217 | .copy_bit_field, .rotate_bit_field { 4, 4 } |
| 218 | else { 1, 1 } |
| 219 | } |
| 220 | check_arity(name, args.len, minimum, maximum)! |
| 221 | for arg in args { |
| 222 | if !is_exact_integer(arg) { return failure('${name} requires exact integers') } |
| 223 | } |
| 224 | if op in [.band, .bor, .bxor] { |
| 225 | if args.len == 0 { return integer(if op == .band { -1 } else { 0 }) } |
| 226 | mut result := args[0] |
| 227 | for index in 1 .. args.len { result = bitwise_binary(op, result, args[index])! } |
| 228 | return result |
| 229 | } |
| 230 | value := args[0] |
| 231 | if op == .complement { return bitwise_complement(value) } |
| 232 | if op in [.bit_count, .length, .first_bit_set] { return bitwise_stat(op, value) } |
| 233 | if op == .select { return bitwise_select(value, args[1], args[2]) } |
| 234 | if op == .bit_setp { return boolean(bitwise_bit(value, args[1])!) } |
| 235 | if op in [.arithmetic_shift, .arithmetic_shift_left, .arithmetic_shift_right] { |
| 236 | count := args[1] |
| 237 | if op == .arithmetic_shift && exact_sign(count) < 0 { |
| 238 | return bitwise_shift(value, numeric_negate(count)!, false) |
| 239 | } |
| 240 | return bitwise_shift(value, count, op != .arithmetic_shift_right) |
| 241 | } |
| 242 | if op == .copy_bit { |
| 243 | bitwise_index(args[1])! |
| 244 | if args[2].tag != .integer || args[2].whole() !in [i64(0), i64(1)] { |
| 245 | return failure('${name} bit must be0 or1') |
| 246 | } |
| 247 | desired := args[2].whole() == 1 |
| 248 | if bitwise_bit(value, args[1])! == desired { return value } |
| 249 | index := bitwise_bounded_index(args[1])! |
| 250 | if index == arithmetic_max_bits { |
| 251 | return failure('bitwise result exceeds supported bit range') |
| 252 | } |
| 253 | if index < 63 && value.tag == .integer { |
| 254 | mask := u64(1) << index |
| 255 | return integer(arithmetic_signed(if desired { |
| 256 | u64(value.whole()) | mask |
| 257 | } else { |
| 258 | u64(value.whole()) & ~mask |
| 259 | })) |
| 260 | } |
| 261 | mask := bitwise_shift(integer(1), integer(index), true)! |
| 262 | return bitwise_select(mask, if desired { integer(-1) } else { integer(0) }, value) |
| 263 | } |
| 264 | bitwise_index(args[1])! |
| 265 | bitwise_index(args[2])! |
| 266 | if numeric_compare(args[1], args[2])! > 0 { return failure('${name} requires start<=end') } |
| 267 | if op == .rotate_bit_field { bitwise_index(args[3])! } |
| 268 | if eqv(args[1], args[2]) { return if op == .bit_field { integer(0) } else { value } } |
| 269 | if op == .bit_field && exact_sign(value) == 0 { return value } |
| 270 | start, end := bitwise_bounded_index(args[1])!, bitwise_bounded_index(args[2])! |
| 271 | width := end - start |
| 272 | if op == .bit_field { return bitwise_field(value, start, end) } |
| 273 | if op == .copy_bit_field { return bitwise_copy_field(value, start, end, args[3]) } |
| 274 | field := bitwise_field(value, start, end)! |
| 275 | if op == .rotate_bit_field { |
| 276 | count_value := integer_division(.remainder, args[3], integer(width))! |
| 277 | count := int(count_value.whole()) |
| 278 | if count == 0 { return value } |
| 279 | rotated := bitwise_binary(.bor, bitwise_shift(field, integer(count), true)!, bitwise_shift(field, integer(width - count), false)!)! |
| 280 | return bitwise_copy_field(value, start, end, rotated) |
| 281 | } |
| 282 | if width <= 63 && field.tag == .integer { |
| 283 | reversed := bits.reverse_64(u64(field.whole())) >> (64 - width) |
| 284 | return bitwise_copy_field(value, start, end, integer(i64(reversed))) |
| 285 | } |
| 286 | input := big_integer_data(field)! |
| 287 | mut bytes := []u8{len: width / 8 + int(width % 8 != 0)} |
| 288 | for index in 0 .. width { |
| 289 | if input.get_bit(u32(index)) { |
| 290 | position := width - index - 1 |
| 291 | bytes[bytes.len - position / 8 - 1] |= u8(1) << (position % 8) |
| 292 | } |
| 293 | } |
| 294 | result := bitwise_owned(big.integer_from_bytes(bytes))! |
| 295 | return bitwise_copy_field(value, start, end, result) |
| 296 | } |
| 297 | |