summaryrefslogtreecommitdiffstats
path: root/include/jau/byte_util.hpp
blob: 9359100496eabcc49ee0694a818377f037454c2e (plain)
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
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef JAU_BYTE_UTIL_HPP_
#define JAU_BYTE_UTIL_HPP_

#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <vector>
#include <type_traits>

#include <jau/cpp_lang_util.hpp>
#include <jau/cpp_pragma.hpp>
#include <jau/packed_attribute.hpp>

#include <jau/int_types.hpp>

namespace jau {

    /** @defgroup ByteUtils Byte Utilities
     * Byte utility functions and types for endian- and bit conversions,
     * inclusive alignment handling and general get & put functionality.
     *
     * This category is also supporting \ref Integer.
     *
     * All \ref endian API entries are of `constexpr` and hence evaluated at compile time.<br>
     * Therefore, if-branches and expressions are also of `constexpr` and optimized 'away' at compile time.<br>
     * This includes the `cpu_to_<endian>(..)` and `<endian>_to_cpu(..)` etc utility functions.
     *
     * See \ref endian enum class regarding endian `constexpr` compile time determination.
     *  @{
     */

    #if defined __has_builtin
        #if __has_builtin(__builtin_bswap16)
            #define __has_builtin_bswap16 1
        #endif
    #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
        #define __has_builtin_bswap16 1
    #endif
    #if defined __has_builtin
        #if __has_builtin(__builtin_bswap32)
            #define __has_builtin_bswap32 1
        #endif
    #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
        #define __has_builtin_bswap32 1
    #endif
    #if defined __has_builtin
        #if __has_builtin(__builtin_bswap64)
            #define __has_builtin_bswap64 1
        #endif
    #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
        #define __has_builtin_bswap64 1
    #endif

    // Remember: constexpr specifier used in a function or static data member (since C++17) declaration implies inline.

    constexpr uint16_t bswap(uint16_t const source) noexcept {
        #if defined __has_builtin_bswap16
            return __builtin_bswap16(source);
        #else
            return (uint16_t) ( ( ( (source) >> 8 ) & 0xff ) |
                                ( ( (source) & 0xff) << 8 ) );
        #endif
    }

    constexpr uint32_t bswap(uint32_t const source) noexcept {
        #if defined __has_builtin_bswap32
            return __builtin_bswap32(source);
        #else
            return ( ( source & 0xff000000U ) >> 24 ) |
                   ( ( source & 0x00ff0000U ) >>  8 ) |
                   ( ( source & 0x0000ff00U ) <<  8 ) |
                   ( ( source & 0x000000ffU ) << 24 );
        #endif
    }

    constexpr uint64_t bswap(uint64_t const & source) noexcept {
        #if defined __has_builtin_bswap64
            return __builtin_bswap64(source);
        #else
            return ( ( source & 0xff00000000000000ULL ) >> 56 ) |
                   ( ( source & 0x00ff000000000000ULL ) >> 40 ) |
                   ( ( source & 0x0000ff0000000000ULL ) >> 24 ) |
                   ( ( source & 0x000000ff00000000ULL ) >>  8 ) |
                   ( ( source & 0x00000000ff000000ULL ) <<  8 ) |
                   ( ( source & 0x0000000000ff0000ULL ) << 24 ) |
                   ( ( source & 0x000000000000ff00ULL ) << 40 ) |
                   ( ( source & 0x00000000000000ffULL ) << 56 );
        #endif
    }

    constexpr void bswap(uint8_t * sink, uint8_t const * source, nsize_t len) {
        source += len - 1;
        for (; len > 0; len--) {
            *sink++ = *source--;
        }
    }

    constexpr uint128_t bswap(uint128_t const & source) noexcept {
        uint128_t dest;
        bswap(dest.data, source.data, 16);
        return dest;
    }

    constexpr uint192_t bswap(uint192_t const & source) noexcept {
        uint192_t dest;
        bswap(dest.data, source.data, 24);
        return dest;
    }

    constexpr uint256_t bswap(uint256_t const & source) noexcept {
        uint256_t dest;
        bswap(dest.data, source.data, 32);
        return dest;
    }

    /**
    // *************************************************
    // *************************************************
    // *************************************************
     */

    // The pragma to stop multichar warning == error `-Werror=multichar` doesn't seem to work with GCC <= 10.2
    // Hence we have to disable this specific warning via: `-Wno-multichar`
    // until all our compiler support `__builtin_bit_cast(T, a)`

    PRAGMA_DISABLE_WARNING_PUSH
    PRAGMA_DISABLE_WARNING_MULTICHAR

    namespace impl {
        constexpr uint32_t get_host_order() noexcept {
            if constexpr ( jau::is_builtin_bit_cast_available() ) {
                constexpr uint8_t b[4] { 0x44, 0x43, 0x42, 0x41 }; // h->l: 41 42 43 44 = 'ABCD' hex ASCII code
                return jau::bit_cast<uint32_t, uint8_t[4]>( b );
            } else {
                return 'ABCD'; // h->l: 41 42 43 44 = 'ABCD' hex ASCII code
            }
        }
    }

    /**
     * Endian identifier, indicating endianess of all scalar types.
     *
     * Inspired by C++20 std::endian
     *
     * Corner case platforms currently not supported,
     * i.e. unified endianess and mixed endianess.
     *
     * All endian API entries are of `constexpr` and hence evaluated at compile time.<br>
     * Therefore, if-branches and expressions are also of `constexpr` and optimized 'away' at compile time.<br>
     * This includes the `cpu_to_<endian>(..)` and `<endian>_to_cpu(..)` etc utility functions.
     *
     * On i386 platforms the host byte order is Least Significant Byte first (LSB) or Little-Endian,
     * whereas the network byte order, as used on the Internet, is Most Significant Byte first (MSB) or Big-Endian.
     * See #include <arpa/inet.h>
     *
     * Bluetooth is LSB or Little-Endian!
     */
    enum class endian : uint32_t
    {
        /** Identifier for little endian. */
        little      = 0x41424344U, // h->l: 41 42 43 44 = 'ABCD' hex ASCII code

        /** Identifier for big endian. */
        big         = 0x44434241U, // h->l: 44 43 42 41 = 'DCBA' hex ASCII code

        /** Identifier for DEC PDP-11, aka `ENDIAN_LITTLE_WORD`. */
        pdp         = 0x43444142U, // h->l: 43 44 41 42 = 'CDAB' hex ASCII code

        /** Identifier for Honeywell 316, aka `ENDIAN_BIG_WORD`. */
        honeywell   = 0x42414443U, // h->l: 42 41 44 43 = 'BADC' hex ASCII code

        /** Undetermined endian */
        undefined   = 0x00000000U,

        /** Identifier for native platform type, one of the above. */
        native      = impl::get_host_order()
    };

    PRAGMA_DISABLE_WARNING_POP

    /**
     * Return std::string representation of the given \ref endian.
     * @param v the \ref endian value
     * @return the std::string representation
     */
    std::string to_string(const endian& v) noexcept;

    /**
     * Evaluates `true` if the given \ref endian is defined,
     * i.e. `little`, `big`, `pdp` or `honeywell`.
     */
    constexpr bool isDefinedEndian(const endian &v) noexcept {
        switch(v) {
            case endian::little:
                [[fallthrough]];
            case endian::big:
                [[fallthrough]];
            case endian::pdp:
                [[fallthrough]];
            case endian::honeywell:
                return true;
            default:
                return false;
        }
    }

    /**
     * Evaluates `true` if platform is running in little \ref endian mode,
     * i.e. `jau::endian::little == jau::endian::native`.
     */
    constexpr bool isLittleEndian() noexcept {
        return endian::little == endian::native;
    }

    /**
     * Evaluates `true` if platform is running in big \ref endian mode,
     * i.e. `jau::endian::big == jau::endian::native`.
     */
    constexpr bool isBigEndian() noexcept {
        return endian::big == endian::native;
    }

    /**
     * Evaluates `true` if platform is running in little or big \ref endian mode,
     * i.e. `jau::endian::little == jau::endian::native || jau::endian::big == jau::endian::native`.
     */
    constexpr bool isLittleOrBigEndian() noexcept {
        return jau::endian::little == jau::endian::native || jau::endian::big == jau::endian::native;
    }

    /**
     * A little-endian type trait for convenience ..
     * <p>
     * Since all \ref endian definitions are of `constexpr`, code can simply use expressions of these
     * for compile-time evaluation and optimization. A template `SFINAE` is not required.
     * </p>
     * @tparam Dummy_type just to make template `SFINAE` happy
     */
    template <typename Dummy_type> struct has_endian_little : std::integral_constant<bool, endian::little == endian::native> {};

    /**
     * Value access of little-endian type trait for convenience ..
     * <p>
     * Since all \ref endian definitions are of `constexpr`, code can simply use expressions of these
     * for compile-time evaluation and optimization. A template `SFINAE` is not required.
     * </p>
     * @tparam Dummy_type just to make template `SFINAE` happy
     */
    template <typename Dummy_type> constexpr bool has_endian_little_v = has_endian_little<Dummy_type>::value;

    /**
     * A big-endian type trait for convenience ..
     * <p>
     * Since all \ref endian definitions are of `constexpr`, code can simply use expressions of these
     * for compile-time evaluation and optimization. A template `SFINAE` is not required.
     * </p>
     * @tparam Dummy_type just to make template `SFINAE` happy
     */
    template <typename Dummy_type> struct has_endian_big : std::integral_constant<bool, endian::big == endian::native> {};

    /**
     * Value access of big-endian type trait for convenience ..
     * <p>
     * Since all \ref endian definitions are of `constexpr`, code can simply use expressions of these
     * for compile-time evaluation and optimization. A template `SFINAE` is not required.
     * </p>
     * @tparam Dummy_type just to make template `SFINAE` happy
     */
    template <typename Dummy_type> constexpr bool has_endian_big_v = has_endian_big<Dummy_type>::value;

    /**
    // *************************************************
    // *************************************************
    // *************************************************
     */

    constexpr uint16_t be_to_cpu(uint16_t const n) noexcept {
        static_assert(isLittleOrBigEndian()); // one static_assert is sufficient for whole compilation unit
        if constexpr ( isLittleEndian() ) {
            return bswap(n);
        } else if constexpr ( isBigEndian() ) {
            return n;
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint16_t cpu_to_be(uint16_t const h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(h);
        } else if constexpr ( isBigEndian() ) {
            return h;
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint16_t le_to_cpu(uint16_t const l) noexcept {
        if constexpr ( isLittleEndian() ) {
            return l;
        } else if constexpr ( isBigEndian() ) {
            return bswap(l);
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint16_t cpu_to_le(uint16_t const h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return h;
        } else if constexpr ( isBigEndian() ) {
            return bswap(h);
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }

    constexpr uint32_t be_to_cpu(uint32_t const n) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(n);
        } else if constexpr ( isBigEndian() ) {
            return n;
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint32_t cpu_to_be(uint32_t const h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(h);
        } else if constexpr ( isBigEndian() ) {
            return h;
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint32_t le_to_cpu(uint32_t const l) noexcept {
        if constexpr ( isLittleEndian() ) {
            return l;
        } else if constexpr ( isBigEndian() ) {
            return bswap(l);
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint32_t cpu_to_le(uint32_t const h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return h;
        } else if constexpr ( isBigEndian() ) {
            return bswap(h);
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }

    constexpr uint64_t be_to_cpu(uint64_t const & n) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(n);
        } else if constexpr ( isBigEndian() ) {
            return n;
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint64_t cpu_to_be(uint64_t const & h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(h);
        } else if constexpr ( isBigEndian() ) {
            return h;
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint64_t le_to_cpu(uint64_t const & l) noexcept {
        if constexpr ( isLittleEndian() ) {
            return l;
        } else if constexpr ( isBigEndian() ) {
            return bswap(l);
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }
    constexpr uint64_t cpu_to_le(uint64_t const & h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return h;
        } else if constexpr ( isBigEndian() ) {
            return bswap(h);
        } else {
            return 0; // unreachable -> static_assert(..) above
        }
    }

    constexpr uint128_t be_to_cpu(uint128_t const & n) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(n);
        } else if constexpr ( isBigEndian() ) {
            return n;
        } else {
            return uint128_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint128_t cpu_to_be(uint128_t const & h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(h);
        } else if constexpr ( isBigEndian() ) {
            return h;
        } else {
            return uint128_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint128_t le_to_cpu(uint128_t const & l) noexcept {
        if constexpr ( isLittleEndian() ) {
            return l;
        } else if constexpr ( isBigEndian() ) {
            return bswap(l);
        } else {
            return uint128_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint128_t cpu_to_le(uint128_t const & h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return h;
        } else if constexpr ( isBigEndian() ) {
            return bswap(h);
        } else {
            return uint128_t(); // unreachable -> static_assert(..) above
        }
    }

    constexpr uint192_t be_to_cpu(uint192_t const & n) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(n);
        } else if constexpr ( isBigEndian() ) {
            return n;
        } else {
            return uint192_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint192_t cpu_to_be(uint192_t const & h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(h);
        } else if constexpr ( isBigEndian() ) {
            return h;
        } else {
            return uint192_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint192_t le_to_cpu(uint192_t const & l) noexcept {
        if constexpr ( isLittleEndian() ) {
            return l;
        } else if constexpr ( isBigEndian() ) {
            return bswap(l);
        } else {
            return uint192_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint192_t cpu_to_le(uint192_t const & h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return h;
        } else if constexpr ( isBigEndian() ) {
            return bswap(h);
        } else {
            return uint192_t(); // unreachable -> static_assert(..) above
        }
    }

    constexpr uint256_t be_to_cpu(uint256_t const & n) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(n);
        } else if constexpr ( isBigEndian() ) {
            return n;
        } else {
            return uint256_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint256_t cpu_to_be(uint256_t const & h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return bswap(h);
        } else if constexpr ( isBigEndian() ) {
            return h;
        } else {
            return uint256_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint256_t le_to_cpu(uint256_t const & l) noexcept {
        if constexpr ( isLittleEndian() ) {
            return l;
        } else if constexpr ( isBigEndian() ) {
            return bswap(l);
        } else {
            return uint256_t(); // unreachable -> static_assert(..) above
        }
    }
    constexpr uint256_t cpu_to_le(uint256_t const & h) noexcept {
        if constexpr ( isLittleEndian() ) {
            return h;
        } else if constexpr ( isBigEndian() ) {
            return bswap(h);
        } else {
            return uint256_t(); // unreachable -> static_assert(..) above
        }

    }

    /**
    // *************************************************
    // *************************************************
    // *************************************************
     */

    constexpr void put_uint8(uint8_t * buffer, nsize_t const byte_offset, const uint8_t v) noexcept
    {
        *pointer_cast<uint8_t *>( buffer + byte_offset ) = v;
    }
    constexpr uint8_t get_uint8(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        return *pointer_cast<uint8_t const *>( buffer + byte_offset );
    }
    constexpr int8_t get_int8(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        return *pointer_cast<int8_t const *>( buffer + byte_offset );
    }

    template<typename T>
    constexpr T get_value(const packed_t<T>* source, const bool littleEndian) noexcept { return littleEndian ? le_to_cpu(source->store) : be_to_cpu(source->store); }

    constexpr void put_uint16(uint8_t * buffer, nsize_t const byte_offset, const uint16_t v) noexcept
    {
        /**
         * Handle potentially misaligned address of buffer + byte_offset, can't just
         *   uint16_t * p = (uint16_t *) ( buffer + byte_offset );
         *   *p = v;
         * Universal alternative using memcpy is costly:
         *   memcpy(buffer + byte_offset, &v, sizeof(v));
         * Use compiler magic 'struct __attribute__((__packed__))' access:
         */
        pointer_cast<packed_t<uint16_t>*>( buffer + byte_offset )->store = v;
    }
    constexpr void put_uint16(uint8_t * buffer, nsize_t const byte_offset, const uint16_t v, const bool littleEndian) noexcept
    {
        /**
         * Handle potentially misaligned address of buffer + byte_offset, can't just
         *   uint16_t * p = (uint16_t *) ( buffer + byte_offset );
         *   *p = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
         * Universal alternative using memcpy is costly:
         *   const uint16_t v2 = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
         *   memcpy(buffer + byte_offset, &v2, sizeof(v2));
         * Use compiler magic 'struct __attribute__((__packed__))' access:
         */
        pointer_cast<packed_t<uint16_t>*>( buffer + byte_offset )->store = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
    }
    constexpr uint16_t get_uint16(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        /**
         * Handle potentially misaligned address of buffer + byte_offset, can't just
         *   uint16_t const * p = (uint16_t const *) ( buffer + byte_offset );
         *   return *p;
         * Universal alternative using memcpy is costly:
         *   uint16_t v;
         *   memcpy(&v, buffer + byte_offset, sizeof(v));
         *   return v;
         * Use compiler magic 'struct __attribute__((__packed__))' access:
         */
        return pointer_cast<const packed_t<uint16_t>*>( buffer + byte_offset )->store;
    }
    constexpr uint16_t get_uint16(uint8_t const * buffer, nsize_t const byte_offset, const bool littleEndian) noexcept
    {
        /**
         * Handle potentially misaligned address of buffer + byte_offset, can't just
         *   uint16_t const * p = (uint16_t const *) ( buffer + byte_offset );
         *   return littleEndian ? le_to_cpu(*p) : be_to_cpu(*p);
         * Universal alternative using memcpy is costly:
         *   uint16_t v;
         *   memcpy(&v, buffer + byte_offset, sizeof(v));
         *   return littleEndian ? le_to_cpu(v) : be_to_cpu(v);
         * Use compiler magic 'struct __attribute__((__packed__))' access:
         */
        return get_value(pointer_cast<const packed_t<uint16_t>*>( buffer + byte_offset ), littleEndian);
    }

    constexpr void put_uint32(uint8_t * buffer, nsize_t const byte_offset, const uint32_t v) noexcept
    {
        pointer_cast<packed_t<uint32_t>*>( buffer + byte_offset )->store = v;
    }
    constexpr void put_uint32(uint8_t * buffer, nsize_t const byte_offset, const uint32_t v, const bool littleEndian) noexcept
    {
        pointer_cast<packed_t<uint32_t>*>( buffer + byte_offset )->store = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
    }
    constexpr uint32_t get_uint32(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        return pointer_cast<const packed_t<uint32_t>*>( buffer + byte_offset )->store;
    }
    constexpr uint32_t get_uint32(uint8_t const * buffer, nsize_t const byte_offset, const bool littleEndian) noexcept
    {
        return get_value(pointer_cast<const packed_t<uint32_t>*>( buffer + byte_offset ), littleEndian);
    }

    constexpr void put_uint64(uint8_t * buffer, nsize_t const byte_offset, const uint64_t & v) noexcept
    {
        pointer_cast<packed_t<uint64_t>*>( buffer + byte_offset )->store = v;
    }
    constexpr void put_uint64(uint8_t * buffer, nsize_t const byte_offset, const uint64_t & v, const bool littleEndian) noexcept
    {
        pointer_cast<packed_t<uint64_t>*>( buffer + byte_offset )->store = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
    }
    constexpr uint64_t get_uint64(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        return pointer_cast<const packed_t<uint64_t>*>( buffer + byte_offset )->store;
    }
    constexpr uint64_t get_uint64(uint8_t const * buffer, nsize_t const byte_offset, const bool littleEndian) noexcept
    {
        return get_value(pointer_cast<const packed_t<uint64_t>*>( buffer + byte_offset ), littleEndian);
    }

    constexpr void put_uint128(uint8_t * buffer, nsize_t const byte_offset, const uint128_t & v) noexcept
    {
        pointer_cast<packed_t<uint128_t>*>( buffer + byte_offset )->store = v;
    }
    constexpr void put_uint128(uint8_t * buffer, nsize_t const byte_offset, const uint128_t & v, const bool littleEndian) noexcept
    {
        pointer_cast<packed_t<uint128_t>*>( buffer + byte_offset )->store = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
    }
    constexpr uint128_t get_uint128(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        return pointer_cast<const packed_t<uint128_t>*>( buffer + byte_offset )->store;
    }
    constexpr uint128_t get_uint128(uint8_t const * buffer, nsize_t const byte_offset, const bool littleEndian) noexcept
    {
        return get_value(pointer_cast<const packed_t<uint128_t>*>( buffer + byte_offset ), littleEndian);
    }

    constexpr void put_uint192(uint8_t * buffer, nsize_t const byte_offset, const uint192_t & v) noexcept
    {
        pointer_cast<packed_t<uint192_t>*>( buffer + byte_offset )->store = v;
    }
    constexpr void put_uint192(uint8_t * buffer, nsize_t const byte_offset, const uint192_t & v, const bool littleEndian) noexcept
    {
        pointer_cast<packed_t<uint192_t>*>( buffer + byte_offset )->store = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
    }
    constexpr uint192_t get_uint192(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        return pointer_cast<const packed_t<uint192_t>*>( buffer + byte_offset )->store;
    }
    constexpr uint192_t get_uint192(uint8_t const * buffer, nsize_t const byte_offset, const bool littleEndian) noexcept
    {
        return get_value(pointer_cast<const packed_t<uint192_t>*>( buffer + byte_offset ), littleEndian);
    }

    constexpr void put_uint256(uint8_t * buffer, nsize_t const byte_offset, const uint256_t & v) noexcept
    {
        pointer_cast<packed_t<uint256_t>*>( buffer + byte_offset )->store = v;
    }
    constexpr void put_uint256(uint8_t * buffer, nsize_t const byte_offset, const uint256_t & v, const bool littleEndian) noexcept
    {
        pointer_cast<packed_t<uint256_t>*>( buffer + byte_offset )->store = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
    }
    constexpr uint256_t get_uint256(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        return pointer_cast<const packed_t<uint256_t>*>( buffer + byte_offset )->store;
    }
    constexpr uint256_t get_uint256(uint8_t const * buffer, nsize_t const byte_offset, const bool littleEndian) noexcept
    {
        return get_value(pointer_cast<const packed_t<uint256_t>*>( buffer + byte_offset ), littleEndian);
    }

    /**
    // *************************************************
    // *************************************************
    // *************************************************
     */

    template<typename T>
    constexpr
    typename std::enable_if_t<
        std::is_standard_layout_v<T>,
        void>
    put_value(uint8_t * buffer, nsize_t const byte_offset, const T& v) noexcept
    {
        // reinterpret_cast<packed_t<T>*>( buffer + byte_offset )->store = v;
        pointer_cast<packed_t<T>*>( buffer + byte_offset )->store = v;
    }

    template<typename T>
    constexpr
    typename std::enable_if_t<
        std::is_standard_layout_v<T>,
        void>
    put_value(uint8_t * buffer, nsize_t const byte_offset, const T& v, const bool littleEndian) noexcept
    {
        pointer_cast<packed_t<T>*>( buffer + byte_offset )->store = littleEndian ? cpu_to_le(v) : cpu_to_be(v);
    }

    template<typename T>
    constexpr
    typename std::enable_if_t<
        std::is_standard_layout_v<T>,
        T>
    get_value(uint8_t const * buffer, nsize_t const byte_offset) noexcept
    {
        return pointer_cast<const packed_t<T>*>( buffer + byte_offset )->store;
    }

    template<typename T>
    constexpr
    typename std::enable_if_t<
        std::is_standard_layout_v<T>,
        T>
    get_value(uint8_t const * buffer, nsize_t const byte_offset, const bool littleEndian) noexcept
    {
        return get_value(pointer_cast<const packed_t<T>*>( buffer + byte_offset ), littleEndian);
    }

    /**@}*/

} // namespace jau

/** \example test_basictypeconv.cpp
 * This C++ unit test validates the jau::bswap and get/set value implementation
 */

#endif /* JAU_BYTE_UTIL_HPP_ */