|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use {Category, ExpInt}; |
| 12 | +use {Float, FloatConvert, ParseError, Round, StatusAnd}; |
| 13 | + |
| 14 | +use std::cmp::Ordering; |
| 15 | +use std::fmt; |
| 16 | +use std::marker::PhantomData; |
| 17 | +use std::ops::Neg; |
| 18 | + |
| 19 | +#[must_use] |
| 20 | +pub struct IeeeFloat<S> { |
| 21 | + marker: PhantomData<S>, |
| 22 | +} |
| 23 | + |
| 24 | +/// Represents floating point arithmetic semantics. |
| 25 | +pub trait Semantics: Sized { |
| 26 | + /// Total number of bits in the in-memory format. |
| 27 | + const BITS: usize; |
| 28 | + |
| 29 | + /// Number of bits in the significand. This includes the integer bit. |
| 30 | + const PRECISION: usize; |
| 31 | + |
| 32 | + /// The largest E such that 2^E is representable; this matches the |
| 33 | + /// definition of IEEE 754. |
| 34 | + const MAX_EXP: ExpInt; |
| 35 | + |
| 36 | + /// The smallest E such that 2^E is a normalized number; this |
| 37 | + /// matches the definition of IEEE 754. |
| 38 | + const MIN_EXP: ExpInt = -Self::MAX_EXP + 1; |
| 39 | +} |
| 40 | + |
| 41 | +impl<S> Copy for IeeeFloat<S> {} |
| 42 | +impl<S> Clone for IeeeFloat<S> { |
| 43 | + fn clone(&self) -> Self { |
| 44 | + *self |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +macro_rules! ieee_semantics { |
| 49 | + ($($name:ident = $sem:ident($bits:tt : $exp_bits:tt)),*) => { |
| 50 | + $(pub struct $sem;)* |
| 51 | + $(pub type $name = IeeeFloat<$sem>;)* |
| 52 | + $(impl Semantics for $sem { |
| 53 | + const BITS: usize = $bits; |
| 54 | + const PRECISION: usize = ($bits - 1 - $exp_bits) + 1; |
| 55 | + const MAX_EXP: ExpInt = (1 << ($exp_bits - 1)) - 1; |
| 56 | + })* |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +ieee_semantics! { |
| 61 | + Half = HalfS(16:5), |
| 62 | + Single = SingleS(32:8), |
| 63 | + Double = DoubleS(64:11), |
| 64 | + Quad = QuadS(128:15) |
| 65 | +} |
| 66 | + |
| 67 | +pub struct X87DoubleExtendedS; |
| 68 | +pub type X87DoubleExtended = IeeeFloat<X87DoubleExtendedS>; |
| 69 | +impl Semantics for X87DoubleExtendedS { |
| 70 | + const BITS: usize = 80; |
| 71 | + const PRECISION: usize = 64; |
| 72 | + const MAX_EXP: ExpInt = (1 << (15 - 1)) - 1; |
| 73 | +} |
| 74 | + |
| 75 | +float_common_impls!(IeeeFloat<S>); |
| 76 | + |
| 77 | +impl<S: Semantics> PartialEq for IeeeFloat<S> { |
| 78 | + fn eq(&self, rhs: &Self) -> bool { |
| 79 | + self.partial_cmp(rhs) == Some(Ordering::Equal) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[allow(unused)] |
| 84 | +impl<S: Semantics> PartialOrd for IeeeFloat<S> { |
| 85 | + fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> { |
| 86 | + panic!("NYI PartialOrd::partial_cmp"); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<S> Neg for IeeeFloat<S> { |
| 91 | + type Output = Self; |
| 92 | + fn neg(self) -> Self { |
| 93 | + panic!("NYI Neg::neg"); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Prints this value as a decimal string. |
| 98 | +/// |
| 99 | +/// \param precision The maximum number of digits of |
| 100 | +/// precision to output. If there are fewer digits available, |
| 101 | +/// zero padding will not be used unless the value is |
| 102 | +/// integral and small enough to be expressed in |
| 103 | +/// precision digits. 0 means to use the natural |
| 104 | +/// precision of the number. |
| 105 | +/// \param width The maximum number of zeros to |
| 106 | +/// consider inserting before falling back to scientific |
| 107 | +/// notation. 0 means to always use scientific notation. |
| 108 | +/// |
| 109 | +/// \param alternate Indicate whether to remove the trailing zero in |
| 110 | +/// fraction part or not. Also setting this parameter to true forces |
| 111 | +/// producing of output more similar to default printf behavior. |
| 112 | +/// Specifically the lower e is used as exponent delimiter and exponent |
| 113 | +/// always contains no less than two digits. |
| 114 | +/// |
| 115 | +/// Number precision width Result |
| 116 | +/// ------ --------- ----- ------ |
| 117 | +/// 1.01E+4 5 2 10100 |
| 118 | +/// 1.01E+4 4 2 1.01E+4 |
| 119 | +/// 1.01E+4 5 1 1.01E+4 |
| 120 | +/// 1.01E-2 5 2 0.0101 |
| 121 | +/// 1.01E-2 4 2 0.0101 |
| 122 | +/// 1.01E-2 4 1 1.01E-2 |
| 123 | +#[allow(unused)] |
| 124 | +impl<S: Semantics> fmt::Display for IeeeFloat<S> { |
| 125 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 126 | + let frac_digits = f.precision().unwrap_or(0); |
| 127 | + let width = f.width().unwrap_or(3); |
| 128 | + let alternate = f.alternate(); |
| 129 | + panic!("NYI Display::fmt"); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl<S: Semantics> fmt::Debug for IeeeFloat<S> { |
| 134 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 135 | + write!(f, "{}", self) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +#[allow(unused)] |
| 140 | +impl<S: Semantics> Float for IeeeFloat<S> { |
| 141 | + const BITS: usize = S::BITS; |
| 142 | + const PRECISION: usize = S::PRECISION; |
| 143 | + const MAX_EXP: ExpInt = S::MAX_EXP; |
| 144 | + const MIN_EXP: ExpInt = S::MIN_EXP; |
| 145 | + |
| 146 | + const ZERO: Self = IeeeFloat { marker: PhantomData }; |
| 147 | + |
| 148 | + const INFINITY: Self = IeeeFloat { marker: PhantomData }; |
| 149 | + |
| 150 | + // FIXME(eddyb) remove when qnan becomes const fn. |
| 151 | + const NAN: Self = IeeeFloat { marker: PhantomData }; |
| 152 | + |
| 153 | + fn qnan(payload: Option<u128>) -> Self { |
| 154 | + panic!("NYI qnan") |
| 155 | + } |
| 156 | + |
| 157 | + fn snan(payload: Option<u128>) -> Self { |
| 158 | + panic!("NYI snan") |
| 159 | + } |
| 160 | + |
| 161 | + fn largest() -> Self { |
| 162 | + panic!("NYI largest") |
| 163 | + } |
| 164 | + |
| 165 | + const SMALLEST: Self = IeeeFloat { marker: PhantomData }; |
| 166 | + |
| 167 | + fn smallest_normalized() -> Self { |
| 168 | + panic!("NYI smallest_normalized") |
| 169 | + } |
| 170 | + |
| 171 | + fn add_r(self, rhs: Self, round: Round) -> StatusAnd<Self> { |
| 172 | + panic!("NYI add_r") |
| 173 | + } |
| 174 | + |
| 175 | + fn mul_r(self, rhs: Self, round: Round) -> StatusAnd<Self> { |
| 176 | + panic!("NYI mul_r") |
| 177 | + } |
| 178 | + |
| 179 | + fn mul_add_r(self, multiplicand: Self, addend: Self, round: Round) -> StatusAnd<Self> { |
| 180 | + panic!("NYI mul_add_r") |
| 181 | + } |
| 182 | + |
| 183 | + fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self> { |
| 184 | + panic!("NYI div_r") |
| 185 | + } |
| 186 | + |
| 187 | + fn c_fmod(self, rhs: Self) -> StatusAnd<Self> { |
| 188 | + panic!("NYI c_fmod") |
| 189 | + } |
| 190 | + |
| 191 | + fn round_to_integral(self, round: Round) -> StatusAnd<Self> { |
| 192 | + panic!("NYI round_to_integral") |
| 193 | + } |
| 194 | + |
| 195 | + fn next_up(self) -> StatusAnd<Self> { |
| 196 | + panic!("NYI next_up") |
| 197 | + } |
| 198 | + |
| 199 | + fn from_bits(input: u128) -> Self { |
| 200 | + panic!("NYI from_bits") |
| 201 | + } |
| 202 | + |
| 203 | + fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self> { |
| 204 | + panic!("NYI from_u128_r") |
| 205 | + } |
| 206 | + |
| 207 | + fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError> { |
| 208 | + panic!("NYI from_str_r") |
| 209 | + } |
| 210 | + |
| 211 | + fn to_bits(self) -> u128 { |
| 212 | + panic!("NYI to_bits") |
| 213 | + } |
| 214 | + |
| 215 | + fn to_u128_r(self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<u128> { |
| 216 | + panic!("NYI to_u128_r"); |
| 217 | + } |
| 218 | + |
| 219 | + fn cmp_abs_normal(self, rhs: Self) -> Ordering { |
| 220 | + panic!("NYI cmp_abs_normal") |
| 221 | + } |
| 222 | + |
| 223 | + fn bitwise_eq(self, rhs: Self) -> bool { |
| 224 | + panic!("NYI bitwise_eq") |
| 225 | + } |
| 226 | + |
| 227 | + fn is_negative(self) -> bool { |
| 228 | + panic!("NYI is_negative") |
| 229 | + } |
| 230 | + |
| 231 | + fn is_denormal(self) -> bool { |
| 232 | + panic!("NYI is_denormal") |
| 233 | + } |
| 234 | + |
| 235 | + fn is_signaling(self) -> bool { |
| 236 | + panic!("NYI is_signaling") |
| 237 | + } |
| 238 | + |
| 239 | + fn category(self) -> Category { |
| 240 | + panic!("NYI category") |
| 241 | + } |
| 242 | + |
| 243 | + fn get_exact_inverse(self) -> Option<Self> { |
| 244 | + panic!("NYI get_exact_inverse") |
| 245 | + } |
| 246 | + |
| 247 | + fn ilogb(self) -> ExpInt { |
| 248 | + panic!("NYI ilogb") |
| 249 | + } |
| 250 | + |
| 251 | + fn scalbn_r(self, exp: ExpInt, round: Round) -> Self { |
| 252 | + panic!("NYI scalbn") |
| 253 | + } |
| 254 | + |
| 255 | + fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self { |
| 256 | + panic!("NYI frexp") |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +#[allow(unused)] |
| 261 | +impl<S: Semantics, T: Semantics> FloatConvert<IeeeFloat<T>> for IeeeFloat<S> { |
| 262 | + fn convert_r(self, round: Round, loses_info: &mut bool) -> StatusAnd<IeeeFloat<T>> { |
| 263 | + panic!("NYI convert_r"); |
| 264 | + } |
| 265 | +} |
0 commit comments