-/// Note that equality for `ConstFloat` means that the it is the same
-/// constant, not that the rust values are equal. In particular, `NaN
-/// == NaN` (at least if it's the same NaN; distinct encodings for NaN
-/// are considering unequal).
-impl PartialEq for ConstFloat {
- fn eq(&self, other: &Self) -> bool {
- match (*self, *other) {
- (F64(a), F64(b)) => {
- unsafe{transmute::<_,u64>(a) == transmute::<_,u64>(b)}
+ pub fn from_i128(input: i128, ty: ast::FloatTy) -> Self {
+ let bits = match ty {
+ ast::FloatTy::F32 => Single::from_i128(input).value.to_bits(),
+ ast::FloatTy::F64 => Double::from_i128(input).value.to_bits()
+ };
+ ConstFloat { bits, ty }
+ }
+
+ pub fn from_u128(input: u128, ty: ast::FloatTy) -> Self {
+ let bits = match ty {
+ ast::FloatTy::F32 => Single::from_u128(input).value.to_bits(),
+ ast::FloatTy::F64 => Double::from_u128(input).value.to_bits()
+ };
+ ConstFloat { bits, ty }
+ }
+
+ pub fn from_str(num: &str, ty: ast::FloatTy) -> Result<Self, ParseFloatError> {
+ let bits = match ty {
+ ast::FloatTy::F32 => {
+ let rust_bits = num.parse::<f32>()?.to_bits() as u128;
+ let apfloat = num.parse::<Single>().unwrap_or_else(|e| {
+ panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e);
+ });
+ let apfloat_bits = apfloat.to_bits();
+ assert!(rust_bits == apfloat_bits,
+ "apfloat::ieee::Single gave different result for `{}`: \
+ {}({:#x}) vs Rust's {}({:#x})",
+ num, apfloat, apfloat_bits,
+ Single::from_bits(rust_bits), rust_bits);
+ apfloat_bits