decstr 0.2.0

IEEE 754 decimal floating point bitstrings
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use core::{
    fmt,
    ops::Index,
    str,
};

use crate::text::{
    ArrayTextBuf,
    FiniteParser,
    TextBuf,
    TextWriter,
};

/**
Generic integers.
*/
pub trait Integer {
    /**
    The integer represented as a little-endian buffer.
    */
    type Bytes: Index<usize, Output = u8>;

    /**
    Parse the integer from ASCII digits.
    */
    fn try_from_ascii<I: Iterator<Item = u8>>(is_negative: bool, ascii: I) -> Option<Self>
    where
        Self: Sized;

    /**
    Convert from a stream of bytes in little-endian byte-order.
    */
    fn from_le_bytes<I: Iterator<Item = u8>>(bytes: I) -> Self;

    /**
    Get an instance of the value `0`.
    */
    fn zero() -> Self
    where
        Self: Sized,
    {
        Self::from_i32(0)
    }

    /**
    Convert from a 32-bit signed integer.
    */
    fn from_i32(n: i32) -> Self;

    /**
    Try convert into a 32-bit signed integer.
    */
    fn to_i32(&self) -> Option<i32>;

    /**
    Whether or not this integer is negative.
    */
    fn is_negative(&self) -> bool;

    /**
    Convert this integer into a little-endian buffer.
    */
    fn to_le_bytes(&self) -> Self::Bytes;

    /**
    Write this integer as ASCII with an optional leading sign.
    */
    fn to_fmt<W: fmt::Write>(&self, out: W) -> fmt::Result;

    /**
    An adapter that can display this integer using its `to_fmt` implementation.
    */
    fn as_display(&self) -> AsDisplay<&Self> {
        AsDisplay(self)
    }
}

pub struct AsDisplay<T>(T);

impl<'a, T: Integer> fmt::Display for AsDisplay<&'a T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.to_fmt(f)
    }
}

/**
Generic floating points.
*/
pub(crate) trait Float {
    /**
    A text writer that can buffer any valid instance of this number.
    */
    type TextWriter: TextBuf + TextWriter + Default;

    /**
    An integer that can represent any valid payload on this number.
    */
    type NanPayload: Integer;

    /**
    Parse the number using the same text format as decimals.
    */
    fn try_finite_from_ascii<I: Iterator<Item = u8>, E: Integer>(
        is_negative: bool,
        ascii: I,
        exponent: E,
    ) -> Option<Self>
    where
        Self: Sized;

    /**
    Get an instance of an infinity.
    */
    fn infinity(is_negative: bool) -> Self;

    /**
    Get an instance of a NaN with the given payload.
    */
    fn nan(is_negative: bool, is_signaling: bool, payload: Self::NanPayload) -> Self;

    /**
    Whether or not the number is negative.
    */
    fn is_sign_negative(&self) -> bool;

    /**
    Whether or not the number is finite.
    */
    fn is_finite(&self) -> bool;

    /**
    Whether or not the number is infinity.
    */
    fn is_infinite(&self) -> bool;

    /**
    Whether or not the number is NaN.
    */
    fn is_nan(&self) -> bool;

    /**
    Whether or not the number is sNaN.

    Note that IEEE 754 didn't originally specify how to interpret the signaling bit, so older
    MIPS architectures interpret qNaN and sNaN differently to more recent ones.
    */
    fn is_nan_signaling(&self) -> bool;

    /**
    Get the payload with a NaN if there is one.
    */
    fn nan_payload(&self) -> Option<Self::NanPayload>;
}

macro_rules! impl_binary_integer {
    ($(($i:ty, $bytes:ty)),*) => {
        $(
            impl Integer for $i {
                type Bytes = $bytes;

                fn try_from_ascii<I: Iterator<Item = u8>>(is_negative: bool, ascii: I) -> Option<Self> {
                    let mut i: $i = 0;

                    if is_negative {
                        for b in ascii {
                            i = i.checked_mul(10)?;
                            i = i.checked_sub((b - b'0') as $i)?;
                        }
                    } else {
                        for b in ascii {
                            i = i.checked_mul(10)?;
                            i = i.checked_add((b - b'0') as $i)?;
                        }
                    }

                    Some(i)
                }

                fn from_le_bytes<I: Iterator<Item = u8>>(bytes: I) -> Self {
                    let mut buf = [0; (<$i>::BITS / 8) as usize];

                    for (i, b) in bytes.enumerate() {
                        buf[i] = b;
                    }

                    Self::from_le_bytes(buf)
                }

                fn from_i32(exp: i32) -> Self {
                    exp as $i
                }

                fn to_i32(&self) -> Option<i32> {
                    (*self).try_into().ok()
                }

                fn is_negative(&self) -> bool {
                    <$i>::is_negative(*self)
                }

                fn to_le_bytes(&self) -> Self::Bytes {
                    <$i>::to_le_bytes(*self)
                }

                fn to_fmt<W: fmt::Write>(&self, mut out: W) -> fmt::Result {
                    write!(out, "{}", self)
                }
            }
        )*
    };
}

macro_rules! impl_binary_unsigned_integer {
    ($(($i:ty, $bytes:ty)),*) => {
        $(
            impl Integer for $i {
                type Bytes = $bytes;

                fn try_from_ascii<I: Iterator<Item = u8>>(is_negative: bool, ascii: I) -> Option<Self> {
                    let mut i: $i = 0;

                    if is_negative {
                        return None;
                    } else {
                        for b in ascii {
                            i = i.checked_mul(10)?;
                            i = i.checked_add((b - b'0') as $i)?;
                        }
                    }

                    Some(i)
                }

                fn from_le_bytes<I: Iterator<Item = u8>>(bytes: I) -> Self {
                    let mut buf = [0; (<$i>::BITS / 8) as usize];

                    for (i, b) in bytes.enumerate() {
                        buf[i] = b;
                    }

                    Self::from_le_bytes(buf)
                }

                fn from_i32(exp: i32) -> Self {
                    exp as $i
                }

                fn to_i32(&self) -> Option<i32> {
                    (*self).try_into().ok()
                }

                fn is_negative(&self) -> bool {
                    false
                }

                fn to_le_bytes(&self) -> Self::Bytes {
                    <$i>::to_le_bytes(*self)
                }

                fn to_fmt<W: fmt::Write>(&self, mut out: W) -> fmt::Result {
                    write!(out, "{}", self)
                }
            }
        )*
    };
}

impl_binary_integer!(
    (i8, [u8; 1]),
    (i16, [u8; 2]),
    (i32, [u8; 4]),
    (i64, [u8; 8]),
    (i128, [u8; 16])
);
impl_binary_unsigned_integer!(
    (u8, [u8; 1]),
    (u16, [u8; 2]),
    (u32, [u8; 4]),
    (u64, [u8; 8]),
    (u128, [u8; 16])
);

// 2f32.powi(23 + 1).log10().ceil() + 1f32
const F32_MAX_MANTISSA_DIGITS: usize = 9;
const F32_MAX_EXPONENT_DIGITS: usize = 3;
const F32_BUF_SIZE: usize = F32_MAX_MANTISSA_DIGITS + F32_MAX_EXPONENT_DIGITS + 4;

// The payload for a NaN is the significand bits, except for the most significant,
// which is used to identify signaling vs quiet NaNs
const F32_NAN_PAYLOAD_MASK: u32 = 0b0000_0000_0111_1111_1111_1111_1111_1111u32;

// 2f64.powi(52 + 1).log10().ceil() + 1f64
const F64_MAX_MANTISSA_DIGITS: usize = 17;
const F64_MAX_EXPONENT_DIGITS: usize = 4;
const F64_BUF_SIZE: usize = F64_MAX_MANTISSA_DIGITS + F64_MAX_EXPONENT_DIGITS + 4;

// The payload for a NaN is the significand bits, except for the most significant,
// which is used to identify signaling vs quiet NaNs
const F64_NAN_PAYLOAD_MASK: u64 =
    0b0000_0000_0000_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111u64;

macro_rules! impl_binary_float {
    ($(($f:ty, $i:ty, $u:ty, $text_writer:ty, $nan_mask:ident)),*) => {
        $(
            impl Float for $f {
                type TextWriter = $text_writer;

                type NanPayload = $i;

                fn try_finite_from_ascii<I: Iterator<Item = u8>, E: Integer>(
                        is_negative: bool,
                        ascii: I,
                        exponent: E,
                ) -> Option<Self> {
                    parse_ascii(is_negative, ascii, exponent)
                }

                fn infinity(is_negative: bool) -> Self {
                    if is_negative {
                        <$f>::NEG_INFINITY
                    } else {
                        <$f>::INFINITY
                    }
                }

                fn nan(is_negative: bool, is_signaling: bool, payload: Self::NanPayload) -> Self {
                    // For binary floating points, always encode the NaN as quiet
                    let _ = is_signaling;

                    let nan = <$f>::NAN.to_bits();

                    let f = if payload == 0 {
                        <$f>::from_bits(nan)
                    } else {
                        <$f>::from_bits(nan | ((payload as $u) & $nan_mask))
                    };

                    if is_negative {
                        -f
                    } else {
                        f
                    }
                }

                fn is_sign_negative(&self) -> bool {
                    <$f>::is_sign_negative(*self)
                }

                fn is_finite(&self) -> bool {
                    <$f>::is_finite(*self)
                }

                fn is_infinite(&self) -> bool {
                    <$f>::is_infinite(*self)
                }

                fn is_nan(&self) -> bool {
                    <$f>::is_nan(*self)
                }

                fn is_nan_signaling(&self) -> bool {
                    // Signaling for binary floating points isn't particularly
                    // well defined, so we just unconditionally ignore it and
                    // assume any NaN is quiet.
                    false
                }

                fn nan_payload(&self) -> Option<Self::NanPayload> {
                    // Rust doesn't guarantee any particular NaN payload for
                    // the constants `f32::NAN` and `f64::NAN`, so we just
                    // unconditionally ignore the payload. This means information
                    // could be lost encoding a binary floating point through a
                    // decimal, but NaN payloads on binary floating points have
                    // rather sketchy portability as it is.
                    None
                }
            }
        )*
    };
}

impl_binary_float!(
    (
        f32,
        i32,
        u32,
        ArrayTextBuf<F32_BUF_SIZE>,
        F32_NAN_PAYLOAD_MASK
    ),
    (
        f64,
        i64,
        u64,
        ArrayTextBuf<F64_BUF_SIZE>,
        F64_NAN_PAYLOAD_MASK
    )
);

/**
Parse a binary floating point number from text.
*/
fn parse_ascii<F: Float + str::FromStr>(
    is_negative: bool,
    digits: impl Iterator<Item = u8>,
    exponent: impl Integer,
) -> Option<F>
where
    F::Err: fmt::Debug,
{
    let mut parser = FiniteParser::begin(F::TextWriter::default());

    // Parse the significand
    if is_negative {
        parser.checked_significand_is_negative().ok()?;
    }

    let mut written = 0;
    for digit in digits.skip_while(|d| *d == b'0') {
        parser.checked_push_significand_digit(digit).ok()?;
        written += 1;
    }
    if written == 0 {
        parser.checked_push_significand_digit(b'0').ok()?;
    }

    // Parse the exponent
    parser.checked_begin_exponent().ok()?;

    parser
        .parse_fmt(exponent.as_display())
        .expect("failed to parse exponent");

    let parsed = parser.end().expect("failed to finish parsing");

    // Convert the parsed value into a float
    str::from_utf8(parsed.finite_buf.get_ascii())
        .ok()?
        .parse()
        .ok()
}