| ... | @@ -1062,14 +1062,7 @@ pub const Value = extern union { | ... | @@ -1062,14 +1062,7 @@ pub const Value = extern union { |
| 1062 | const limbs_buffer = try arena.alloc(std.math.big.Limb, 2); | 1062 | const limbs_buffer = try arena.alloc(std.math.big.Limb, 2); |
| 1063 | var bigint = BigIntMutable.init(limbs_buffer, 0); | 1063 | var bigint = BigIntMutable.init(limbs_buffer, 0); |
| 1064 | bigint.readTwosComplement(buffer, int_info.bits, endian, int_info.signedness); | 1064 | bigint.readTwosComplement(buffer, int_info.bits, endian, int_info.signedness); |
| 1065 | // TODO if it fits in 64 bits then use one of those tags | 1065 | return fromBigInt(arena, bigint.toConst()); |
| 1066 | | | |
| 1067 | const result_limbs = bigint.limbs[0..bigint.len]; | | |
| 1068 | if (bigint.positive) { | | |
| 1069 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 1070 | } else { | | |
| 1071 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 1072 | } | | |
| 1073 | }, | 1066 | }, |
| 1074 | .Float => switch (ty.floatBits(target)) { | 1067 | .Float => switch (ty.floatBits(target)) { |
| 1075 | 16 => return Value.Tag.float_16.create(arena, floatReadFromMemory(f16, target, buffer)), | 1068 | 16 => return Value.Tag.float_16.create(arena, floatReadFromMemory(f16, target, buffer)), |
| ... | @@ -1200,16 +1193,34 @@ pub const Value = extern union { | ... | @@ -1200,16 +1193,34 @@ pub const Value = extern union { |
| 1200 | if (x == 0) return 0; | 1193 | if (x == 0) return 0; |
| 1201 | return @intCast(usize, std.math.log2(x) + 1); | 1194 | return @intCast(usize, std.math.log2(x) + 1); |
| 1202 | }, | 1195 | }, |
| 1203 | .int_i64 => { | | |
| 1204 | @panic("TODO implement i64 intBitCountTwosComp"); | | |
| 1205 | }, | | |
| 1206 | .int_big_positive => return self.castTag(.int_big_positive).?.asBigInt().bitCountTwosComp(), | 1196 | .int_big_positive => return self.castTag(.int_big_positive).?.asBigInt().bitCountTwosComp(), |
| 1207 | .int_big_negative => return self.castTag(.int_big_negative).?.asBigInt().bitCountTwosComp(), | 1197 | .int_big_negative => return self.castTag(.int_big_negative).?.asBigInt().bitCountTwosComp(), |
| 1208 | | 1198 | |
| 1209 | else => unreachable, | 1199 | else => { |
| | 1200 | var buffer: BigIntSpace = undefined; |
| | 1201 | return self.toBigInt(&buffer).bitCountTwosComp(); |
| | 1202 | }, |
| 1210 | } | 1203 | } |
| 1211 | } | 1204 | } |
| 1212 | | 1205 | |
| | 1206 | pub fn popCount(val: Value, ty: Type, target: Target, arena: *Allocator) !Value { |
| | 1207 | assert(!val.isUndef()); |
| | 1208 | |
| | 1209 | const info = ty.intInfo(target); |
| | 1210 | |
| | 1211 | var buffer: Value.BigIntSpace = undefined; |
| | 1212 | const operand_bigint = val.toBigInt(&buffer); |
| | 1213 | |
| | 1214 | const limbs = try arena.alloc( |
| | 1215 | std.math.big.Limb, |
| | 1216 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| | 1217 | ); |
| | 1218 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| | 1219 | result_bigint.popCount(operand_bigint, info.bits); |
| | 1220 | |
| | 1221 | return fromBigInt(arena, result_bigint.toConst()); |
| | 1222 | } |
| | 1223 | |
| 1213 | /// Asserts the value is an integer, and the destination type is ComptimeInt or Int. | 1224 | /// Asserts the value is an integer, and the destination type is ComptimeInt or Int. |
| 1214 | pub fn intFitsInType(self: Value, ty: Type, target: Target) bool { | 1225 | pub fn intFitsInType(self: Value, ty: Type, target: Target) bool { |
| 1215 | switch (self.tag()) { | 1226 | switch (self.tag()) { |
| ... | @@ -1246,7 +1257,8 @@ pub const Value = extern union { | ... | @@ -1246,7 +1257,8 @@ pub const Value = extern union { |
| 1246 | const info = ty.intInfo(target); | 1257 | const info = ty.intInfo(target); |
| 1247 | if (info.signedness == .unsigned and x < 0) | 1258 | if (info.signedness == .unsigned and x < 0) |
| 1248 | return false; | 1259 | return false; |
| 1249 | @panic("TODO implement i64 intFitsInType"); | 1260 | var buffer: BigIntSpace = undefined; |
| | 1261 | return self.toBigInt(&buffer).fitsInTwosComp(info.signedness, info.bits); |
| 1250 | }, | 1262 | }, |
| 1251 | .ComptimeInt => return true, | 1263 | .ComptimeInt => return true, |
| 1252 | else => unreachable, | 1264 | else => unreachable, |
| ... | @@ -1943,12 +1955,22 @@ pub const Value = extern union { | ... | @@ -1943,12 +1955,22 @@ pub const Value = extern union { |
| 1943 | ); | 1955 | ); |
| 1944 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 1956 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 1945 | result_bigint.addWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits); | 1957 | result_bigint.addWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits); |
| 1946 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 1958 | return fromBigInt(arena, result_bigint.toConst()); |
| | 1959 | } |
| 1947 | | 1960 | |
| 1948 | if (result_bigint.positive) { | 1961 | fn fromBigInt(arena: *Allocator, big_int: BigIntConst) !Value { |
| 1949 | return Value.Tag.int_big_positive.create(arena, result_limbs); | 1962 | if (big_int.positive) { |
| | 1963 | if (big_int.to(u64)) |x| { |
| | 1964 | return Value.Tag.int_u64.create(arena, x); |
| | 1965 | } else |_| { |
| | 1966 | return Value.Tag.int_big_positive.create(arena, big_int.limbs); |
| | 1967 | } |
| 1950 | } else { | 1968 | } else { |
| 1951 | return Value.Tag.int_big_negative.create(arena, result_limbs); | 1969 | if (big_int.to(i64)) |x| { |
| | 1970 | return Value.Tag.int_i64.create(arena, x); |
| | 1971 | } else |_| { |
| | 1972 | return Value.Tag.int_big_negative.create(arena, big_int.limbs); |
| | 1973 | } |
| 1952 | } | 1974 | } |
| 1953 | } | 1975 | } |
| 1954 | | 1976 | |
| ... | @@ -1975,13 +1997,7 @@ pub const Value = extern union { | ... | @@ -1975,13 +1997,7 @@ pub const Value = extern union { |
| 1975 | ); | 1997 | ); |
| 1976 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 1998 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 1977 | result_bigint.addSat(lhs_bigint, rhs_bigint, info.signedness, info.bits); | 1999 | result_bigint.addSat(lhs_bigint, rhs_bigint, info.signedness, info.bits); |
| 1978 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2000 | return fromBigInt(arena, result_bigint.toConst()); |
| 1979 | | | |
| 1980 | if (result_bigint.positive) { | | |
| 1981 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 1982 | } else { | | |
| 1983 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 1984 | } | | |
| 1985 | } | 2001 | } |
| 1986 | | 2002 | |
| 1987 | /// Supports both floats and ints; handles undefined. | 2003 | /// Supports both floats and ints; handles undefined. |
| ... | @@ -2010,13 +2026,7 @@ pub const Value = extern union { | ... | @@ -2010,13 +2026,7 @@ pub const Value = extern union { |
| 2010 | ); | 2026 | ); |
| 2011 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2027 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2012 | result_bigint.subWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits); | 2028 | result_bigint.subWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits); |
| 2013 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2029 | return fromBigInt(arena, result_bigint.toConst()); |
| 2014 | | | |
| 2015 | if (result_bigint.positive) { | | |
| 2016 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2017 | } else { | | |
| 2018 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2019 | } | | |
| 2020 | } | 2030 | } |
| 2021 | | 2031 | |
| 2022 | /// Supports integers only; asserts neither operand is undefined. | 2032 | /// Supports integers only; asserts neither operand is undefined. |
| ... | @@ -2042,13 +2052,7 @@ pub const Value = extern union { | ... | @@ -2042,13 +2052,7 @@ pub const Value = extern union { |
| 2042 | ); | 2052 | ); |
| 2043 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2053 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2044 | result_bigint.subSat(lhs_bigint, rhs_bigint, info.signedness, info.bits); | 2054 | result_bigint.subSat(lhs_bigint, rhs_bigint, info.signedness, info.bits); |
| 2045 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2055 | return fromBigInt(arena, result_bigint.toConst()); |
| 2046 | | | |
| 2047 | if (result_bigint.positive) { | | |
| 2048 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2049 | } else { | | |
| 2050 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2051 | } | | |
| 2052 | } | 2056 | } |
| 2053 | | 2057 | |
| 2054 | /// Supports both floats and ints; handles undefined. | 2058 | /// Supports both floats and ints; handles undefined. |
| ... | @@ -2082,13 +2086,7 @@ pub const Value = extern union { | ... | @@ -2082,13 +2086,7 @@ pub const Value = extern union { |
| 2082 | ); | 2086 | ); |
| 2083 | defer arena.free(limbs_buffer); | 2087 | defer arena.free(limbs_buffer); |
| 2084 | result_bigint.mulWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits, limbs_buffer, arena); | 2088 | result_bigint.mulWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits, limbs_buffer, arena); |
| 2085 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2089 | return fromBigInt(arena, result_bigint.toConst()); |
| 2086 | | | |
| 2087 | if (result_bigint.positive) { | | |
| 2088 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2089 | } else { | | |
| 2090 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2091 | } | | |
| 2092 | } | 2090 | } |
| 2093 | | 2091 | |
| 2094 | /// Supports integers only; asserts neither operand is undefined. | 2092 | /// Supports integers only; asserts neither operand is undefined. |
| ... | @@ -2124,13 +2122,7 @@ pub const Value = extern union { | ... | @@ -2124,13 +2122,7 @@ pub const Value = extern union { |
| 2124 | defer arena.free(limbs_buffer); | 2122 | defer arena.free(limbs_buffer); |
| 2125 | result_bigint.mul(lhs_bigint, rhs_bigint, limbs_buffer, arena); | 2123 | result_bigint.mul(lhs_bigint, rhs_bigint, limbs_buffer, arena); |
| 2126 | result_bigint.saturate(result_bigint.toConst(), info.signedness, info.bits); | 2124 | result_bigint.saturate(result_bigint.toConst(), info.signedness, info.bits); |
| 2127 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2125 | return fromBigInt(arena, result_bigint.toConst()); |
| 2128 | | | |
| 2129 | if (result_bigint.positive) { | | |
| 2130 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2131 | } else { | | |
| 2132 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2133 | } | | |
| 2134 | } | 2126 | } |
| 2135 | | 2127 | |
| 2136 | /// Supports both floats and ints; handles undefined. | 2128 | /// Supports both floats and ints; handles undefined. |
| ... | @@ -2174,13 +2166,7 @@ pub const Value = extern union { | ... | @@ -2174,13 +2166,7 @@ pub const Value = extern union { |
| 2174 | | 2166 | |
| 2175 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2167 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2176 | result_bigint.bitNotWrap(val_bigint, info.signedness, info.bits); | 2168 | result_bigint.bitNotWrap(val_bigint, info.signedness, info.bits); |
| 2177 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2169 | return fromBigInt(arena, result_bigint.toConst()); |
| 2178 | | | |
| 2179 | if (result_bigint.positive) { | | |
| 2180 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2181 | } else { | | |
| 2182 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2183 | } | | |
| 2184 | } | 2170 | } |
| 2185 | | 2171 | |
| 2186 | /// operands must be integers; handles undefined. | 2172 | /// operands must be integers; handles undefined. |
| ... | @@ -2200,13 +2186,7 @@ pub const Value = extern union { | ... | @@ -2200,13 +2186,7 @@ pub const Value = extern union { |
| 2200 | ); | 2186 | ); |
| 2201 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2187 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2202 | result_bigint.bitAnd(lhs_bigint, rhs_bigint); | 2188 | result_bigint.bitAnd(lhs_bigint, rhs_bigint); |
| 2203 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2189 | return fromBigInt(arena, result_bigint.toConst()); |
| 2204 | | | |
| 2205 | if (result_bigint.positive) { | | |
| 2206 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2207 | } else { | | |
| 2208 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2209 | } | | |
| 2210 | } | 2190 | } |
| 2211 | | 2191 | |
| 2212 | /// operands must be integers; handles undefined. | 2192 | /// operands must be integers; handles undefined. |
| ... | @@ -2239,13 +2219,7 @@ pub const Value = extern union { | ... | @@ -2239,13 +2219,7 @@ pub const Value = extern union { |
| 2239 | ); | 2219 | ); |
| 2240 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2220 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2241 | result_bigint.bitOr(lhs_bigint, rhs_bigint); | 2221 | result_bigint.bitOr(lhs_bigint, rhs_bigint); |
| 2242 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2222 | return fromBigInt(arena, result_bigint.toConst()); |
| 2243 | | | |
| 2244 | if (result_bigint.positive) { | | |
| 2245 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2246 | } else { | | |
| 2247 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2248 | } | | |
| 2249 | } | 2223 | } |
| 2250 | | 2224 | |
| 2251 | /// operands must be integers; handles undefined. | 2225 | /// operands must be integers; handles undefined. |
| ... | @@ -2265,13 +2239,7 @@ pub const Value = extern union { | ... | @@ -2265,13 +2239,7 @@ pub const Value = extern union { |
| 2265 | ); | 2239 | ); |
| 2266 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2240 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2267 | result_bigint.bitXor(lhs_bigint, rhs_bigint); | 2241 | result_bigint.bitXor(lhs_bigint, rhs_bigint); |
| 2268 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2242 | return fromBigInt(arena, result_bigint.toConst()); |
| 2269 | | | |
| 2270 | if (result_bigint.positive) { | | |
| 2271 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2272 | } else { | | |
| 2273 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2274 | } | | |
| 2275 | } | 2243 | } |
| 2276 | | 2244 | |
| 2277 | pub fn intAdd(lhs: Value, rhs: Value, allocator: *Allocator) !Value { | 2245 | pub fn intAdd(lhs: Value, rhs: Value, allocator: *Allocator) !Value { |
| ... | @@ -2287,13 +2255,7 @@ pub const Value = extern union { | ... | @@ -2287,13 +2255,7 @@ pub const Value = extern union { |
| 2287 | ); | 2255 | ); |
| 2288 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2256 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2289 | result_bigint.add(lhs_bigint, rhs_bigint); | 2257 | result_bigint.add(lhs_bigint, rhs_bigint); |
| 2290 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2258 | return fromBigInt(allocator, result_bigint.toConst()); |
| 2291 | | | |
| 2292 | if (result_bigint.positive) { | | |
| 2293 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2294 | } else { | | |
| 2295 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2296 | } | | |
| 2297 | } | 2259 | } |
| 2298 | | 2260 | |
| 2299 | pub fn intSub(lhs: Value, rhs: Value, allocator: *Allocator) !Value { | 2261 | pub fn intSub(lhs: Value, rhs: Value, allocator: *Allocator) !Value { |
| ... | @@ -2309,13 +2271,7 @@ pub const Value = extern union { | ... | @@ -2309,13 +2271,7 @@ pub const Value = extern union { |
| 2309 | ); | 2271 | ); |
| 2310 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2272 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2311 | result_bigint.sub(lhs_bigint, rhs_bigint); | 2273 | result_bigint.sub(lhs_bigint, rhs_bigint); |
| 2312 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2274 | return fromBigInt(allocator, result_bigint.toConst()); |
| 2313 | | | |
| 2314 | if (result_bigint.positive) { | | |
| 2315 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2316 | } else { | | |
| 2317 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2318 | } | | |
| 2319 | } | 2275 | } |
| 2320 | | 2276 | |
| 2321 | pub fn intDiv(lhs: Value, rhs: Value, allocator: *Allocator) !Value { | 2277 | pub fn intDiv(lhs: Value, rhs: Value, allocator: *Allocator) !Value { |
| ... | @@ -2340,13 +2296,7 @@ pub const Value = extern union { | ... | @@ -2340,13 +2296,7 @@ pub const Value = extern union { |
| 2340 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; | 2296 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; |
| 2341 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; | 2297 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; |
| 2342 | result_q.divTrunc(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); | 2298 | result_q.divTrunc(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); |
| 2343 | const result_limbs = result_q.limbs[0..result_q.len]; | 2299 | return fromBigInt(allocator, result_q.toConst()); |
| 2344 | | | |
| 2345 | if (result_q.positive) { | | |
| 2346 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2347 | } else { | | |
| 2348 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2349 | } | | |
| 2350 | } | 2300 | } |
| 2351 | | 2301 | |
| 2352 | pub fn intDivFloor(lhs: Value, rhs: Value, allocator: *Allocator) !Value { | 2302 | pub fn intDivFloor(lhs: Value, rhs: Value, allocator: *Allocator) !Value { |
| ... | @@ -2371,13 +2321,7 @@ pub const Value = extern union { | ... | @@ -2371,13 +2321,7 @@ pub const Value = extern union { |
| 2371 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; | 2321 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; |
| 2372 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; | 2322 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; |
| 2373 | result_q.divFloor(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); | 2323 | result_q.divFloor(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); |
| 2374 | const result_limbs = result_q.limbs[0..result_q.len]; | 2324 | return fromBigInt(allocator, result_q.toConst()); |
| 2375 | | | |
| 2376 | if (result_q.positive) { | | |
| 2377 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2378 | } else { | | |
| 2379 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2380 | } | | |
| 2381 | } | 2325 | } |
| 2382 | | 2326 | |
| 2383 | pub fn intRem(lhs: Value, rhs: Value, allocator: *Allocator) !Value { | 2327 | pub fn intRem(lhs: Value, rhs: Value, allocator: *Allocator) !Value { |
| ... | @@ -2404,13 +2348,7 @@ pub const Value = extern union { | ... | @@ -2404,13 +2348,7 @@ pub const Value = extern union { |
| 2404 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; | 2348 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; |
| 2405 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; | 2349 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; |
| 2406 | result_q.divTrunc(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); | 2350 | result_q.divTrunc(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); |
| 2407 | const result_limbs = result_r.limbs[0..result_r.len]; | 2351 | return fromBigInt(allocator, result_r.toConst()); |
| 2408 | | | |
| 2409 | if (result_r.positive) { | | |
| 2410 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2411 | } else { | | |
| 2412 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2413 | } | | |
| 2414 | } | 2352 | } |
| 2415 | | 2353 | |
| 2416 | pub fn intMod(lhs: Value, rhs: Value, allocator: *Allocator) !Value { | 2354 | pub fn intMod(lhs: Value, rhs: Value, allocator: *Allocator) !Value { |
| ... | @@ -2435,13 +2373,7 @@ pub const Value = extern union { | ... | @@ -2435,13 +2373,7 @@ pub const Value = extern union { |
| 2435 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; | 2373 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; |
| 2436 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; | 2374 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; |
| 2437 | result_q.divFloor(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); | 2375 | result_q.divFloor(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); |
| 2438 | const result_limbs = result_r.limbs[0..result_r.len]; | 2376 | return fromBigInt(allocator, result_r.toConst()); |
| 2439 | | | |
| 2440 | if (result_r.positive) { | | |
| 2441 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2442 | } else { | | |
| 2443 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2444 | } | | |
| 2445 | } | 2377 | } |
| 2446 | | 2378 | |
| 2447 | /// Returns true if the value is a floating point type and is NaN. Returns false otherwise. | 2379 | /// Returns true if the value is a floating point type and is NaN. Returns false otherwise. |
| ... | @@ -2487,13 +2419,7 @@ pub const Value = extern union { | ... | @@ -2487,13 +2419,7 @@ pub const Value = extern union { |
| 2487 | ); | 2419 | ); |
| 2488 | defer allocator.free(limbs_buffer); | 2420 | defer allocator.free(limbs_buffer); |
| 2489 | result_bigint.mul(lhs_bigint, rhs_bigint, limbs_buffer, allocator); | 2421 | result_bigint.mul(lhs_bigint, rhs_bigint, limbs_buffer, allocator); |
| 2490 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2422 | return fromBigInt(allocator, result_bigint.toConst()); |
| 2491 | | | |
| 2492 | if (result_bigint.positive) { | | |
| 2493 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2494 | } else { | | |
| 2495 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2496 | } | | |
| 2497 | } | 2423 | } |
| 2498 | | 2424 | |
| 2499 | pub fn intTrunc(val: Value, allocator: *Allocator, signedness: std.builtin.Signedness, bits: u16) !Value { | 2425 | pub fn intTrunc(val: Value, allocator: *Allocator, signedness: std.builtin.Signedness, bits: u16) !Value { |
| ... | @@ -2507,13 +2433,7 @@ pub const Value = extern union { | ... | @@ -2507,13 +2433,7 @@ pub const Value = extern union { |
| 2507 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; | 2433 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2508 | | 2434 | |
| 2509 | result_bigint.truncate(val_bigint, signedness, bits); | 2435 | result_bigint.truncate(val_bigint, signedness, bits); |
| 2510 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2436 | return fromBigInt(allocator, result_bigint.toConst()); |
| 2511 | | | |
| 2512 | if (result_bigint.positive) { | | |
| 2513 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2514 | } else { | | |
| 2515 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2516 | } | | |
| 2517 | } | 2437 | } |
| 2518 | | 2438 | |
| 2519 | pub fn shl(lhs: Value, rhs: Value, allocator: *Allocator) !Value { | 2439 | pub fn shl(lhs: Value, rhs: Value, allocator: *Allocator) !Value { |
| ... | @@ -2532,13 +2452,7 @@ pub const Value = extern union { | ... | @@ -2532,13 +2452,7 @@ pub const Value = extern union { |
| 2532 | .len = undefined, | 2452 | .len = undefined, |
| 2533 | }; | 2453 | }; |
| 2534 | result_bigint.shiftLeft(lhs_bigint, shift); | 2454 | result_bigint.shiftLeft(lhs_bigint, shift); |
| 2535 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2455 | return fromBigInt(allocator, result_bigint.toConst()); |
| 2536 | | | |
| 2537 | if (result_bigint.positive) { | | |
| 2538 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2539 | } else { | | |
| 2540 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2541 | } | | |
| 2542 | } | 2456 | } |
| 2543 | | 2457 | |
| 2544 | pub fn shlSat( | 2458 | pub fn shlSat( |
| ... | @@ -2565,13 +2479,7 @@ pub const Value = extern union { | ... | @@ -2565,13 +2479,7 @@ pub const Value = extern union { |
| 2565 | .len = undefined, | 2479 | .len = undefined, |
| 2566 | }; | 2480 | }; |
| 2567 | result_bigint.shiftLeftSat(lhs_bigint, shift, info.signedness, info.bits); | 2481 | result_bigint.shiftLeftSat(lhs_bigint, shift, info.signedness, info.bits); |
| 2568 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2482 | return fromBigInt(arena, result_bigint.toConst()); |
| 2569 | | | |
| 2570 | if (result_bigint.positive) { | | |
| 2571 | return Value.Tag.int_big_positive.create(arena, result_limbs); | | |
| 2572 | } else { | | |
| 2573 | return Value.Tag.int_big_negative.create(arena, result_limbs); | | |
| 2574 | } | | |
| 2575 | } | 2483 | } |
| 2576 | | 2484 | |
| 2577 | pub fn shr(lhs: Value, rhs: Value, allocator: *Allocator) !Value { | 2485 | pub fn shr(lhs: Value, rhs: Value, allocator: *Allocator) !Value { |
| ... | @@ -2590,13 +2498,7 @@ pub const Value = extern union { | ... | @@ -2590,13 +2498,7 @@ pub const Value = extern union { |
| 2590 | .len = undefined, | 2498 | .len = undefined, |
| 2591 | }; | 2499 | }; |
| 2592 | result_bigint.shiftRight(lhs_bigint, shift); | 2500 | result_bigint.shiftRight(lhs_bigint, shift); |
| 2593 | const result_limbs = result_bigint.limbs[0..result_bigint.len]; | 2501 | return fromBigInt(allocator, result_bigint.toConst()); |
| 2594 | | | |
| 2595 | if (result_bigint.positive) { | | |
| 2596 | return Value.Tag.int_big_positive.create(allocator, result_limbs); | | |
| 2597 | } else { | | |
| 2598 | return Value.Tag.int_big_negative.create(allocator, result_limbs); | | |
| 2599 | } | | |
| 2600 | } | 2502 | } |
| 2601 | | 2503 | |
| 2602 | pub fn floatAdd( | 2504 | pub fn floatAdd( |