Skip to content

Commit 219201e

Browse files
bors[bot]Etherian
andauthored
Merge #868
868: feat(std): Add 'remainder' functions for Int and Float r=Marwes a=Etherian For `Int` adds - `rem` - `checked_rem` - `wrapping_rem` - `overflowing_rem` - `rem_euclid` - `checked_rem_euclid` - `wrapping_rem_euclid` - `overflowing_rem_euclid` For `Float` adds - `rem` - `rem_euclid` Co-authored-by: Etherian <[email protected]>
2 parents cc93674 + 8f1d742 commit 219201e

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

vm/src/primitives.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,86 @@ pub mod array {
163163
}
164164
}
165165

166+
mod int {
167+
use super::*;
168+
use crate::types::VmInt;
169+
170+
pub(crate) fn rem(dividend: VmInt, divisor: VmInt) -> RuntimeResult<VmInt, String> {
171+
if divisor != 0 {
172+
RuntimeResult::Return(dividend % divisor)
173+
} else {
174+
RuntimeResult::Panic(format!(
175+
"attempted to calculate remainder of {} divided by 0",
176+
dividend
177+
))
178+
}
179+
}
180+
181+
pub(crate) fn rem_euclid(dividend: VmInt, divisor: VmInt) -> RuntimeResult<VmInt, String> {
182+
if divisor != 0 {
183+
RuntimeResult::Return(dividend.rem_euclid(divisor))
184+
} else {
185+
RuntimeResult::Panic(format!(
186+
"attempted to calculate euclidean remainder of {} divided by 0",
187+
dividend
188+
))
189+
}
190+
}
191+
192+
pub(crate) fn wrapping_rem(dividend: VmInt, divisor: VmInt) -> RuntimeResult<VmInt, String> {
193+
if divisor != 0 {
194+
RuntimeResult::Return(dividend.wrapping_rem(divisor))
195+
} else {
196+
RuntimeResult::Panic(format!(
197+
"attempted to calculate wrapping remainder of {} divided by 0",
198+
dividend
199+
))
200+
}
201+
}
202+
203+
pub(crate) fn wrapping_rem_euclid(
204+
dividend: VmInt,
205+
divisor: VmInt,
206+
) -> RuntimeResult<VmInt, String> {
207+
if divisor != 0 {
208+
RuntimeResult::Return(dividend.wrapping_rem_euclid(divisor))
209+
} else {
210+
RuntimeResult::Panic(format!(
211+
"attempted to calculate wrapping euclidean remainder of {} divided by 0",
212+
dividend
213+
))
214+
}
215+
}
216+
217+
pub(crate) fn overflowing_rem(
218+
dividend: VmInt,
219+
divisor: VmInt,
220+
) -> RuntimeResult<(VmInt, bool), String> {
221+
if divisor != 0 {
222+
RuntimeResult::Return(dividend.overflowing_rem(divisor))
223+
} else {
224+
RuntimeResult::Panic(format!(
225+
"attempted to calculate overflowing remainder of {} divided by 0",
226+
dividend
227+
))
228+
}
229+
}
230+
231+
pub(crate) fn overflowing_rem_euclid(
232+
dividend: VmInt,
233+
divisor: VmInt,
234+
) -> RuntimeResult<(VmInt, bool), String> {
235+
if divisor != 0 {
236+
RuntimeResult::Return(dividend.overflowing_rem_euclid(divisor))
237+
} else {
238+
RuntimeResult::Panic(format!(
239+
"attempted to calculate overflowing euclidean remainder of {} divided by 0",
240+
dividend
241+
))
242+
}
243+
}
244+
}
245+
166246
mod string {
167247
use super::*;
168248
use crate::value::ValueStr;
@@ -432,6 +512,8 @@ pub fn load_float(thread: &Thread) -> Result<ExternModule> {
432512
is_sign_negative => primitive!(1, std::float::prim::is_sign_negative),
433513
mul_add => primitive!(3, std::float::prim::mul_add),
434514
recip => primitive!(1, std::float::prim::recip),
515+
rem => primitive!(2, "std::float::prim::rem", |a: f64, b: f64| a % b),
516+
rem_euclid => primitive!(2, std::float::prim::rem_euclid),
435517
powi => primitive!(2, std::float::prim::powi),
436518
powf => primitive!(2, std::float::prim::powf),
437519
sqrt => primitive!(1, std::float::prim::sqrt),
@@ -538,6 +620,10 @@ pub fn load_int(vm: &Thread) -> Result<ExternModule> {
538620
to_le => primitive!(1, std::int::prim::to_le),
539621
pow => primitive!(2, std::int::prim::pow),
540622
abs => primitive!(1, std::int::prim::abs),
623+
rem => primitive!(2, "std::int::prim::rem", int::rem),
624+
rem_euclid => primitive!(2, "std::int::prim::rem_euclid", int::rem_euclid),
625+
checked_rem => primitive!(2, std::int::prim::checked_rem),
626+
checked_rem_euclid => primitive!(2, std::int::prim::checked_rem_euclid),
541627
saturating_add => primitive!(2, std::int::prim::saturating_add),
542628
saturating_sub => primitive!(2, std::int::prim::saturating_sub),
543629
saturating_mul => primitive!(2, std::int::prim::saturating_mul),
@@ -546,12 +632,16 @@ pub fn load_int(vm: &Thread) -> Result<ExternModule> {
546632
wrapping_mul => primitive!(2, std::int::prim::wrapping_mul),
547633
wrapping_div => primitive!(2, std::int::prim::wrapping_div),
548634
wrapping_abs => primitive!(1, std::int::prim::wrapping_abs),
635+
wrapping_rem => primitive!(2, "std::int::prim::wrapping_rem", int::wrapping_rem),
636+
wrapping_rem_euclid => primitive!(2, "std::int::prim::wrapping_rem", int::wrapping_rem_euclid),
549637
wrapping_negate => primitive!(1, "std.int.prim.wrapping_negate", std::int::prim::wrapping_neg),
550638
overflowing_add => primitive!(2, std::int::prim::overflowing_add),
551639
overflowing_sub => primitive!(2, std::int::prim::overflowing_sub),
552640
overflowing_mul => primitive!(2, std::int::prim::overflowing_mul),
553641
overflowing_div => primitive!(2, std::int::prim::overflowing_div),
554642
overflowing_abs => primitive!(1, std::int::prim::overflowing_abs),
643+
overflowing_rem => primitive!(2, "std::int::prim::overflowing_rem", int::overflowing_rem),
644+
overflowing_rem_euclid => primitive!(2, "std::int::prim::overflowing_rem_euclid", int::overflowing_rem_euclid),
555645
overflowing_negate => primitive!(1, "std.int.prim.overflowing_negate", std::int::prim::overflowing_neg),
556646
signum => primitive!(1, std::int::prim::signum),
557647
is_positive => primitive!(1, std::int::prim::is_positive),

0 commit comments

Comments
 (0)