Skip to content

Commit 50671dc

Browse files
committed
auto merge of #13410 : alexcrichton/rust/issue-12278, r=pcwalton
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
2 parents 4d49693 + 1563ea9 commit 50671dc

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
@@ -4091,14 +4091,15 @@ pub fn is_binopable(cx: &ctxt, ty: t, op: ast::BinOp) -> bool {
40914091
static opcat_eq: int = 5;
40924092
static opcat_bit: int = 6;
40934093
static opcat_logic: int = 7;
4094+
static opcat_mod: int = 8;
40944095

40954096
fn opcat(op: ast::BinOp) -> int {
40964097
match op {
40974098
ast::BiAdd => opcat_add,
40984099
ast::BiSub => opcat_sub,
40994100
ast::BiMul => opcat_mult,
41004101
ast::BiDiv => opcat_mult,
4101-
ast::BiRem => opcat_mult,
4102+
ast::BiRem => opcat_mod,
41024103
ast::BiAnd => opcat_logic,
41034104
ast::BiOr => opcat_logic,
41044105
ast::BiBitXor => opcat_bit,
@@ -4134,14 +4135,14 @@ pub fn is_binopable(cx: &ctxt, ty: t, op: ast::BinOp) -> bool {
41344135
static f: bool = false;
41354136

41364137
let tbl = [
4137-
// +, -, *, shift, rel, ==, bit, logic
4138-
/*other*/ [f, f, f, f, f, f, f, f],
4139-
/*bool*/ [f, f, f, f, t, t, t, t],
4140-
/*char*/ [f, f, f, f, t, t, f, f],
4141-
/*int*/ [t, t, t, t, t, t, t, f],
4142-
/*float*/ [t, t, t, f, t, t, f, f],
4143-
/*bot*/ [t, t, t, t, t, t, t, t],
4144-
/*raw ptr*/ [f, f, f, f, t, t, f, f]];
4138+
// +, -, *, shift, rel, ==, bit, logic, mod
4139+
/*other*/ [f, f, f, f, f, f, f, f, f],
4140+
/*bool*/ [f, f, f, f, t, t, t, t, f],
4141+
/*char*/ [f, f, f, f, t, t, f, f, f],
4142+
/*int*/ [t, t, t, t, t, t, t, f, t],
4143+
/*float*/ [t, t, t, f, t, t, f, f, f],
4144+
/*bot*/ [t, t, t, t, t, t, t, t, t],
4145+
/*raw ptr*/ [f, f, f, f, t, t, f, f, f]];
41454146

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

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)