Skip to content

Commit 0457315

Browse files
committed
fuzz: Move host floats to a separate module
1 parent 0c76d19 commit 0457315

2 files changed

Lines changed: 137 additions & 131 deletions

File tree

fuzz/src/host.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
use std::fmt;
2+
3+
use rustc_apfloat::{Round, Status, StatusAnd};
4+
5+
/// Abstraction over host float operations. If the requested rounding mode is not supported,
6+
/// return `None`.
7+
pub trait HostFloat: Copy + Sized + fmt::Debug {
8+
type UInt: Copy + fmt::LowerHex;
9+
fn from_bits(bits: Self::UInt) -> Self;
10+
fn to_bits(self) -> Self::UInt;
11+
fn neg(self) -> Self;
12+
fn add_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>>;
13+
fn sub_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>>;
14+
fn mul_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>>;
15+
fn div_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>>;
16+
fn rem(self, other: Self) -> Self;
17+
fn mul_add_r(self, mul: Self, add: Self, rm: Round) -> Option<StatusAnd<Self>>;
18+
fn to_i128_r(self, rm: Round) -> Option<StatusAnd<i128>>;
19+
fn from_i128_r(x: i128, rm: Round) -> Option<StatusAnd<Self>>;
20+
fn to_u128_r(self, rm: Round) -> Option<StatusAnd<u128>>;
21+
fn from_u128_r(x: u128, rm: Round) -> Option<StatusAnd<Self>>;
22+
fn to_double_r(self, rm: Round) -> Option<StatusAnd<f64>>;
23+
fn from_double_r(x: f64, rm: Round) -> Option<StatusAnd<Self>>;
24+
fn to_single_r(self, rm: Round) -> Option<StatusAnd<f32>>;
25+
fn from_single_r(x: f32, rm: Round) -> Option<StatusAnd<Self>>;
26+
}
27+
28+
macro_rules! impl_host_float {
29+
($ty:ty, $ity:ty) => {
30+
impl HostFloat for $ty {
31+
type UInt = $ity;
32+
fn from_bits(bits: Self::UInt) -> Self {
33+
Self::from_bits(bits)
34+
}
35+
fn to_bits(self) -> Self::UInt {
36+
self.to_bits()
37+
}
38+
fn neg(self) -> Self {
39+
-self
40+
}
41+
fn add_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>> {
42+
match rm {
43+
Round::NearestTiesToEven => Some(Status::OK.and(self + other)),
44+
_ => None,
45+
}
46+
}
47+
fn sub_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>> {
48+
match rm {
49+
Round::NearestTiesToEven => Some(Status::OK.and(self - other)),
50+
_ => None,
51+
}
52+
}
53+
fn mul_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>> {
54+
match rm {
55+
Round::NearestTiesToEven => Some(Status::OK.and(self * other)),
56+
_ => None,
57+
}
58+
}
59+
fn div_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>> {
60+
match rm {
61+
Round::NearestTiesToEven => Some(Status::OK.and(self / other)),
62+
_ => None,
63+
}
64+
}
65+
fn rem(self, other: Self) -> Self {
66+
self % other
67+
}
68+
fn mul_add_r(self, mul: Self, add: Self, rm: Round) -> Option<StatusAnd<Self>> {
69+
match rm {
70+
Round::NearestTiesToEven => Some(Status::OK.and(self.mul_add(mul, add))),
71+
_ => None,
72+
}
73+
}
74+
75+
/* float->int casts are toward zero */
76+
fn to_i128_r(self, rm: Round) -> Option<StatusAnd<i128>> {
77+
match rm {
78+
Round::TowardZero => Some(Status::OK.and(self as i128)),
79+
_ => None,
80+
}
81+
}
82+
fn to_u128_r(self, rm: Round) -> Option<StatusAnd<u128>> {
83+
match rm {
84+
Round::TowardZero => Some(Status::OK.and(self as u128)),
85+
_ => None,
86+
}
87+
}
88+
89+
fn from_i128_r(x: i128, rm: Round) -> Option<StatusAnd<Self>> {
90+
match rm {
91+
Round::NearestTiesToEven => Some(Status::OK.and(x as Self)),
92+
_ => None,
93+
}
94+
}
95+
fn from_u128_r(x: u128, rm: Round) -> Option<StatusAnd<Self>> {
96+
match rm {
97+
Round::NearestTiesToEven => Some(Status::OK.and(x as Self)),
98+
_ => None,
99+
}
100+
}
101+
fn to_double_r(self, rm: Round) -> Option<StatusAnd<f64>> {
102+
match rm {
103+
Round::NearestTiesToEven => Some(Status::OK.and(self as f64)),
104+
_ => None,
105+
}
106+
}
107+
fn from_double_r(x: f64, rm: Round) -> Option<StatusAnd<Self>> {
108+
match rm {
109+
Round::NearestTiesToEven => Some(Status::OK.and(x as Self)),
110+
_ => None,
111+
}
112+
}
113+
fn to_single_r(self, rm: Round) -> Option<StatusAnd<f32>> {
114+
match rm {
115+
Round::NearestTiesToEven => Some(Status::OK.and(self as f32)),
116+
_ => None,
117+
}
118+
}
119+
fn from_single_r(x: f32, rm: Round) -> Option<StatusAnd<Self>> {
120+
match rm {
121+
Round::NearestTiesToEven => Some(Status::OK.and(x as Self)),
122+
_ => None,
123+
}
124+
}
125+
}
126+
};
127+
}
128+
129+
#[cfg(target_has_reliable_f16)]
130+
impl_host_float!(f16, u16);
131+
impl_host_float!(f32, u32);
132+
impl_host_float!(f64, u64);
133+
#[cfg(target_has_reliable_f128)]
134+
impl_host_float!(f128, u128);

fuzz/src/main.rs

Lines changed: 3 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(cfg_target_has_reliable_f16_f128)]
55

66
mod exhaustive;
7+
mod host;
78

89
use io::IsTerminal;
910
use io::Read;
@@ -17,6 +18,8 @@ use num_traits::{FromPrimitive, ToPrimitive};
1718
use rustc_apfloat::ieee::{Double, Single};
1819
use rustc_apfloat::{Float, FloatConvert, Round, Status, StatusAnd, ieee};
1920

21+
use crate::host::HostFloat;
22+
2023
#[derive(Clone, Parser, Debug)]
2124
struct Args {
2225
/// Disable comparison with C++ (LLVM's original) APFloat
@@ -1034,137 +1037,6 @@ fn eval_host<F: HostFloat>(
10341037
Some(res.map(F::to_bits))
10351038
}
10361039

1037-
/// Abstraction over host float operations. If the requested rounding mode is not supported,
1038-
/// return `None`.
1039-
trait HostFloat: Copy + Sized + fmt::Debug {
1040-
type UInt: Copy + fmt::LowerHex;
1041-
fn from_bits(bits: Self::UInt) -> Self;
1042-
fn to_bits(self) -> Self::UInt;
1043-
fn neg(self) -> Self;
1044-
fn add_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>>;
1045-
fn sub_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>>;
1046-
fn mul_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>>;
1047-
fn div_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>>;
1048-
fn rem(self, other: Self) -> Self;
1049-
fn mul_add_r(self, mul: Self, add: Self, rm: Round) -> Option<StatusAnd<Self>>;
1050-
fn to_i128_r(self, rm: Round) -> Option<StatusAnd<i128>>;
1051-
fn from_i128_r(x: i128, rm: Round) -> Option<StatusAnd<Self>>;
1052-
fn to_u128_r(self, rm: Round) -> Option<StatusAnd<u128>>;
1053-
fn from_u128_r(x: u128, rm: Round) -> Option<StatusAnd<Self>>;
1054-
fn to_double_r(self, rm: Round) -> Option<StatusAnd<f64>>;
1055-
fn from_double_r(x: f64, rm: Round) -> Option<StatusAnd<Self>>;
1056-
fn to_single_r(self, rm: Round) -> Option<StatusAnd<f32>>;
1057-
fn from_single_r(x: f32, rm: Round) -> Option<StatusAnd<Self>>;
1058-
}
1059-
1060-
macro_rules! impl_host_float {
1061-
($ty:ty, $ity:ty) => {
1062-
impl HostFloat for $ty {
1063-
type UInt = $ity;
1064-
fn from_bits(bits: Self::UInt) -> Self {
1065-
Self::from_bits(bits)
1066-
}
1067-
fn to_bits(self) -> Self::UInt {
1068-
self.to_bits()
1069-
}
1070-
fn neg(self) -> Self {
1071-
-self
1072-
}
1073-
fn add_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>> {
1074-
match rm {
1075-
Round::NearestTiesToEven => Some(Status::OK.and(self + other)),
1076-
_ => None,
1077-
}
1078-
}
1079-
fn sub_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>> {
1080-
match rm {
1081-
Round::NearestTiesToEven => Some(Status::OK.and(self - other)),
1082-
_ => None,
1083-
}
1084-
}
1085-
fn mul_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>> {
1086-
match rm {
1087-
Round::NearestTiesToEven => Some(Status::OK.and(self * other)),
1088-
_ => None,
1089-
}
1090-
}
1091-
fn div_r(self, other: Self, rm: Round) -> Option<StatusAnd<Self>> {
1092-
match rm {
1093-
Round::NearestTiesToEven => Some(Status::OK.and(self / other)),
1094-
_ => None,
1095-
}
1096-
}
1097-
fn rem(self, other: Self) -> Self {
1098-
self % other
1099-
}
1100-
fn mul_add_r(self, mul: Self, add: Self, rm: Round) -> Option<StatusAnd<Self>> {
1101-
match rm {
1102-
Round::NearestTiesToEven => Some(Status::OK.and(self.mul_add(mul, add))),
1103-
_ => None,
1104-
}
1105-
}
1106-
1107-
/* float->int casts are toward zero */
1108-
fn to_i128_r(self, rm: Round) -> Option<StatusAnd<i128>> {
1109-
match rm {
1110-
Round::TowardZero => Some(Status::OK.and(self as i128)),
1111-
_ => None,
1112-
}
1113-
}
1114-
fn to_u128_r(self, rm: Round) -> Option<StatusAnd<u128>> {
1115-
match rm {
1116-
Round::TowardZero => Some(Status::OK.and(self as u128)),
1117-
_ => None,
1118-
}
1119-
}
1120-
1121-
fn from_i128_r(x: i128, rm: Round) -> Option<StatusAnd<Self>> {
1122-
match rm {
1123-
Round::NearestTiesToEven => Some(Status::OK.and(x as Self)),
1124-
_ => None,
1125-
}
1126-
}
1127-
fn from_u128_r(x: u128, rm: Round) -> Option<StatusAnd<Self>> {
1128-
match rm {
1129-
Round::NearestTiesToEven => Some(Status::OK.and(x as Self)),
1130-
_ => None,
1131-
}
1132-
}
1133-
fn to_double_r(self, rm: Round) -> Option<StatusAnd<f64>> {
1134-
match rm {
1135-
Round::NearestTiesToEven => Some(Status::OK.and(self as f64)),
1136-
_ => None,
1137-
}
1138-
}
1139-
fn from_double_r(x: f64, rm: Round) -> Option<StatusAnd<Self>> {
1140-
match rm {
1141-
Round::NearestTiesToEven => Some(Status::OK.and(x as Self)),
1142-
_ => None,
1143-
}
1144-
}
1145-
fn to_single_r(self, rm: Round) -> Option<StatusAnd<f32>> {
1146-
match rm {
1147-
Round::NearestTiesToEven => Some(Status::OK.and(self as f32)),
1148-
_ => None,
1149-
}
1150-
}
1151-
fn from_single_r(x: f32, rm: Round) -> Option<StatusAnd<Self>> {
1152-
match rm {
1153-
Round::NearestTiesToEven => Some(Status::OK.and(x as Self)),
1154-
_ => None,
1155-
}
1156-
}
1157-
}
1158-
};
1159-
}
1160-
1161-
#[cfg(target_has_reliable_f16)]
1162-
impl_host_float!(f16, u16);
1163-
impl_host_float!(f32, u32);
1164-
impl_host_float!(f64, u64);
1165-
#[cfg(target_has_reliable_f128)]
1166-
impl_host_float!(f128, u128);
1167-
11681040
struct FloatPrintHelper<F: FloatRepr>(F);
11691041
impl<F: FloatRepr> fmt::Debug for FloatPrintHelper<F> {
11701042
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

0 commit comments

Comments
 (0)