Skip to content

Commit 152c76e

Browse files
committed
Minimize the implementation of Rem in libcore
The implementation of the remainder operation belongs to librustc_trans, but it is also stubbed out in libcore in order to expose it as a trait on primitive types. Instead of exposing some implementation details (like the upcast to `f64` in MSVC), use a minimal implementation just like that of the `Div` trait.
1 parent db67cbe commit 152c76e

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

src/libcore/ops.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ pub trait Rem<RHS=Self> {
423423
fn rem(self, rhs: RHS) -> Self::Output;
424424
}
425425

426-
macro_rules! rem_impl {
426+
macro_rules! rem_impl_integer {
427427
($($t:ty)*) => ($(
428428
/// This operation satisfies `n % d == n - (n / d) * d`. The
429429
/// result has the same sign as the left operand.
@@ -439,42 +439,23 @@ macro_rules! rem_impl {
439439
)*)
440440
}
441441

442-
rem_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
442+
rem_impl_integer! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
443443

444-
#[stable(feature = "rust1", since = "1.0.0")]
445-
impl Rem for f32 {
446-
type Output = f32;
447-
448-
// The builtin f32 rem operator is broken when targeting
449-
// MSVC; see comment in std::f32::floor.
450-
// FIXME: See also #27859.
451-
#[inline]
452-
#[cfg(target_env = "msvc")]
453-
fn rem(self, other: f32) -> f32 {
454-
(self as f64).rem(other as f64) as f32
455-
}
456-
457-
#[inline]
458-
#[cfg(not(target_env = "msvc"))]
459-
fn rem(self, other: f32) -> f32 {
460-
extern { fn fmodf(a: f32, b: f32) -> f32; }
461-
unsafe { fmodf(self, other) }
462-
}
463-
}
444+
macro_rules! rem_impl_float {
445+
($($t:ty)*) => ($(
446+
#[stable(feature = "rust1", since = "1.0.0")]
447+
impl Rem for $t {
448+
type Output = $t;
464449

465-
#[stable(feature = "rust1", since = "1.0.0")]
466-
impl Rem for f64 {
467-
type Output = f64;
450+
#[inline]
451+
fn rem(self, other: $t) -> $t { self % other }
452+
}
468453

469-
#[inline]
470-
fn rem(self, other: f64) -> f64 {
471-
extern { fn fmod(a: f64, b: f64) -> f64; }
472-
unsafe { fmod(self, other) }
473-
}
454+
forward_ref_binop! { impl Rem, rem for $t, $t }
455+
)*)
474456
}
475457

476-
forward_ref_binop! { impl Rem, rem for f64, f64 }
477-
forward_ref_binop! { impl Rem, rem for f32, f32 }
458+
rem_impl_float! { f32 f64 }
478459

479460
/// The `Neg` trait is used to specify the functionality of unary `-`.
480461
///

0 commit comments

Comments
 (0)