1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#![deny(
    missing_docs,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces,
    unused_qualifications
)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "std", deny(missing_debug_implementations))]

//! A Rust library for [IBM floating point
//! numbers](https://en.wikipedia.org/wiki/IBM_hexadecimal_floating_point), specifically focused on
//! converting them to IEEE-754 floating point values.
//!
//! See [`F32`](struct.F32.html) for 32-bit floats and [`F64`](struct.F64.html) for 64-bit floats.

#[cfg(feature = "std")]
use std::{cmp, fmt};

#[cfg(not(feature = "std"))]
use core::cmp;

mod convert;

/// A 32-bit IBM floating point number.
///
/// This type supports the conversions:
///
/// * Transmuting to/from a `u32` via `from_bits()`, `to_bits()`
/// * Transmuting to/from a big-endian `[u8; 4]` via `from_be_bytes()`/`to_be_bytes()`
/// * Lossily converting to an `f32` via `From`/`Into`
/// * Losslessly converting to an `f64` via `From`/`Into`
///
/// IBM `F32` floats have slightly less precision than IEEE-754 `f32` floats, but it covers a
/// slightly larger domain. `F32`s of typical magnitude can be converted to `f32` without rounding
/// or other loss of precision. Converting `F32`s of large magnitude to `f32` will cause rounding;
/// `F32`s of extreme magnitude can also cause overflow and underflow to occur.
///
/// Every `F32` can be precisely represented as an `f64`, without rounding, overflow, or underflow.
/// Those seeking a lossless path to IEEE-754 should convert `F32` to `f64`.
///
/// ```
/// // Use the example -118.625:
/// //   https://en.wikipedia.org/wiki/IBM_hexadecimal_floating_point#Example
/// let foreign_float = ibmfloat::F32::from_bits(0b1_1000010_0111_0110_1010_0000_0000_0000);
///
/// let native_float = f32::from(foreign_float);
/// assert_eq!(native_float, -118.625f32);
///
/// let native_float: f32 = foreign_float.into();
/// assert_eq!(native_float, -118.625f32);
/// ```
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct F32(u32);

impl F32 {
    /// Transmute a native-endian `u64` into an `F64`.
    ///
    /// ```
    /// let foreign_float = ibmfloat::F32::from_bits(0x46000001);
    ///
    /// let native_float = f32::from(foreign_float); // potential loss of precision
    /// assert_eq!(native_float, 1.0f32);
    ///
    /// let native_float = f64::from(foreign_float); // always exact
    /// assert_eq!(native_float, 1.0f64);
    /// ```
    #[inline]
    pub fn from_bits(value: u32) -> Self {
        Self(value)
    }

    /// Transmute this `F32` to a native-endian `u32`.
    ///
    /// ```
    /// let foreign_float = ibmfloat::F32::from_bits(0x46000001);
    ///
    /// assert_eq!(foreign_float.to_bits(), 0x46000001);
    /// ```
    #[inline]
    pub fn to_bits(self) -> u32 {
        self.0
    }

    /// Create a floating point value from its representation as a byte array in big endian.
    ///
    /// ```
    /// let foreign_float = ibmfloat::F32::from_be_bytes([0x46, 0, 0, 1]);
    ///
    /// assert_eq!(foreign_float.to_bits(), 0x46000001);
    ///
    /// let native_float = f32::from(foreign_float);
    /// assert_eq!(native_float, 1.0f32);
    /// ```
    #[inline]
    pub fn from_be_bytes(bytes: [u8; 4]) -> Self {
        Self(u32::from_be_bytes(bytes))
    }

    /// Return the memory representation of this floating point number as a byte array in big-endian
    /// (network) byte order.
    ///
    /// ```
    /// let foreign_float = ibmfloat::F32::from_bits(0x46000001);
    ///
    /// assert_eq!(foreign_float.to_be_bytes(), [0x46, 0, 0, 1]);
    /// ```
    #[inline]
    pub fn to_be_bytes(self) -> [u8; 4] {
        self.0.to_be_bytes()
    }
}

/// A 64-bit IBM floating point number.
///
/// This type supports the conversions:
///
/// * Transmuting to/from a `u64` via `from_bits()`, `to_bits()`
/// * Transmuting to/from a big-endian `[u8; 8]` via `from_be_bytes()`/`to_be_bytes()`
/// * Lossily converting to an `f32` via `From`/`Into`
/// * Lossily converting to an `f64` via `From`/`Into`
///
/// IBM `F64` floats have slightly more precision than IEEE-754 `f64` floats, but they cover a
/// slightly smaller domain. Most conversions will require rounding, but there is no risk of
/// overflow or underflow.
///
/// ```
/// let foreign_float = ibmfloat::F64::from_bits(0x4110000000000000);
///
/// let native_float = f64::from(foreign_float);
/// assert_eq!(native_float, 1.0f64);
///
/// let native_float: f64 = foreign_float.into();
/// assert_eq!(native_float, 1.0f64);
/// ```
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct F64(u64);

impl F64 {
    /// Transmute a native-endian `u64` into an `F64`.
    ///
    /// ```
    /// let foreign_float = ibmfloat::F64::from_bits(0x4110000000000000);
    ///
    /// let native_float = f64::from(foreign_float);
    /// assert_eq!(native_float, 1.0f64);
    /// ```
    #[inline]
    pub fn from_bits(value: u64) -> Self {
        Self(value)
    }

    /// Transmute this `F64` to a native-endian `u64`.
    ///
    /// ```
    /// let foreign_float = ibmfloat::F64::from_bits(0x4110000000000000);
    ///
    /// assert_eq!(foreign_float.to_bits(), 0x4110000000000000);
    /// ```
    #[inline]
    pub fn to_bits(self) -> u64 {
        self.0
    }

    /// Create a floating point value from its representation as a byte array in big endian.
    ///
    /// ```
    /// let foreign_float = ibmfloat::F64::from_be_bytes([0x41, 0x10, 0, 0, 0, 0, 0, 0]);
    ///
    /// assert_eq!(foreign_float.to_bits(), 0x4110000000000000);
    ///
    /// let native_float = f64::from(foreign_float);
    /// assert_eq!(native_float, 1.0f64);
    /// ```
    #[inline]
    pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
        Self(u64::from_be_bytes(bytes))
    }

    /// Return the memory representation of this floating point number as a byte array in big-endian
    /// (network) byte order.
    ///
    /// ```
    /// let foreign_float = ibmfloat::F64::from_bits(0x4110000000000000);
    ///
    /// assert_eq!(foreign_float.to_be_bytes(), [0x41, 0x10, 0, 0, 0, 0, 0, 0]);
    /// ```
    #[inline]
    pub fn to_be_bytes(self) -> [u8; 8] {
        self.0.to_be_bytes()
    }
}

macro_rules! float {
    ($t:ty) => {
        // Convert everything to an f64 and implement Debug, PartialEq, PartialOrd over top

        #[cfg(feature = "std")]
        impl fmt::Debug for $t {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f64::from(*self).fmt(f)
            }
        }

        impl PartialEq for $t {
            fn eq(&self, other: &Self) -> bool {
                f64::from(*self).eq(&f64::from(*other))
            }
        }

        impl PartialOrd for $t {
            fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
                f64::from(*self).partial_cmp(&f64::from(*other))
            }
        }
    };
}
float!(F32);
float!(F64);

impl From<F32> for f32 {
    #[inline]
    fn from(v: F32) -> Self {
        f32::from_bits(convert::ibm32ieee32(v.0))
    }
}

impl From<F32> for f64 {
    #[inline]
    fn from(v: F32) -> Self {
        f64::from_bits(convert::ibm32ieee64(v.0))
    }
}

impl From<F64> for f32 {
    #[inline]
    fn from(v: F64) -> Self {
        f32::from_bits(convert::ibm64ieee32(v.0))
    }
}

impl From<F64> for f64 {
    #[inline]
    fn from(v: F64) -> Self {
        f64::from_bits(convert::ibm64ieee64(v.0))
    }
}