mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
stage2: implement comptime division
This commit is contained in:
parent
69645e2817
commit
df983b30d2
@ -4585,6 +4585,37 @@ pub fn intSub(allocator: *Allocator, lhs: Value, rhs: Value) !Value {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn intDiv(allocator: *Allocator, lhs: Value, rhs: Value) !Value {
|
||||
// TODO is this a performance issue? maybe we should try the operation without
|
||||
// resorting to BigInt first.
|
||||
var lhs_space: Value.BigIntSpace = undefined;
|
||||
var rhs_space: Value.BigIntSpace = undefined;
|
||||
const lhs_bigint = lhs.toBigInt(&lhs_space);
|
||||
const rhs_bigint = rhs.toBigInt(&rhs_space);
|
||||
const limbs_q = try allocator.alloc(
|
||||
std.math.big.Limb,
|
||||
lhs_bigint.limbs.len + rhs_bigint.limbs.len + 1,
|
||||
);
|
||||
const limbs_r = try allocator.alloc(
|
||||
std.math.big.Limb,
|
||||
lhs_bigint.limbs.len,
|
||||
);
|
||||
const limbs_buffer = try allocator.alloc(
|
||||
std.math.big.Limb,
|
||||
std.math.big.int.calcDivLimbsBufferLen(lhs_bigint.limbs.len, rhs_bigint.limbs.len),
|
||||
);
|
||||
var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined };
|
||||
var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined };
|
||||
result_q.divTrunc(&result_r, lhs_bigint, rhs_bigint, limbs_buffer, null);
|
||||
const result_limbs = result_q.limbs[0..result_q.len];
|
||||
|
||||
if (result_q.positive) {
|
||||
return Value.Tag.int_big_positive.create(allocator, result_limbs);
|
||||
} else {
|
||||
return Value.Tag.int_big_negative.create(allocator, result_limbs);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn floatAdd(
|
||||
arena: *Allocator,
|
||||
float_type: Type,
|
||||
@ -4651,6 +4682,39 @@ pub fn floatSub(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn floatDiv(
|
||||
arena: *Allocator,
|
||||
float_type: Type,
|
||||
src: LazySrcLoc,
|
||||
lhs: Value,
|
||||
rhs: Value,
|
||||
) !Value {
|
||||
switch (float_type.tag()) {
|
||||
.f16 => {
|
||||
@panic("TODO add __trunctfhf2 to compiler-rt");
|
||||
//const lhs_val = lhs.toFloat(f16);
|
||||
//const rhs_val = rhs.toFloat(f16);
|
||||
//return Value.Tag.float_16.create(arena, lhs_val / rhs_val);
|
||||
},
|
||||
.f32 => {
|
||||
const lhs_val = lhs.toFloat(f32);
|
||||
const rhs_val = rhs.toFloat(f32);
|
||||
return Value.Tag.float_32.create(arena, lhs_val / rhs_val);
|
||||
},
|
||||
.f64 => {
|
||||
const lhs_val = lhs.toFloat(f64);
|
||||
const rhs_val = rhs.toFloat(f64);
|
||||
return Value.Tag.float_64.create(arena, lhs_val / rhs_val);
|
||||
},
|
||||
.f128, .comptime_float, .c_longdouble => {
|
||||
const lhs_val = lhs.toFloat(f128);
|
||||
const rhs_val = rhs.toFloat(f128);
|
||||
return Value.Tag.float_128.create(arena, lhs_val / rhs_val);
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn simplePtrType(
|
||||
mod: *Module,
|
||||
arena: *Allocator,
|
||||
|
||||
@ -4139,6 +4139,13 @@ fn analyzeArithmetic(
|
||||
try Module.floatSub(sema.arena, scalar_type, src, lhs_val, rhs_val);
|
||||
break :blk val;
|
||||
},
|
||||
.div => blk: {
|
||||
const val = if (is_int)
|
||||
try Module.intDiv(sema.arena, lhs_val, rhs_val)
|
||||
else
|
||||
try Module.floatDiv(sema.arena, scalar_type, src, lhs_val, rhs_val);
|
||||
break :blk val;
|
||||
},
|
||||
else => return sema.mod.fail(&block.base, src, "TODO Implement arithmetic operand '{s}'", .{@tagName(zir_tag)}),
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user