Skip to content

Commit 92f188a

Browse files
committed
feat(std): add modulo functions to int and float
1 parent 775ebaf commit 92f188a

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

vm/src/primitives.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,71 @@ 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(
175+
format!("attempted to calculate remainder of {} divided by 0", dividend)
176+
)
177+
}
178+
}
179+
180+
pub(crate) fn rem_euclid(dividend: VmInt, divisor: VmInt) -> RuntimeResult<VmInt, String> {
181+
if divisor != 0 {
182+
RuntimeResult::Return(dividend.rem_euclid(divisor))
183+
} else {
184+
RuntimeResult::Panic(
185+
format!("attempted to calculate euclidean remainder of {} divided by 0", dividend)
186+
)
187+
}
188+
}
189+
190+
pub(crate) fn wrapping_rem(dividend: VmInt, divisor: VmInt) -> RuntimeResult<VmInt, String> {
191+
if divisor != 0 {
192+
RuntimeResult::Return(dividend.wrapping_rem(divisor))
193+
} else {
194+
RuntimeResult::Panic(
195+
format!("attempted to calculate wrapping remainder of {} divided by 0", dividend)
196+
)
197+
}
198+
}
199+
200+
pub(crate) fn wrapping_rem_euclid(dividend: VmInt, divisor: VmInt) -> RuntimeResult<VmInt, String> {
201+
if divisor != 0 {
202+
RuntimeResult::Return(dividend.wrapping_rem_euclid(divisor))
203+
} else {
204+
RuntimeResult::Panic(
205+
format!("attempted to calculate wrapping euclidean remainder of {} divided by 0", dividend)
206+
)
207+
}
208+
}
209+
210+
pub(crate) fn overflowing_rem(dividend: VmInt, divisor: VmInt) -> RuntimeResult<(VmInt, bool), String> {
211+
if divisor != 0 {
212+
RuntimeResult::Return(dividend.overflowing_rem(divisor))
213+
} else {
214+
RuntimeResult::Panic(
215+
format!("attempted to calculate overflowing remainder of {} divided by 0", dividend)
216+
)
217+
}
218+
}
219+
220+
pub(crate) fn overflowing_rem_euclid(dividend: VmInt, divisor: VmInt) -> RuntimeResult<(VmInt, bool), String> {
221+
if divisor != 0 {
222+
RuntimeResult::Return(dividend.overflowing_rem_euclid(divisor))
223+
} else {
224+
RuntimeResult::Panic(
225+
format!("attempted to calculate overflowing euclidean remainder of {} divided by 0", dividend)
226+
)
227+
}
228+
}
229+
}
230+
166231
mod string {
167232
use super::*;
168233
use crate::value::ValueStr;
@@ -432,6 +497,8 @@ pub fn load_float(thread: &Thread) -> Result<ExternModule> {
432497
is_sign_negative => primitive!(1, std::float::prim::is_sign_negative),
433498
mul_add => primitive!(3, std::float::prim::mul_add),
434499
recip => primitive!(1, std::float::prim::recip),
500+
rem => primitive!(2, "std::float::prim::rem", |a: f64, b: f64| a % b),
501+
rem_euclid => primitive!(2, std::float::prim::rem_euclid),
435502
powi => primitive!(2, std::float::prim::powi),
436503
powf => primitive!(2, std::float::prim::powf),
437504
sqrt => primitive!(1, std::float::prim::sqrt),
@@ -538,6 +605,10 @@ pub fn load_int(vm: &Thread) -> Result<ExternModule> {
538605
to_le => primitive!(1, std::int::prim::to_le),
539606
pow => primitive!(2, std::int::prim::pow),
540607
abs => primitive!(1, std::int::prim::abs),
608+
rem => primitive!(2, "std::int::prim::rem", int::rem),
609+
rem_euclid => primitive!(2, "std::int::prim::rem_euclid", int::rem_euclid),
610+
checked_rem => primitive!(2, std::int::prim::checked_rem),
611+
checked_rem_euclid => primitive!(2, std::int::prim::checked_rem_euclid),
541612
saturating_add => primitive!(2, std::int::prim::saturating_add),
542613
saturating_sub => primitive!(2, std::int::prim::saturating_sub),
543614
saturating_mul => primitive!(2, std::int::prim::saturating_mul),
@@ -546,12 +617,16 @@ pub fn load_int(vm: &Thread) -> Result<ExternModule> {
546617
wrapping_mul => primitive!(2, std::int::prim::wrapping_mul),
547618
wrapping_div => primitive!(2, std::int::prim::wrapping_div),
548619
wrapping_abs => primitive!(1, std::int::prim::wrapping_abs),
620+
wrapping_rem => primitive!(2, "std::int::prim::wrapping_rem", int::wrapping_rem),
621+
wrapping_rem_euclid => primitive!(2, "std::int::prim::wrapping_rem", int::wrapping_rem_euclid),
549622
wrapping_negate => primitive!(1, "std.int.prim.wrapping_negate", std::int::prim::wrapping_neg),
550623
overflowing_add => primitive!(2, std::int::prim::overflowing_add),
551624
overflowing_sub => primitive!(2, std::int::prim::overflowing_sub),
552625
overflowing_mul => primitive!(2, std::int::prim::overflowing_mul),
553626
overflowing_div => primitive!(2, std::int::prim::overflowing_div),
554627
overflowing_abs => primitive!(1, std::int::prim::overflowing_abs),
628+
overflowing_rem => primitive!(2, "std::int::prim::overflowing_rem", int::overflowing_rem),
629+
overflowing_rem_euclid => primitive!(2, "std::int::prim::overflowing_rem_euclid", int::overflowing_rem_euclid),
555630
overflowing_negate => primitive!(1, "std.int.prim.overflowing_negate", std::int::prim::overflowing_neg),
556631
signum => primitive!(1, std::int::prim::signum),
557632
is_positive => primitive!(1, std::int::prim::is_positive),

0 commit comments

Comments
 (0)