lexical-util 1.0.7

Shared utilities for lexical creates.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
//! Utilities for Rust numbers.
//!
//! These traits define useful properties, methods, associated
//! types, and trait bounds, and conversions for working with
//! numbers in generic code.

use core::{fmt, mem, ops};

#[cfg(feature = "f16")]
use crate::bf16::bf16;
#[cfg(feature = "f16")]
use crate::f16::f16;
#[cfg(all(not(feature = "std"), any(feature = "parse-floats", feature = "write-floats")))]
use crate::libm;

// AS PRIMITIVE
// ------------

/// Type that can be converted to [`primitive`] values with `as`.
///
/// [`primitive`]: https://doc.rust-lang.org/rust-by-example/primitives.html
pub trait AsPrimitive: Copy + PartialEq + PartialOrd + Send + Sync + Sized {
    /// Convert the value to a [`u8`], as if by `value as u8`.
    fn as_u8(self) -> u8;

    /// Convert the value to a [`u16`], as if by `value as u16`.
    fn as_u16(self) -> u16;

    /// Convert the value to a [`u32`], as if by `value as u32`.
    fn as_u32(self) -> u32;

    /// Convert the value to a [`u64`], as if by `value as u64`.
    fn as_u64(self) -> u64;

    /// Convert the value to a [`u128`], as if by `value as u128`.
    fn as_u128(self) -> u128;

    /// Convert the value to a [`usize`], as if by `value as usize`.
    fn as_usize(self) -> usize;

    /// Convert the value to an [`i8`], as if by `value as i8`.
    fn as_i8(self) -> i8;

    /// Convert the value to an [`i16`], as if by `value as i16`.
    fn as_i16(self) -> i16;

    /// Convert the value to an [`i32`], as if by `value as i32`.
    fn as_i32(self) -> i32;

    /// Convert the value to an [`i64`], as if by `value as i64`.
    fn as_i64(self) -> i64;

    /// Convert the value to an [`i128`], as if by `value as i128`.
    fn as_i128(self) -> i128;

    /// Convert the value to an [`isize`], as if by `value as isize`.
    fn as_isize(self) -> isize;

    /// Convert the value to an [`f32`], as if by `value as f32`.
    fn as_f32(self) -> f32;

    /// Convert the value to an [`f64`], as if by `value as f64`.
    fn as_f64(self) -> f64;

    /// Convert the value from a [`u32`], as if by `value as _`.
    fn from_u32(value: u32) -> Self;

    /// Convert the value from a [`u64`], as if by `value as _`.
    fn from_u64(value: u64) -> Self;

    /// Convert the value to an [`struct@f16`], identical to `value as f16`
    /// if [`struct@f16`] was a primitive type.
    #[cfg(feature = "f16")]
    fn as_f16(self) -> f16;

    /// Convert the value to an [`struct@bf16`], identical to `value as bf16`
    /// if [`struct@bf16`] was a primitive type.
    #[cfg(feature = "f16")]
    fn as_bf16(self) -> bf16;
}

macro_rules! as_primitive {
    ($($t:ty)*) => ($(
        impl AsPrimitive for $t {
            #[inline(always)]
            fn as_u8(self) -> u8 {
                self as u8
            }

            #[inline(always)]
            fn as_u16(self) -> u16 {
                self as u16
            }

            #[inline(always)]
            fn as_u32(self) -> u32 {
                self as u32
            }

            #[inline(always)]
            fn as_u64(self) -> u64 {
                self as u64
            }

            #[inline(always)]
            fn as_u128(self) -> u128 {
                self as u128
            }

            #[inline(always)]
            fn as_usize(self) -> usize {
                self as usize
            }

            #[inline(always)]
            fn as_i8(self) -> i8 {
                self as i8
            }

            #[inline(always)]
            fn as_i16(self) -> i16 {
                self as i16
            }

            #[inline(always)]
            fn as_i32(self) -> i32 {
                self as i32
            }

            #[inline(always)]
            fn as_i64(self) -> i64 {
                self as i64
            }

            #[inline(always)]
            fn as_i128(self) -> i128 {
                self as i128
            }

            #[inline(always)]
            fn as_isize(self) -> isize {
                self as isize
            }

            #[inline(always)]
            fn as_f32(self) -> f32 {
                self as f32
            }

            #[inline(always)]
            fn as_f64(self) -> f64 {
                self as f64
            }

            #[inline(always)]
            fn from_u32(value: u32) -> Self {
                value as Self
            }

            #[inline(always)]
            fn from_u64(value: u64) -> Self {
                value as Self
            }

            #[cfg(feature = "f16")]
            #[inline(always)]
            fn as_f16(self) -> f16 {
                f16::from_f32(self as f32)
            }

            #[cfg(feature = "f16")]
            #[inline(always)]
            fn as_bf16(self) -> bf16 {
                bf16::from_f32(self as f32)
            }
        }
    )*)
}

as_primitive! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64 }

#[cfg(feature = "f16")]
macro_rules! half_as_primitive {
    ($($t:ty)*) => ($(
        impl AsPrimitive for $t {
            #[inline(always)]
            fn as_u8(self) -> u8 {
                self.as_f32() as u8
            }

            #[inline(always)]
            fn as_u16(self) -> u16 {
                self.as_f32() as u16
            }

            #[inline(always)]
            fn as_u32(self) -> u32 {
                self.as_f32() as u32
            }

            #[inline(always)]
            fn as_u64(self) -> u64 {
                self.as_f32() as u64
            }

            #[inline(always)]
            fn as_u128(self) -> u128 {
                self.as_f32() as u128
            }

            #[inline(always)]
            fn as_usize(self) -> usize {
                self.as_f32() as usize
            }

            #[inline(always)]
            fn as_i8(self) -> i8 {
                self.as_f32() as i8
            }

            #[inline(always)]
            fn as_i16(self) -> i16 {
                self.as_f32() as i16
            }

            #[inline(always)]
            fn as_i32(self) -> i32 {
                self.as_f32() as i32
            }

            #[inline(always)]
            fn as_i64(self) -> i64 {
                self.as_f32() as i64
            }

            #[inline(always)]
            fn as_i128(self) -> i128 {
                self.as_f32() as i128
            }

            #[inline(always)]
            fn as_isize(self) -> isize {
                self.as_f32() as isize
            }

            #[inline(always)]
            fn as_f32(self) -> f32 {
                self.as_f32() as f32
            }

            #[inline(always)]
            fn as_f64(self) -> f64 {
                self.as_f32() as f64
            }

            #[inline(always)]
            #[allow(clippy::as_underscore)] // reason="intentionally used in a generic sense"
            fn from_u32(value: u32) -> Self {
                Self::from_f32(value as _)
            }

            #[inline(always)]
            fn from_u64(value: u64) -> Self {
                _ = value;
                unimplemented!()
            }

            #[inline(always)]
            fn as_f16(self) -> f16 {
                f16::from_f32(self.as_f32())
            }

            #[inline(always)]
            fn as_bf16(self) -> bf16 {
                bf16::from_f32(self.as_f32())
            }
        }
    )*)
}

#[cfg(feature = "f16")]
half_as_primitive! { f16 bf16 }

// AS CAST
// -------

/// An interface for casting between machine scalars, as if `as` was used.
///
/// All values that the type can be cast to must be [`primitive`] values.
///
/// [`primitive`]: https://doc.rust-lang.org/rust-by-example/primitives.html
pub trait AsCast: AsPrimitive {
    /// Creates a number from another value that can be converted into
    /// a primitive via the [`AsPrimitive`] trait.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lexical_util::num::AsCast;
    ///
    /// assert_eq!(u8::as_cast(256u16), 256u16 as u8); // 0
    /// ```
    fn as_cast<N: AsPrimitive>(n: N) -> Self;
}

/// Allows the high-level conversion of generic types as if `as` was used.
///
/// # Examples
///
/// ```rust
/// use lexical_util::num::as_cast;
///
/// assert_eq!(as_cast::<u8, u16>(256u16), 256u16 as u8); // 0
/// ```
#[inline(always)]
pub fn as_cast<U: AsCast, T: AsCast>(t: T) -> U {
    U::as_cast(t)
}

macro_rules! as_cast {
    ($($t:ty, $meth:ident ; )*) => ($(
        impl AsCast for $t {
            #[inline(always)]
            #[allow(clippy::as_underscore)] // reason="intentional due to generic API"
            fn as_cast<N: AsPrimitive>(n: N) -> $t {
                n.$meth() as _
            }
        }
    )*);
}

as_cast!(
    u8, as_u8 ;
    u16, as_u16 ;
    u32, as_u32 ;
    u64, as_u64 ;
    u128, as_u128 ;
    usize, as_usize ;
    i8, as_i8 ;
    i16, as_i16 ;
    i32, as_i32 ;
    i64, as_i64 ;
    i128, as_i128 ;
    isize, as_isize ;
    f32, as_f32 ;
    f64, as_f64 ;
);

#[cfg(feature = "f16")]
as_cast!(
    f16, as_f16 ;
    bf16, as_bf16 ;
);

// PRIMITIVE
// ---------

/// The base trait for all [`primitive`] types.
///
/// [`primitive`]: https://doc.rust-lang.org/rust-by-example/primitives.html
pub trait Primitive: 'static + fmt::Debug + fmt::Display + AsCast {}

macro_rules! primitive {
    ($($t:ty)*) => ($(
        impl Primitive for $t {}
    )*)
}

primitive! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64 }

#[cfg(feature = "f16")]
primitive! { f16 bf16 }

// NUMBER
// ------

/// The base trait for all numbers (integers and floating-point numbers).
pub trait Number:
    Default +
    Primitive +
    // Operations
    ops::Add<Output=Self> +
    ops::AddAssign +
    ops::Div<Output=Self> +
    ops::DivAssign +
    ops::Mul<Output=Self> +
    ops::MulAssign +
    ops::Rem<Output=Self> +
    ops::RemAssign +
    ops::Sub<Output=Self> +
    ops::SubAssign
{
    /// If the number can hold negative values.
    const IS_SIGNED: bool;
}

macro_rules! number_impl {
    ($($t:tt $is_signed:literal ; )*) => ($(
        impl Number for $t {
            const IS_SIGNED: bool = $is_signed;
        }
    )*)
}

number_impl! {
    u8 false ;
    u16 false ;
    u32 false ;
    u64 false ;
    u128 false ;
    usize false ;
    i8 true ;
    i16 true ;
    i32 true ;
    i64 true ;
    i128 true ;
    isize true ;
    f32 true ;
    f64 true ;
    // f128 true
}

#[cfg(feature = "f16")]
number_impl! {
    f16 true ;
    bf16 true ;
}

// INTEGER
// -------

/// The base trait for all signed and unsigned [`integers`].
///
/// [`integers`]: https://en.wikipedia.org/wiki/Integer_(computer_science)
pub trait Integer:
    // Basic
    Number + Eq + Ord +
    // Operations
    ops::BitAnd<Output=Self> +
    ops::BitAndAssign +
    ops::BitOr<Output=Self> +
    ops::BitOrAssign +
    ops::BitXor<Output=Self> +
    ops::BitXorAssign +
    ops::Not<Output=Self> +
    ops::Shl<Self, Output=Self> +
    ops::Shl<i32, Output=Self> +
    ops::ShlAssign<i32> +
    ops::Shr<i32, Output=Self> +
    ops::ShrAssign<i32> +
{
    // CONSTANTS
    /// A value equal to `0`.
    const ZERO: Self;

    /// A value equal to `1`.
    const ONE: Self;

    /// A value equal to `2`.
    const TWO: Self;

    /// The largest value that can be represented by this integer type.
    ///
    /// See [`u32::MAX`].
    const MAX: Self;

    /// The smallest value that can be represented by this integer type.
    ///
    /// See [`u32::MIN`].
    const MIN: Self;

    /// The size of this integer type in bits.
    ///
    /// See [`u32::BITS`].
    const BITS: usize;

    // FUNCTIONS (INHERITED)
    /// Returns the number of leading zeros in the binary representation
    /// of `self`.
    ///
    /// See [`u32::leading_zeros`].
    fn leading_zeros(self) -> u32;

    /// Returns the number of trailing zeros in the binary representation
    /// of `self`.
    ///
    /// See [`u32::trailing_zeros`].
    fn trailing_zeros(self) -> u32;

    /// Raises self to the power of `exp`, using exponentiation by squaring.
    ///
    /// See [`u32::pow`].
    fn pow(self, exp: u32) -> Self;

    /// Checked exponentiation. Computes `self.pow(exp)`, returning
    /// `None` if overflow occurred.
    ///
    /// See [`u32::checked_pow`].
    fn checked_pow(self, exp: u32) -> Option<Self>;

    /// Raises self to the power of `exp`, using exponentiation by squaring.
    ///
    /// Returns a tuple of the exponentiation along with a bool indicating
    /// whether an overflow happened.
    ///
    /// See [`u32::overflowing_pow`].
    fn overflowing_pow(self, exp: u32) -> (Self, bool);

    /// Checked integer addition. Computes `self + i`, returning `None` if
    /// overflow occurred.
    ///
    /// See [`u32::checked_add`].
    fn checked_add(self, i: Self) -> Option<Self>;

    /// Checked integer subtraction. Computes `self - i`, returning `None`
    /// if overflow occurred.
    ///
    /// See [`u32::checked_sub`].
    fn checked_sub(self, i: Self) -> Option<Self>;

    /// Checked integer multiplication. Computes `self * rhs`, returning `None`
    /// if overflow occurred.
    ///
    /// See [`u32::checked_mul`].
    fn checked_mul(self, i: Self) -> Option<Self>;

    /// Calculates `self + i`.
    ///
    /// Returns a tuple of the addition along with a boolean indicating whether
    /// an arithmetic overflow would occur. If an overflow would have occurred
    /// then the wrapped value is returned. See [`u32::overflowing_add`].
    fn overflowing_add(self, i: Self) -> (Self, bool);

    /// Calculates `self - i`.
    ///
    /// Returns a tuple of the addition along with a boolean indicating whether
    /// an arithmetic overflow would occur. If an overflow would have occurred
    /// then the wrapped value is returned. See [`u32::overflowing_sub`].
    fn overflowing_sub(self, i: Self) -> (Self, bool);

    /// Calculates `self * i`.
    ///
    /// Returns a tuple of the addition along with a boolean indicating whether
    /// an arithmetic overflow would occur. If an overflow would have occurred
    /// then the wrapped value is returned. See [`u32::overflowing_mul`].
    fn overflowing_mul(self, i: Self) -> (Self, bool);

    /// Wrapping (modular) addition. Computes `self + i`, wrapping around at
    /// the boundary of the type.
    ///
    /// See [`u32::wrapping_add`].
    fn wrapping_add(self, i: Self) -> Self;

    /// Wrapping (modular) subtraction. Computes `self - i`, wrapping around at
    /// the boundary of the type.
    ///
    /// See [`u32::wrapping_sub`].
    fn wrapping_sub(self, i: Self) -> Self;

    /// Wrapping (modular) multiplication. Computes `self * i`, wrapping around at
    /// the boundary of the type.
    ///
    /// See [`u32::wrapping_mul`].
    fn wrapping_mul(self, i: Self) -> Self;

    /// Wrapping (modular) negation. Computes `-self`, wrapping around at
    /// the boundary of the type.
    ///
    /// See [`u32::wrapping_neg`].
    fn wrapping_neg(self) -> Self;

    /// Saturating integer addition. Computes `self + i`, saturating at the
    /// numeric bounds instead of overflowing.
    ///
    /// See [`u32::saturating_add`].
    fn saturating_add(self, i: Self) -> Self;

    /// Saturating integer subtraction. Computes `self - i`, saturating at the
    /// numeric bounds instead of overflowing.
    ///
    /// See [`u32::saturating_sub`].
    fn saturating_sub(self, i: Self) -> Self;

    /// Saturating integer multiplication. Computes `self * i`, saturating at
    /// the numeric bounds instead of overflowing.
    ///
    /// See [`u32::saturating_mul`].
    fn saturating_mul(self, i: Self) -> Self;

    /// Get the fast ceiling of the quotient from integer division.
    ///
    /// The remainder may wrap to the numerical boundaries for the type.
    /// See [`u32::div_ceil`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lexical_util::num::Integer;
    ///
    /// assert_eq!(250u16.ceil_divmod(10), (25, 0));
    /// assert_eq!(256u16.ceil_divmod(10), (26, -4));
    /// assert_eq!(i32::MAX.ceil_divmod(-2), (-0x3FFFFFFE, 3));
    ///
    /// // notice how `-1` wraps since `i32` cannot hold `i128::MAX`.
    /// assert_eq!((i128::MAX - 1).ceil_divmod(i128::MAX), (1, -1));
    /// ```
    #[inline(always)]
    fn ceil_divmod(self, y: Self) -> (Self, i32) {
        let q = self / y;
        let r = self % y;
        match r == Self::ZERO {
            true  => (q, i32::as_cast(r)),
            false => (q + Self::ONE, i32::as_cast(r) - i32::as_cast(y))
        }
    }

    /// Get the fast ceiling of the quotient from integer division.
    ///
    /// This is identical to [`u32::div_ceil`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lexical_util::num::Integer;
    ///
    /// assert_eq!(250u16.ceil_div(10), 25);
    /// assert_eq!(256u16.ceil_div(10), 26);
    /// assert_eq!(i32::MAX.ceil_div(-2), -0x3FFFFFFE);
    /// assert_eq!((i128::MAX - 1).ceil_div(i128::MAX), 1);
    /// ```
    #[inline(always)]
    fn ceil_div(self, y: Self) -> Self {
        self.ceil_divmod(y).0
    }

    /// Get the fast ceiling modulus from integer division.
    ///
    /// The remainder is not guaranteed to be valid since it can
    /// overflow if the remainder is not 0. See [`Self::ceil_divmod`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lexical_util::num::Integer;
    ///
    /// assert_eq!(250u16.ceil_mod(10), 0);
    /// assert_eq!(256u16.ceil_mod(10), -4);
    /// assert_eq!(i32::MAX.ceil_mod(-2), 3);
    ///
    /// // notice how `-1` wraps since `i32` cannot hold `i128::MAX`.
    /// assert_eq!((i128::MAX - 1).ceil_mod(i128::MAX), -1);
    /// ```
    #[inline(always)]
    fn ceil_mod(self, y: Self) -> i32 {
        self.ceil_divmod(y).1
    }

    // PROPERTIES

    /// Get the number of bits in a value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lexical_util::num::Integer;
    ///
    /// assert_eq!(1u64.bit_length(), 1);
    /// assert_eq!(2u64.bit_length(), 2);
    /// assert_eq!(3u64.bit_length(), 2);
    /// assert_eq!(16u64.bit_length(), 5);
    /// ```
    #[inline(always)]
    fn bit_length(self) -> u32 {
        Self::BITS as u32 - self.leading_zeros()
    }

    /// Returns true if the least-significant bit is odd.
    #[inline(always)]
    fn is_odd(self) -> bool {
        self & Self::ONE == Self::ONE
    }

    /// Returns true if the least-significant bit is even.
    #[inline(always)]
    fn is_even(self) -> bool {
        !self.is_odd()
    }

    /// Get the maximum number of digits before the slice will overflow.
    ///
    /// This is effectively the `floor(log(2^BITS-1, radix))`, but we can
    /// try to go a bit lower without worrying too much.
    #[inline(always)]
    fn overflow_digits(radix: u32) -> usize {
        // this is heavily optimized for base10 and it's a way under estimate
        // that said, it's fast and works.
        if radix <= 16 {
            mem::size_of::<Self>() * 2 - Self::IS_SIGNED as usize
        } else {
            // way under approximation but always works and is fast
            mem::size_of::<Self>()
        }
    }
}

macro_rules! integer_impl {
($($t:tt)*) => ($(
    impl Integer for $t {
        const ZERO: $t = 0;
        const ONE: $t = 1;
        const TWO: $t = 2;
        const MAX: $t = $t::MAX;
        const MIN: $t = $t::MIN;
        const BITS: usize = $t::BITS as usize;

        #[inline(always)]
        fn leading_zeros(self) -> u32 {
            $t::leading_zeros(self)
        }

        #[inline(always)]
        fn trailing_zeros(self) -> u32 {
            $t::trailing_zeros(self)
        }

        #[inline(always)]
        fn checked_add(self, i: Self) -> Option<Self> {
            $t::checked_add(self, i)
        }

        #[inline(always)]
        fn checked_sub(self, i: Self) -> Option<Self> {
            $t::checked_sub(self, i)
        }

        #[inline(always)]
        fn checked_mul(self, i: Self) -> Option<Self> {
            $t::checked_mul(self, i)
        }

        #[inline(always)]
        fn overflowing_add(self, i: Self) -> (Self, bool) {
            $t::overflowing_add(self, i)
        }

        #[inline(always)]
        fn overflowing_sub(self, i: Self) -> (Self, bool) {
            $t::overflowing_sub(self, i)
        }

        #[inline(always)]
        fn overflowing_mul(self, i: Self) -> (Self, bool) {
            $t::overflowing_mul(self, i)
        }

        #[inline(always)]
        fn wrapping_add(self, i: Self) -> Self {
            $t::wrapping_add(self, i)
        }

        #[inline(always)]
        fn wrapping_sub(self, i: Self) -> Self {
            $t::wrapping_sub(self, i)
        }

        #[inline(always)]
        fn wrapping_mul(self, i: Self) -> Self {
            $t::wrapping_mul(self, i)
        }

        #[inline(always)]
        fn wrapping_neg(self) -> Self {
            $t::wrapping_neg(self)
        }

        #[inline(always)]
        fn pow(self, exp: u32) -> Self {
            Self::pow(self, exp)
        }

        #[inline(always)]
        fn checked_pow(self, exp: u32) -> Option<Self> {
            Self::checked_pow(self, exp)
        }

        #[inline(always)]
        fn overflowing_pow(self, exp: u32) -> (Self, bool) {
            Self::overflowing_pow(self, exp)
        }

        #[inline(always)]
        fn saturating_add(self, i: Self) -> Self {
            $t::saturating_add(self, i)
        }

        #[inline(always)]
        fn saturating_sub(self, i: Self) -> Self {
            $t::saturating_sub(self, i)
        }

        #[inline(always)]
        fn saturating_mul(self, i: Self) -> Self {
            $t::saturating_mul(self, i)
        }
    }
)*)
}

integer_impl! { u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 usize isize }

// SIGNED INTEGER
// --------------

/// The trait for types that support [`signed`] integral operations, that is,
/// they can hold negative numbers.
///
/// [`signed`]: https://en.wikipedia.org/wiki/Integer_(computer_science)#Value_and_representation
pub trait SignedInteger: Integer + ops::Neg<Output = Self> {}

macro_rules! signed_integer_impl {
($($t:tt)*) => ($(
    impl SignedInteger for $t {}
)*)
}

signed_integer_impl! { i8 i16 i32 i64 i128 isize }

// UNSIGNED INTEGER
// ----------------

/// The trait for types that support [`unsigned`] integral operations, that is,
/// they can only hold positive numbers.
///
/// [`unsigned`]: https://en.wikipedia.org/wiki/Integer_(computer_science)#Value_and_representation
pub trait UnsignedInteger: Integer {}

macro_rules! unsigned_integer_impl {
($($t:ty)*) => ($(
    impl UnsignedInteger for $t {}
)*)
}

unsigned_integer_impl! { u8 u16 u32 u64 u128 usize }

// FLOAT
// -----

/// The trait for floating-point [`numbers`][`floats`].
///
/// Floating-point numbers are numbers that may contain a fraction
/// and are stored internally as the significant digits and an
/// exponent of base 2.
///
/// [`floats`]: https://en.wikipedia.org/wiki/Floating-point_arithmetic
#[cfg(any(feature = "parse-floats", feature = "write-floats"))]
pub trait Float: Number + ops::Neg<Output = Self> {
    /// Unsigned type of the same size.
    type Unsigned: UnsignedInteger;

    // CONSTANTS

    /// A value equal to `0`.
    const ZERO: Self;

    /// A value equal to `1`.
    const ONE: Self;

    /// A value equal to `2`.
    const TWO: Self;

    /// Largest finite value.
    ///
    /// See [`f64::MAX`].
    const MAX: Self;

    /// Smallest finite value.
    ///
    /// See [`f64::MIN`].
    const MIN: Self;

    /// Infinity (`∞`).
    ///
    /// See [`f64::INFINITY`].
    const INFINITY: Self;

    /// Negative infinity (`−∞`).
    ///
    /// See [`f64::NEG_INFINITY`].
    const NEG_INFINITY: Self;

    /// Not a Number (NaN).
    ///
    /// See [`f64::NAN`].
    const NAN: Self;

    /// The size of this float type in bits.
    ///
    /// Analogous to [`u32::BITS`].
    const BITS: usize;

    /// Bitmask to extract the sign from the float.
    const SIGN_MASK: Self::Unsigned;

    /// Bitmask to extract the biased exponent, including the hidden bit.
    const EXPONENT_MASK: Self::Unsigned;

    /// Bitmask to extract the hidden bit in the exponent, which is an
    /// implicit 1 in the significant digits.
    const HIDDEN_BIT_MASK: Self::Unsigned;

    /// Bitmask to extract the mantissa (significant digits), excluding
    /// the hidden bit.
    const MANTISSA_MASK: Self::Unsigned;

    /// Mask to determine if a full-carry occurred (1 in bit above hidden bit).
    const CARRY_MASK: Self::Unsigned;

    // PROPERTIES

    // The following constants can be calculated as follows:
    //  - `INFINITY_BITS`: EXPONENT_MASK
    //  - `NEGATIVE_INFINITY_BITS`: INFINITY_BITS | SIGN_MASK
    //  - `EXPONENT_BIAS`: `2^(EXPONENT_SIZE-1) - 1 + MANTISSA_SIZE`
    //  - `DENORMAL_EXPONENT`: `1 - EXPONENT_BIAS`
    //  - `MAX_EXPONENT`: `2^EXPONENT_SIZE - 1 - EXPONENT_BIAS`

    /// Positive infinity as bits.
    const INFINITY_BITS: Self::Unsigned;

    /// Positive infinity as bits.
    const NEGATIVE_INFINITY_BITS: Self::Unsigned;

    /// The number of bits in the exponent.
    const EXPONENT_SIZE: i32;

    /// Size of the significand (mantissa) without the hidden bit.
    const MANTISSA_SIZE: i32;

    /// Bias of the exponent. See [`exponent bias`].
    ///
    /// [`exponent bias`]: https://en.wikipedia.org/wiki/Exponent_bias
    const EXPONENT_BIAS: i32;

    /// Exponent portion of a [`denormal`] float.
    ///
    /// [`denormal`]: https://en.wikipedia.org/wiki/Subnormal_number
    const DENORMAL_EXPONENT: i32;

    /// Maximum (unbiased) exponent value in the float.
    const MAX_EXPONENT: i32;

    // FUNCTIONS (INHERITED)

    // Re-export the to and from bits methods.

    /// Raw transmutation to the unsigned integral type.
    ///
    /// See [`f64::to_bits`].
    fn to_bits(self) -> Self::Unsigned;

    /// Raw transmutation from the unsigned integral type.
    ///
    /// See [`f64::from_bits`].
    fn from_bits(u: Self::Unsigned) -> Self;

    /// Returns the natural logarithm of the number.
    ///
    /// See [`f64::ln`].
    fn ln(self) -> Self;

    /// Returns the largest integer less than or equal to `self`.
    ///
    /// See [`f64::floor`].
    fn floor(self) -> Self;

    /// Returns true if `self` has a positive sign, including `+0.0`,
    /// NaNs with positive sign bit and positive infinity.
    ///
    /// See [`f64::is_sign_positive`].
    fn is_sign_positive(self) -> bool;

    /// Returns true if `self` has a negative sign, including `-0.0`,
    /// NaNs with negative sign bit and negative infinity.
    ///
    /// See [`f64::is_sign_negative`].
    fn is_sign_negative(self) -> bool;

    /// Returns true if the float is [`denormal`].
    ///
    /// Denormal (subnormal) numbers fall below the range of numbers
    /// that can be stored as `mantissa * 2^exp`, and therefore
    /// always have the minimum exponent.
    ///
    /// [`denormal`]: https://en.wikipedia.org/wiki/Subnormal_number
    #[inline(always)]
    fn is_denormal(self) -> bool {
        self.to_bits() & Self::EXPONENT_MASK == Self::Unsigned::ZERO
    }

    /// Returns true if the float is NaN, positive infinity, or negative
    /// infinity.
    #[inline(always)]
    fn is_special(self) -> bool {
        self.to_bits() & Self::EXPONENT_MASK == Self::EXPONENT_MASK
    }

    /// Returns true if the float is NaN.
    #[inline(always)]
    fn is_nan(self) -> bool {
        self.is_special() && (self.to_bits() & Self::MANTISSA_MASK) != Self::Unsigned::ZERO
    }

    /// Returns true if the float is positive or negative infinity.
    #[inline(always)]
    fn is_inf(self) -> bool {
        self.is_special() && (self.to_bits() & Self::MANTISSA_MASK) == Self::Unsigned::ZERO
    }

    /// Returns true if the float's least-significant mantissa bit is odd.
    #[inline(always)]
    fn is_odd(self) -> bool {
        self.to_bits().is_odd()
    }

    /// Returns true if the float's least-significant mantissa bit is even.
    #[inline(always)]
    fn is_even(self) -> bool {
        !self.is_odd()
    }

    /// Returns true if the float needs a negative sign when serializing it.
    ///
    /// This is true if it's `-0.0` or it's below 0 and not NaN. But inf values
    /// need the sign.
    #[inline(always)]
    fn needs_negative_sign(self) -> bool {
        self.is_sign_negative() && !self.is_nan()
    }

    /// Get the unbiased exponent component from the float.
    #[inline(always)]
    fn exponent(self) -> i32 {
        if self.is_denormal() {
            return Self::DENORMAL_EXPONENT;
        }

        let bits = self.to_bits();
        let biased_e = i32::as_cast((bits & Self::EXPONENT_MASK) >> Self::MANTISSA_SIZE).as_i32();
        biased_e - Self::EXPONENT_BIAS
    }

    /// Get the mantissa (significand) component from float.
    #[inline(always)]
    fn mantissa(self) -> Self::Unsigned {
        let bits = self.to_bits();
        let s = bits & Self::MANTISSA_MASK;
        if !self.is_denormal() {
            s + Self::HIDDEN_BIT_MASK
        } else {
            s
        }
    }

    /// Get next greater float.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lexical_util::num::Float;
    ///
    /// assert_eq!(1f32.next(), 1.0000001);
    /// assert_eq!((-0.0f32).next(), 0.0); // +0.0
    /// assert_eq!(0f32.next(), 1e-45);
    /// ```
    #[inline(always)]
    fn next(self) -> Self {
        let bits = self.to_bits();
        if self.is_sign_negative() && self == Self::ZERO {
            // -0.0
            Self::ZERO
        } else if bits == Self::INFINITY_BITS {
            Self::from_bits(Self::INFINITY_BITS)
        } else if self.is_sign_negative() {
            Self::from_bits(bits.saturating_sub(Self::Unsigned::ONE))
        } else {
            Self::from_bits(bits.saturating_add(Self::Unsigned::ONE))
        }
    }

    /// Get next greater float for a positive float.
    ///
    /// Value must be >= 0.0 and < INFINITY.
    #[inline(always)]
    fn next_positive(self) -> Self {
        debug_assert!(self.is_sign_positive() && !self.is_inf());
        Self::from_bits(self.to_bits() + Self::Unsigned::ONE)
    }

    /// Get previous greater float, such that `self.prev().next() == self`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lexical_util::num::Float;
    ///
    /// assert_eq!(1f32.prev(), 0.99999994);
    /// assert_eq!(0.0f32.prev(), 0.0); // -0.0
    /// assert_eq!((-0.0f32).prev(), -1e-45);
    /// ```
    #[inline(always)]
    fn prev(self) -> Self {
        let bits = self.to_bits();
        if self.is_sign_positive() && self == Self::ZERO {
            // +0.0
            -Self::ZERO
        } else if bits == Self::NEGATIVE_INFINITY_BITS {
            Self::from_bits(Self::NEGATIVE_INFINITY_BITS)
        } else if self.is_sign_negative() {
            Self::from_bits(bits.saturating_add(Self::Unsigned::ONE))
        } else {
            Self::from_bits(bits.saturating_sub(Self::Unsigned::ONE))
        }
    }

    /// Get previous greater float for a positive float.
    /// Value must be > 0.0.
    #[inline(always)]
    fn prev_positive(self) -> Self {
        debug_assert!(self.is_sign_positive() && self != Self::ZERO);
        Self::from_bits(self.to_bits() - Self::Unsigned::ONE)
    }

    /// Round a positive number to even.
    #[inline(always)]
    fn round_positive_even(self) -> Self {
        if self.mantissa().is_odd() {
            self.next_positive()
        } else {
            self
        }
    }

    /// Get the max of two finite numbers.
    ///
    /// This assumes that both floats form a [`total ord`],
    /// that is, `x < y` is always `y >= x`. Non-finite floats,
    /// such as NaN, break this criteria, but finite floats enable
    /// simpler (and faster) comparison criteria while remaining
    /// accurate.
    ///
    /// [`total ord`]: https://doc.rust-lang.org/std/cmp/trait.Ord.html
    #[inline(always)]
    fn max_finite(self, f: Self) -> Self {
        debug_assert!(!self.is_special() && !f.is_special(), "max_finite self={} f={}", self, f);
        if self < f {
            f
        } else {
            self
        }
    }

    /// Get the min of two finite numbers.
    ///
    /// This assumes that both floats form a [`total ord`],
    /// that is, `x < y` is always `y >= x`. Non-finite floats,
    /// such as NaN, break this criteria, but finite floats enable
    /// simpler (and faster) comparison criteria while remaining
    /// accurate.
    ///
    /// [`total ord`]: https://doc.rust-lang.org/std/cmp/trait.Ord.html
    #[inline(always)]
    fn min_finite(self, f: Self) -> Self {
        debug_assert!(!self.is_special() && !f.is_special(), "min_finite self={} f={}", self, f);
        if self < f {
            self
        } else {
            f
        }
    }
}

/// Define the float literals.
#[cfg(any(feature = "parse-floats", feature = "write-floats"))]
macro_rules! float_literals {
    ($float:ty) => {
        const ZERO: $float = 0.0;
        const ONE: $float = 1.0;
        const TWO: $float = 2.0;
        const MAX: $float = <$float>::MAX;
        const MIN: $float = <$float>::MIN;
        const INFINITY: $float = <$float>::INFINITY;
        const NEG_INFINITY: $float = <$float>::NEG_INFINITY;
        const NAN: $float = <$float>::NAN;
        const BITS: usize = mem::size_of::<$float>() * 8;
    };
}

/// Define the float masks.
#[cfg(any(feature = "parse-floats", feature = "write-floats"))]
macro_rules! float_masks {
    (
        float =>
        $float:ty,sign_mask =>
        $sign:literal,exponent_mask =>
        $exponent:literal,hidden_bit_mask =>
        $hidden:literal,mantissa_mask =>
        $mantissa:literal,
    ) => {
        const SIGN_MASK: <$float>::Unsigned = $sign;
        const EXPONENT_MASK: <$float>::Unsigned = $exponent;
        const HIDDEN_BIT_MASK: <$float>::Unsigned = $hidden;
        const MANTISSA_MASK: <$float>::Unsigned = $mantissa;
        // The carry mask is always 1 bit above the hidden bit.
        const CARRY_MASK: <$float>::Unsigned = $hidden << 1;
        // Infinity is always every exponent bit set.
        const INFINITY_BITS: <$float>::Unsigned = $exponent;
        // Negative infinity is just infinity + sign.
        const NEGATIVE_INFINITY_BITS: <$float>::Unsigned = $exponent | $sign;
    };
}

//  Due to missing specifics or types for the following float types,
//  `Float` is not yet fully implemented for:
//      - f128

#[cfg(feature = "f16")]
macro_rules! float_one {
    ($f:ident) => {
        (($f::EXPONENT_BIAS - $f::MANTISSA_SIZE) as u16) << $f::MANTISSA_SIZE
    };
}

#[cfg(feature = "f16")]
macro_rules! float_two {
    ($f:ident) => {
        (($f::EXPONENT_BIAS - $f::MANTISSA_SIZE + 1) as u16) << $f::MANTISSA_SIZE
    };
}

#[cfg(feature = "f16")]
macro_rules! float_max {
    ($f:ident) => {
        ($f::EXPONENT_MASK ^ $f::HIDDEN_BIT_MASK) | $f::MANTISSA_MASK
    };
}

#[cfg(feature = "f16")]
macro_rules! float_min {
    ($f:ident) => {
        $f::MAX.to_bits() | $f::SIGN_MASK
    };
}

#[cfg(feature = "f16")]
macro_rules! float_nan {
    ($f:ident) => {
        $f::EXPONENT_MASK | ($f::HIDDEN_BIT_MASK >> 1)
    };
}

#[cfg(feature = "f16")]
impl Float for f16 {
    type Unsigned = u16;

    const ZERO: Self = Self::from_bits(0);
    const ONE: Self = Self::from_bits(float_one!(Self));
    const TWO: Self = Self::from_bits(float_two!(Self));
    const MAX: Self = Self::from_bits(float_max!(Self));
    const MIN: Self = Self::from_bits(float_min!(Self));
    const INFINITY: Self = Self::from_bits(Self::INFINITY_BITS);
    const NEG_INFINITY: Self = Self::from_bits(Self::NEGATIVE_INFINITY_BITS);
    const NAN: Self = Self::from_bits(float_nan!(Self));
    const BITS: usize = mem::size_of::<Self>() * 8;

    float_masks!(
        float => Self,
        sign_mask => 0x8000,
        exponent_mask => 0x7C00,
        hidden_bit_mask => 0x0400,
        mantissa_mask => 0x03FF,
    );
    const EXPONENT_SIZE: i32 = 5;
    const MANTISSA_SIZE: i32 = 10;
    const EXPONENT_BIAS: i32 = 15 + Self::MANTISSA_SIZE;
    const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
    const MAX_EXPONENT: i32 = 0x1F - Self::EXPONENT_BIAS;

    #[inline(always)]
    fn to_bits(self) -> u16 {
        f16::to_bits(self)
    }

    #[inline(always)]
    fn from_bits(u: u16) -> f16 {
        f16::from_bits(u)
    }

    #[inline(always)]
    fn ln(self) -> f16 {
        f16::from_f32(self.as_f32().ln())
    }

    #[inline(always)]
    fn floor(self) -> f16 {
        f16::from_f32(self.as_f32().floor())
    }

    #[inline(always)]
    fn is_sign_positive(self) -> bool {
        self.to_bits() & Self::SIGN_MASK == 0
    }

    #[inline(always)]
    fn is_sign_negative(self) -> bool {
        !self.is_sign_positive()
    }
}

#[cfg(feature = "f16")]
impl Float for bf16 {
    type Unsigned = u16;

    const ZERO: Self = Self::from_bits(0);
    const ONE: Self = Self::from_bits(float_one!(Self));
    const TWO: Self = Self::from_bits(float_two!(Self));
    const MAX: Self = Self::from_bits(float_max!(Self));
    const MIN: Self = Self::from_bits(float_min!(Self));
    const INFINITY: Self = Self::from_bits(Self::INFINITY_BITS);
    const NEG_INFINITY: Self = Self::from_bits(Self::NEGATIVE_INFINITY_BITS);
    const NAN: Self = Self::from_bits(float_nan!(Self));
    const BITS: usize = mem::size_of::<Self>() * 8;

    float_masks!(
        float => Self,
        sign_mask => 0x8000,
        exponent_mask => 0x7F80,
        hidden_bit_mask => 0x0080,
        mantissa_mask => 0x007F,
    );
    const EXPONENT_SIZE: i32 = 8;
    const MANTISSA_SIZE: i32 = 7;
    const EXPONENT_BIAS: i32 = 127 + Self::MANTISSA_SIZE;
    const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
    const MAX_EXPONENT: i32 = 0xFF - Self::EXPONENT_BIAS;

    #[inline(always)]
    fn to_bits(self) -> u16 {
        bf16::to_bits(self)
    }

    #[inline(always)]
    fn from_bits(u: u16) -> bf16 {
        bf16::from_bits(u)
    }

    #[inline(always)]
    fn ln(self) -> bf16 {
        bf16::from_f32(self.as_f32().ln())
    }

    #[inline(always)]
    fn floor(self) -> bf16 {
        bf16::from_f32(self.as_f32().floor())
    }

    #[inline(always)]
    fn is_sign_positive(self) -> bool {
        self.to_bits() & Self::SIGN_MASK == 0
    }

    #[inline(always)]
    fn is_sign_negative(self) -> bool {
        !self.is_sign_positive()
    }
}

#[cfg(any(feature = "parse-floats", feature = "write-floats"))]
impl Float for f32 {
    type Unsigned = u32;
    float_literals!(f32);
    float_masks!(
        float => Self,
        sign_mask => 0x80000000,
        exponent_mask => 0x7F800000,
        hidden_bit_mask => 0x00800000,
        mantissa_mask => 0x007FFFFF,
    );
    const EXPONENT_SIZE: i32 = 8;
    const MANTISSA_SIZE: i32 = 23;
    const EXPONENT_BIAS: i32 = 127 + Self::MANTISSA_SIZE;
    const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
    const MAX_EXPONENT: i32 = 0xFF - Self::EXPONENT_BIAS;

    #[inline(always)]
    fn to_bits(self) -> u32 {
        f32::to_bits(self)
    }

    #[inline(always)]
    fn from_bits(u: u32) -> f32 {
        f32::from_bits(u)
    }

    #[inline(always)]
    fn ln(self) -> f32 {
        #[cfg(feature = "std")]
        return f32::ln(self);

        #[cfg(not(feature = "std"))]
        return libm::logf(self);
    }

    #[inline(always)]
    fn floor(self) -> f32 {
        #[cfg(feature = "std")]
        return f32::floor(self);

        #[cfg(not(feature = "std"))]
        return libm::floorf(self);
    }

    #[inline(always)]
    fn is_sign_positive(self) -> bool {
        f32::is_sign_positive(self)
    }

    #[inline(always)]
    fn is_sign_negative(self) -> bool {
        f32::is_sign_negative(self)
    }
}

#[cfg(any(feature = "parse-floats", feature = "write-floats"))]
impl Float for f64 {
    type Unsigned = u64;
    float_literals!(f64);
    float_masks!(
        float => Self,
        sign_mask => 0x8000000000000000,
        exponent_mask => 0x7FF0000000000000,
        hidden_bit_mask => 0x0010000000000000,
        mantissa_mask => 0x000FFFFFFFFFFFFF,
    );
    const EXPONENT_SIZE: i32 = 11;
    const MANTISSA_SIZE: i32 = 52;
    const EXPONENT_BIAS: i32 = 1023 + Self::MANTISSA_SIZE;
    const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
    const MAX_EXPONENT: i32 = 0x7FF - Self::EXPONENT_BIAS;

    #[inline(always)]
    fn to_bits(self) -> u64 {
        f64::to_bits(self)
    }

    #[inline(always)]
    fn from_bits(u: u64) -> f64 {
        f64::from_bits(u)
    }

    #[inline(always)]
    fn ln(self) -> f64 {
        #[cfg(feature = "std")]
        return f64::ln(self);

        #[cfg(not(feature = "std"))]
        return libm::logd(self);
    }

    #[inline(always)]
    fn floor(self) -> f64 {
        #[cfg(feature = "std")]
        return f64::floor(self);

        #[cfg(not(feature = "std"))]
        return libm::floord(self);
    }

    #[inline(always)]
    fn is_sign_positive(self) -> bool {
        f64::is_sign_positive(self)
    }

    #[inline(always)]
    fn is_sign_negative(self) -> bool {
        f64::is_sign_negative(self)
    }
}

// #[cfg(feature = "f128")]
// impl Float for f128 {
//     type Unsigned = u128;
//     float_literals!(f128);
//     float_masks!(
//         float => Self,
//         sign_mask => 0x80000000000000000000000000000000,
//         exponent_mask => 0x7FFF0000000000000000000000000000,
//         hidden_bit_mask => 0x00010000000000000000000000000000,
//         mantissa_mask => 0x0000FFFFFFFFFFFFFFFFFFFFFFFFFFFF,
//     );
//     const EXPONENT_SIZE: i32 = 15;
//     const MANTISSA_SIZE: i32 = 112;
//     const EXPONENT_BIAS: i32 = 16383 + Self::MANTISSA_SIZE;
//     const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
//     const MAX_EXPONENT: i32 = 0x7FFF - Self::EXPONENT_BIAS;
// }