i24 2.3.3

A Rust library for working with 24-bit integers.
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
use bytemuck::{NoUninit, Zeroable};
use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
use num_traits::FromPrimitive;

#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum ZeroByte {
    Zero = 0,
}

const _: () =
    assert!(align_of::<u8>() == align_of::<ZeroByte>() && size_of::<u8>() == size_of::<ZeroByte>());

// Safety: ZeroByte is one single byte that is always initialized to zero
unsafe impl NoUninit for ZeroByte {}

// Safety: ZeroByte is one single byte that is always initialized to zero
// in fact the only valid bit pattern is zero
unsafe impl Zeroable for ZeroByte {}

#[derive(Debug, Copy, Clone)]
#[repr(C, align(4))]
pub struct BigEndianI24Repr {
    // most significant byte at the start
    pub(crate) most_significant_byte: ZeroByte,
    pub(crate) data: [u8; 3],
}

#[derive(Debug, Copy, Clone)]
#[repr(C, align(4))]
pub struct LittleEndianI24Repr {
    pub(crate) data: [u8; 3],
    // most significant byte at the end
    pub(crate) most_significant_byte: ZeroByte,
}

#[cfg(target_endian = "big")]
pub(super) type I24Repr = BigEndianI24Repr;

#[cfg(target_endian = "little")]
pub type I24Repr = LittleEndianI24Repr;

const _: () =
    assert!(align_of::<u32>() == align_of::<I24Repr>() && size_of::<u32>() == size_of::<I24Repr>());

// Safety: I24Repr is laid out in memory as a `u32` with the most significant byte set to zero
// Must be NoUninit due to the padding byte.
unsafe impl Zeroable for I24Repr {}

// Safety: I24 repr is laid out in memory as a `u32` with the most significant byte set to zero.
// Must be NoUninit due to the padding byte.
unsafe impl NoUninit for I24Repr {}

#[cfg(any(
    all(target_endian = "little", target_endian = "big"),
    not(any(target_endian = "little", target_endian = "big"))
))]
compile_error!("unknown endianness");

impl FromPrimitive for I24Repr {
    #[inline]
    fn from_i64(n: i64) -> Option<Self> {
        Self::try_from_i64(n)
    }

    #[inline]
    fn from_u64(n: u64) -> Option<Self> {
        Self::try_from_u64(n)
    }

    #[inline]
    fn from_i32(n: i32) -> Option<Self> {
        Self::try_from_i32(n)
    }

    #[inline]
    fn from_u32(n: u32) -> Option<Self> {
        Self::try_from_u32(n)
    }
}

impl I24Repr {
    pub(super) const MAX: i32 = (1 << 23) - 1;
    pub(super) const MIN: i32 = -(1 << 23);
    pub(super) const ZERO: i32 = 0;
    pub(super) const BITS_MASK: u32 = 0xFFFFFF;

    #[inline]
    pub const fn to_i32(self) -> i32 {
        ((self.to_bits() as i32) << 8) >> 8
    }

    #[inline]
    pub const fn wrapping_from_i32(value: i32) -> Self {
        let proper_i24 = value as u32 & Self::BITS_MASK;

        // Safety: we only use the first 24 least significant bits (i.e 3 bytes) of the value,
        // and the most significant byte is set to zero
        // therefore layout guarantees hold true
        unsafe { Self::from_bits(proper_i24) }
    }

    #[inline]
    pub const fn saturating_from_i32(value: i32) -> Self {
        // Safety: we only use the first 24 least significant bits (i.e 3 bytes) of the value,
        // and the most significant byte is set to zero
        // therefore layout guarantees hold true
        if value > Self::MAX {
            const { Self::wrapping_from_i32(Self::MAX) }
        } else if value < Self::MIN {
            const { Self::wrapping_from_i32(Self::MIN) }
        } else {
            Self::wrapping_from_i32(value)
        }
    }

    #[inline]
    pub const fn from_ne_bytes(bytes: [u8; 3]) -> Self {
        Self {
            data: bytes,
            most_significant_byte: ZeroByte::Zero,
        }
    }

    #[inline]
    pub const fn from_be_bytes(bytes: [u8; 3]) -> Self {
        Self::from_ne_bytes(bytes).to_be()
    }

    #[inline]
    pub const fn from_le_bytes(bytes: [u8; 3]) -> Self {
        Self::from_ne_bytes(bytes).to_le()
    }

    pub const fn swap_bytes(self) -> Self {
        // can't just make a `swap_bytes` without endianness checks
        // because it also swaps their `repr`, and so this is easier to do
        #[cfg(target_endian = "little")]
        {
            self.to_be()
        }
        #[cfg(target_endian = "big")]
        {
            self.to_le()
        }
    }

    #[inline]
    pub const fn to_be(self) -> Self {
        #[cfg(target_endian = "big")]
        {
            self
        }

        #[cfg(target_endian = "little")]
        {
            Self::from_ne_bytes(self.to_be_repr().data)
        }
    }

    #[inline]
    pub const fn to_le(self) -> Self {
        #[cfg(target_endian = "little")]
        {
            self
        }

        #[cfg(target_endian = "big")]
        {
            Self::from_ne_bytes(self.to_le_repr().data)
        }
    }

    #[inline]
    pub const fn to_ne_bytes(self) -> [u8; 3] {
        self.data
    }

    #[inline]
    pub const fn to_be_bytes(self) -> [u8; 3] {
        self.to_be_repr().data
    }

    #[inline]
    pub const fn to_le_bytes(self) -> [u8; 3] {
        self.to_le_repr().data
    }

    #[inline]
    const fn to_be_repr(self) -> BigEndianI24Repr {
        #[cfg(target_endian = "big")]
        {
            self
        }

        #[cfg(target_endian = "little")]
        {
            let val = self.to_bits().swap_bytes();

            // Safety:
            // since this is little endian, the bytes started off being laid out as
            // [data1, data2, data3, zero]
            // so after swapping the bytes it turns into
            // [zero, data3, data2, data1]
            // which is the proper layout for `BigEndianI24Repr`
            unsafe { core::mem::transmute::<u32, BigEndianI24Repr>(val) }
        }
    }

    #[inline]
    const fn to_le_repr(self) -> Self {
        #[cfg(target_endian = "little")]
        {
            self
        }

        #[cfg(target_endian = "big")]
        {
            let val = self.to_bits().swap_bytes();

            // Safety:
            // since this is big endian, the bytes started off being laid out as
            // [zero, data3, data2, data1]
            // so after swapping the bytes it turns into
            // [data1, data2, data3, zero]
            // which is the proper layout for `LittleEndianI24Repr`
            unsafe { std::mem::transmute::<u32, LittleEndianI24Repr>(val) }.data
        }
    }

    #[inline]
    pub(super) const fn to_bits(self) -> u32 {
        // Safety: I24Repr has the same memory layout as a `u32`
        unsafe { core::mem::transmute::<Self, u32>(self) }
    }

    /// Safety: the most significant byte has to equal 0
    #[inline]
    pub(super) const unsafe fn from_bits(bits: u32) -> Self {
        debug_assert!((bits & Self::BITS_MASK) == bits);
        // Safety: upheld by caller
        unsafe { core::mem::transmute::<u32, Self>(bits) }
    }

    #[inline]
    pub(super) const fn as_bits(&self) -> &u32 {
        // Safety: I24Repr has the same memory layout and alignment as a `u32`
        unsafe { core::mem::transmute::<&Self, &u32>(self) }
    }

    /// this returns a slice of u32's with the most significant byte set to zero
    #[inline]
    const fn slice_as_bits(slice: &[Self]) -> &[u32] {
        // Safety: I24Repr has the same memory layout and alignment as a `u32`
        unsafe { core::mem::transmute::<&[Self], &[u32]>(slice) }
    }

    #[inline]
    const fn const_eq(&self, other: &Self) -> bool {
        (*self.as_bits()) == (*other.as_bits())
    }
}

macro_rules! impl_infallible_unsigned {
    ($target_type:ty, $($meth: ident: $ty:ty),+) => {$(
        impl $target_type {
            #[inline]
            pub const fn $meth(x: $ty) -> Self {
                const {
                    assert!(<$ty>::MIN == 0 && <$ty>::BITS < 24);
                }

                // checked by the assert above
                unsafe { Self::from_bits(x as u32) }
            }
        }
    )+};
}

trait BoolLimits {
    const MIN: u8 = 0;
    const BITS: u32 = 1;
}

impl BoolLimits for bool {}

impl_infallible_unsigned! {
    I24Repr,
    from_u8: u8,
    from_u16: u16,
    from_bool: bool
}

macro_rules! impl_infallible_signed {
    ($target_type:ty, $($meth: ident: $ty:ty),+) => {$(
        impl $target_type {
            #[inline]
            pub const fn $meth(x: $ty) -> Self {
                const {
                    assert!(<$ty>::MIN < 0 && <$ty>::BITS < 24);
                }

                // at least on x86 (and probably all arches with sign extention)
                // this seems like the implementation with the best code gen
                // https://rust.godbolt.org/z/eThE5n9s1 -> from_i16_3

                // `x as u32` sign extends in accord to the refrence (https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions)
                // if positive this would be just the exact same number
                // if negative the sign extention is done for us and all we have to do
                // is zero out the high byte
                unsafe { Self::from_bits(x as u32 & Self::BITS_MASK) }
            }
        }
    )+};
}

impl_infallible_signed! {
    I24Repr,
    from_i8: i8,
    from_i16: i16
}

macro_rules! impl_fallible_unsigned {
    ($target_type:ty, $($meth: ident: $ty:ty),+) => {$(
        impl $target_type {
            #[inline]
            pub const fn $meth(x: $ty) -> Option<Self> {
                const { assert!(<$ty>::MIN == 0 && <$ty>::BITS > 24) }

                // the 2 impls have equivlent codegen
                // https://rust.godbolt.org/z/nE7nzGKPT
                if x > const { Self::MAX as $ty } {
                    return None
                }

                // Safety: x is <= Self::MAX meaning the msb is 0
                Some(unsafe { Self::from_bits(x as u32) })
            }
        }
    )+};
}

impl_fallible_unsigned! {
    I24Repr,
    try_from_u32: u32,
    try_from_u64: u64,
    try_from_u128: u128
}

macro_rules! impl_fallible_signed {
    ($target_type:ty, $($meth: ident: $ty:ty),+) => {$(
        impl $target_type {
            #[inline]
            pub const fn $meth(x: $ty) -> Option<Self> {
                const { assert!(<$ty>::MIN < 0 && <$ty>::BITS > 24) }

                if x < const { Self::MIN as $ty } || x > const { Self::MAX as $ty } {
                    return None
                }


                // this cast already sign extends as the source is signed
                // so we get a valid twos compliment number

                // Safety: we zero off the msb
                Some(unsafe { Self::from_bits(x as u32 & Self::BITS_MASK) })
            }
        }
    )+};
}

impl_fallible_signed! {
    I24Repr,
    try_from_i32: i32,
    try_from_i64: i64,
    try_from_i128: i128
}

impl PartialOrd<Self> for I24Repr {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for I24Repr {
    fn cmp(&self, other: &Self) -> Ordering {
        <i32 as Ord>::cmp(&self.to_i32(), &other.to_i32())
    }
}

impl PartialEq<Self> for I24Repr {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        Self::const_eq(self, other)
    }
}

impl Eq for I24Repr {}

impl Hash for I24Repr {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        u32::hash(self.as_bits(), state);
    }

    #[inline]
    fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
    where
        Self: Sized,
    {
        u32::hash_slice(Self::slice_as_bits(data), state);
    }
}

impl Default for I24Repr {
    fn default() -> Self {
        Self::zeroed()
    }
}

const _: () = {
    macro_rules! unwrap {
        ($e: expr) => {
            match $e {
                Some(x) => x,
                None => panic!("`unwrap` failed"),
            }
        };
    }

    // test arbitrary numbers
    assert!(I24Repr::const_eq(
        &unwrap!(I24Repr::try_from_i32(
            unwrap!(I24Repr::try_from_i32(349)).to_i32() - 1897
        )),
        &unwrap!(I24Repr::try_from_i32(349 - 1897))
    ));

    // test MIN
    assert!(unwrap!(I24Repr::try_from_i32(I24Repr::MIN)).to_i32() == I24Repr::MIN);

    // test MIN
    assert!(unwrap!(I24Repr::try_from_i32(I24Repr::MAX)).to_i32() == I24Repr::MAX);
};

#[derive(Debug, Copy, Clone)]
#[repr(C, align(4))]
pub struct BigEndianU24Repr {
    // most significant byte at the start
    pub(crate) most_significant_byte: ZeroByte,
    pub(crate) data: [u8; 3],
}

#[derive(Debug, Copy, Clone)]
#[repr(C, align(4))]
pub struct LittleEndianU24Repr {
    pub(crate) data: [u8; 3],
    // most significant byte at the end
    pub(crate) most_significant_byte: ZeroByte,
}

#[cfg(target_endian = "big")]
pub(super) type U24Repr = BigEndianU24Repr;

#[cfg(target_endian = "little")]
pub type U24Repr = LittleEndianU24Repr;

const _: () =
    assert!(align_of::<u32>() == align_of::<U24Repr>() && size_of::<u32>() == size_of::<U24Repr>());

// Safety: U24Repr is laid out in memory as a `u32` with the most significant byte set to zero
// Must be NoUninit due to the padding byte.
unsafe impl Zeroable for U24Repr {}

// Safety: U24 repr is laid out in memory as a `u32` with the most significant byte set to zero.
// Must be NoUninit due to the padding byte.
unsafe impl NoUninit for U24Repr {}

impl FromPrimitive for U24Repr {
    #[inline]
    fn from_i64(n: i64) -> Option<Self> {
        Self::try_from_i64(n)
    }

    #[inline]
    fn from_u64(n: u64) -> Option<Self> {
        Self::try_from_u64(n)
    }

    #[inline]
    fn from_i32(n: i32) -> Option<Self> {
        Self::try_from_i32(n)
    }

    #[inline]
    fn from_u32(n: u32) -> Option<Self> {
        Self::try_from_u32(n)
    }
}

impl U24Repr {
    pub(super) const MAX: u32 = (1 << 24) - 1;
    pub(super) const MIN: u32 = 0;
    pub(super) const ZERO: u32 = 0;
    pub(super) const BITS_MASK: u32 = 0xFFFFFF;

    #[inline]
    pub const fn to_u32(self) -> u32 {
        self.to_bits()
    }

    #[inline]
    pub const fn wrapping_from_u32(value: u32) -> Self {
        let proper_u24 = value & Self::BITS_MASK;

        // Safety: we only use the first 24 least significant bits (i.e 3 bytes) of the value,
        // and the most significant byte is set to zero
        // therefore layout guarantees hold true
        unsafe { Self::from_bits(proper_u24) }
    }

    #[inline]
    pub const fn saturating_from_u32(value: u32) -> Self {
        if value > Self::MAX {
            Self::wrapping_from_u32(Self::MAX)
        } else {
            Self::wrapping_from_u32(value)
        }
    }

    /// Safety: see `I24Repr::from_bits`
    #[inline]
    pub(super) const unsafe fn from_bits(bits: u32) -> Self {
        debug_assert!((bits & Self::BITS_MASK) == bits);
        // Safety: upheld by caller
        unsafe { core::mem::transmute::<u32, Self>(bits) }
    }

    #[inline]
    pub(super) const fn to_bits(self) -> u32 {
        // Safety: U24Repr has the same memory layout as a `u32`
        unsafe { core::mem::transmute::<Self, u32>(self) }
    }

    #[inline]
    pub(super) const fn as_bits(&self) -> &u32 {
        // Safety: U24Repr has the same memory layout and alignment as a `u32`
        unsafe { core::mem::transmute::<&Self, &u32>(self) }
    }

    #[inline]
    pub const fn from_ne_bytes(bytes: [u8; 3]) -> Self {
        Self {
            data: bytes,
            most_significant_byte: ZeroByte::Zero,
        }
    }

    #[inline]
    pub const fn from_be_bytes(bytes: [u8; 3]) -> Self {
        Self::from_ne_bytes(bytes).to_be()
    }

    #[inline]
    pub const fn from_le_bytes(bytes: [u8; 3]) -> Self {
        Self::from_ne_bytes(bytes).to_le()
    }

    pub const fn swap_bytes(self) -> Self {
        #[cfg(target_endian = "little")]
        {
            self.to_be()
        }
        #[cfg(target_endian = "big")]
        {
            self.to_le()
        }
    }

    #[inline]
    pub const fn to_be(self) -> Self {
        #[cfg(target_endian = "big")]
        {
            self
        }

        #[cfg(target_endian = "little")]
        {
            Self::from_ne_bytes(self.to_be_repr().data)
        }
    }

    #[inline]
    pub const fn to_le(self) -> Self {
        #[cfg(target_endian = "little")]
        {
            self
        }

        #[cfg(target_endian = "big")]
        {
            Self::from_ne_bytes(self.to_le_repr().data)
        }
    }

    #[inline]
    pub const fn to_ne_bytes(self) -> [u8; 3] {
        self.data
    }

    #[inline]
    pub const fn to_be_bytes(self) -> [u8; 3] {
        self.to_be_repr().data
    }

    #[inline]
    pub const fn to_le_bytes(self) -> [u8; 3] {
        self.to_le_repr().data
    }

    #[inline]
    const fn to_be_repr(self) -> BigEndianU24Repr {
        #[cfg(target_endian = "big")]
        {
            self
        }

        #[cfg(target_endian = "little")]
        {
            let val = self.to_bits().swap_bytes();

            // Safety: byte swapping creates proper big-endian layout
            unsafe { core::mem::transmute::<u32, BigEndianU24Repr>(val) }
        }
    }

    #[inline]
    const fn to_le_repr(self) -> Self {
        #[cfg(target_endian = "little")]
        {
            self
        }

        #[cfg(target_endian = "big")]
        {
            let val = self.to_bits().swap_bytes();

            // Safety: byte swapping creates proper little-endian layout
            unsafe { core::mem::transmute::<u32, LittleEndianU24Repr>(val) }
        }
    }

    #[inline]
    const fn const_eq(&self, other: &Self) -> bool {
        (*self.as_bits()) == (*other.as_bits())
    }
}

impl U24Repr {
    #[inline]
    pub(crate) const fn try_from_i32(x: i32) -> Option<Self> {
        if x < 0 || x as u32 > Self::MAX {
            return None;
        }
        Some(unsafe { Self::from_bits(x as u32) })
    }

    #[inline]
    pub(crate) const fn try_from_i64(x: i64) -> Option<Self> {
        if x < 0 || x as u64 > Self::MAX as u64 {
            return None;
        }
        Some(unsafe { Self::from_bits(x as u32) })
    }

    #[inline]
    pub(crate) const fn try_from_i128(x: i128) -> Option<Self> {
        if x < 0 || x as u128 > Self::MAX as u128 {
            return None;
        }
        Some(unsafe { Self::from_bits(x as u32) })
    }
}

impl PartialOrd<Self> for U24Repr {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for U24Repr {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        <u32 as Ord>::cmp(&self.to_u32(), &other.to_u32())
    }
}

impl PartialEq<Self> for U24Repr {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        Self::const_eq(self, other)
    }
}

impl Eq for U24Repr {}

impl Hash for U24Repr {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        u32::hash(self.as_bits(), state);
    }

    #[inline]
    fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
    where
        Self: Sized,
    {
        for item in data {
            u32::hash(item.as_bits(), state);
        }
    }
}

impl Default for U24Repr {
    fn default() -> Self {
        Self::zeroed()
    }
}

impl_fallible_unsigned!(
    U24Repr,
    try_from_u32: u32,
    try_from_u64: u64,
    try_from_u128: u128
);

impl_infallible_unsigned!(U24Repr, from_u8: u8, from_u16: u16, from_bool: bool);

const _: () = {
    // ZeroByte layout checks
    assert!(size_of::<ZeroByte>() == 1, "ZeroByte should be 1 byte");
    assert!(
        align_of::<ZeroByte>() == 1,
        "ZeroByte should have alignment 1"
    );

    // BigEndianI24Repr layout checks
    assert!(
        size_of::<BigEndianI24Repr>() == 4,
        "BigEndianI24Repr should be 4 bytes"
    );
    assert!(
        align_of::<BigEndianI24Repr>() == 4,
        "BigEndianI24Repr should be aligned to 4"
    );

    // LittleEndianI24Repr layout checks
    assert!(
        size_of::<LittleEndianI24Repr>() == 4,
        "LittleEndianI24Repr should be 4 bytes"
    );
    assert!(
        align_of::<LittleEndianI24Repr>() == 4,
        "LittleEndianI24Repr should be aligned to 4"
    );

    // I24Repr layout check (resolved depending on target endianness)
    assert!(size_of::<I24Repr>() == 4, "I24Repr should be 4 bytes");
    assert!(align_of::<I24Repr>() == 4, "I24Repr should be aligned to 4");

    // BigEndianU24Repr layout checks
    assert!(
        size_of::<BigEndianU24Repr>() == 4,
        "BigEndianU24Repr should be 4 bytes"
    );
    assert!(
        align_of::<BigEndianU24Repr>() == 4,
        "BigEndianU24Repr should be aligned to 4"
    );

    // LittleEndianU24Repr layout checks
    assert!(
        size_of::<LittleEndianU24Repr>() == 4,
        "LittleEndianU24Repr should be 4 bytes"
    );
    assert!(
        align_of::<LittleEndianU24Repr>() == 4,
        "LittleEndianU24Repr should be aligned to 4"
    );

    // U24Repr layout check (resolved depending on target endianness)
    assert!(size_of::<U24Repr>() == 4, "U24Repr should be 4 bytes");
    assert!(align_of::<U24Repr>() == 4, "U24Repr should be aligned to 4");
};