Skip to content

Commit 1563ea9

Browse files
committed
rustc: Remove f{32,64} % from the language
This commit removes the compiler support for floating point modulus operations, as well as from the language. An implementation for this operator is now required to be provided by libraries. Floating point modulus is rarely used, doesn't exist in C, and is always lowered to an fmod library call by LLVM, and LLVM is considering removing support entirely. Closes #12278
1 parent 02f5121 commit 1563ea9

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/librustc/middle/ty.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -4125,14 +4125,15 @@ pub fn is_binopable(cx: &ctxt, ty: t, op: ast::BinOp) -> bool {
41254125
static opcat_eq: int = 5;
41264126
static opcat_bit: int = 6;
41274127
static opcat_logic: int = 7;
4128+
static opcat_mod: int = 8;
41284129

41294130
fn opcat(op: ast::BinOp) -> int {
41304131
match op {
41314132
ast::BiAdd => opcat_add,
41324133
ast::BiSub => opcat_sub,
41334134
ast::BiMul => opcat_mult,
41344135
ast::BiDiv => opcat_mult,
4135-
ast::BiRem => opcat_mult,
4136+
ast::BiRem => opcat_mod,
41364137
ast::BiAnd => opcat_logic,
41374138
ast::BiOr => opcat_logic,
41384139
ast::BiBitXor => opcat_bit,
@@ -4168,14 +4169,14 @@ pub fn is_binopable(cx: &ctxt, ty: t, op: ast::BinOp) -> bool {
41684169
static f: bool = false;
41694170

41704171
let tbl = [
4171-
// +, -, *, shift, rel, ==, bit, logic
4172-
/*other*/ [f, f, f, f, f, f, f, f],
4173-
/*bool*/ [f, f, f, f, t, t, t, t],
4174-
/*char*/ [f, f, f, f, t, t, f, f],
4175-
/*int*/ [t, t, t, t, t, t, t, f],
4176-
/*float*/ [t, t, t, f, t, t, f, f],
4177-
/*bot*/ [t, t, t, t, t, t, t, t],
4178-
/*raw ptr*/ [f, f, f, f, t, t, f, f]];
4172+
// +, -, *, shift, rel, ==, bit, logic, mod
4173+
/*other*/ [f, f, f, f, f, f, f, f, f],
4174+
/*bool*/ [f, f, f, f, t, t, t, t, f],
4175+
/*char*/ [f, f, f, f, t, t, f, f, f],
4176+
/*int*/ [t, t, t, t, t, t, t, f, t],
4177+
/*float*/ [t, t, t, f, t, t, f, f, f],
4178+
/*bot*/ [t, t, t, t, t, t, t, t, t],
4179+
/*raw ptr*/ [f, f, f, f, t, t, f, f, f]];
41794180

41804181
return tbl[tycat(cx, ty) as uint ][opcat(op) as uint];
41814182
}

src/libstd/num/f32.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod cmath {
4141
pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
4242
pub fn fmaxf(a: c_float, b: c_float) -> c_float;
4343
pub fn fminf(a: c_float, b: c_float) -> c_float;
44+
pub fn fmodf(a: c_float, b: c_float) -> c_float;
4445
pub fn nextafterf(x: c_float, y: c_float) -> c_float;
4546
pub fn hypotf(x: c_float, y: c_float) -> c_float;
4647
pub fn ldexpf(x: c_float, n: c_int) -> c_float;
@@ -201,7 +202,9 @@ impl Div<f32,f32> for f32 {
201202
#[cfg(not(test))]
202203
impl Rem<f32,f32> for f32 {
203204
#[inline]
204-
fn rem(&self, other: &f32) -> f32 { *self % *other }
205+
fn rem(&self, other: &f32) -> f32 {
206+
unsafe { cmath::fmodf(*self, *other) }
207+
}
205208
}
206209

207210
#[cfg(not(test))]

src/libstd/num/f64.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mod cmath {
4040
pub fn fdim(a: c_double, b: c_double) -> c_double;
4141
pub fn fmax(a: c_double, b: c_double) -> c_double;
4242
pub fn fmin(a: c_double, b: c_double) -> c_double;
43+
pub fn fmod(a: c_double, b: c_double) -> c_double;
4344
pub fn nextafter(x: c_double, y: c_double) -> c_double;
4445
pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
4546
pub fn hypot(x: c_double, y: c_double) -> c_double;
@@ -210,7 +211,9 @@ impl Div<f64,f64> for f64 {
210211
#[cfg(not(test))]
211212
impl Rem<f64,f64> for f64 {
212213
#[inline]
213-
fn rem(&self, other: &f64) -> f64 { *self % *other }
214+
fn rem(&self, other: &f64) -> f64 {
215+
unsafe { cmath::fmod(*self, *other) }
216+
}
214217
}
215218
#[cfg(not(test))]
216219
impl Neg<f64> for f64 {

0 commit comments

Comments
 (0)