Merge pull request #6460 from roc-lang/dec-div-floor

`floor`, `ceiling` and `round` for `Dec`
This commit is contained in:
Brendan Hansknecht 2024-01-30 16:38:12 -08:00 committed by GitHub
commit e7be9d435d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 315 additions and 69 deletions

View file

@ -1636,6 +1636,7 @@ impl<'a> LowLevelCall<'a> {
match arg_type {
F32 => self.load_args_and_call_zig(backend, &bitcode::NUM_ROUND_F32[width]),
F64 => self.load_args_and_call_zig(backend, &bitcode::NUM_ROUND_F64[width]),
Decimal => self.load_args_and_call_zig(backend, &bitcode::DEC_ROUND[width]),
_ => internal_error!("Invalid argument type for round: {:?}", arg_type),
}
}
@ -1643,6 +1644,14 @@ impl<'a> LowLevelCall<'a> {
self.load_args(backend);
let arg_type = CodeGenNumType::for_symbol(backend, self.arguments[0]);
let ret_type = CodeGenNumType::from(self.ret_layout);
let width = match ret_type {
CodeGenNumType::I32 => IntWidth::I32,
CodeGenNumType::I64 => IntWidth::I64,
CodeGenNumType::I128 => todo!("{:?} for I128", self.lowlevel),
_ => internal_error!("Invalid return type for round: {:?}", ret_type),
};
match (arg_type, self.lowlevel) {
(F32, NumCeiling) => {
backend.code_builder.f32_ceil();
@ -1650,14 +1659,21 @@ impl<'a> LowLevelCall<'a> {
(F64, NumCeiling) => {
backend.code_builder.f64_ceil();
}
(Decimal, NumCeiling) => {
return self.load_args_and_call_zig(backend, &bitcode::DEC_CEILING[width]);
}
(F32, NumFloor) => {
backend.code_builder.f32_floor();
}
(F64, NumFloor) => {
backend.code_builder.f64_floor();
}
(Decimal, NumFloor) => {
return self.load_args_and_call_zig(backend, &bitcode::DEC_FLOOR[width]);
}
_ => internal_error!("Invalid argument type for ceiling: {:?}", arg_type),
}
match (ret_type, arg_type) {
// TODO: unsigned truncation
(I32, F32) => backend.code_builder.i32_trunc_s_f32(),