Lely core libraries 1.9.2
endian.h
Go to the documentation of this file.
1
22#ifndef LELY_UTIL_ENDIAN_H_
23#define LELY_UTIL_ENDIAN_H_
24
25#include <lely/util/bits.h>
26#include <lely/util/float.h>
27
28#include <string.h>
29
30#ifndef LELY_UTIL_ENDIAN_INLINE
31#define LELY_UTIL_ENDIAN_INLINE static inline
32#endif
33
34#ifndef LELY_BIG_ENDIAN
35#if defined(__BIG_ENDIAN__) || defined(__big_endian__) \
36 || (__GNUC__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
37 || defined(__ARMEB__) || defined(__AARCH64EB__) \
38 || defined(__THUMBEB__)
40#define LELY_BIG_ENDIAN 1
41#endif
42#endif
43
44#if LELY_BIG_ENDIAN
45#undef LELY_LITTLE_ENDIAN
46#endif
47
48#ifndef LELY_LITTLE_ENDIAN
49#if defined(__LITTLE_ENDIAN__) || defined(__little_endian__) \
50 || (__GNUC__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
51 || defined(__i386__) || defined(_M_IX86) \
52 || defined(__x86_64__) || defined(_M_AMD64) \
53 || defined(__ARMEL__) || defined(__AARCH64EL__) \
54 || defined(__THUMBEL__)
56#define LELY_LITTLE_ENDIAN 1
57#endif
58#endif
59
60#if LELY_LITTLE_ENDIAN
61#undef LELY_BIG_ENDIAN
62#endif
63
64#if !LELY_BIG_ENDIAN && !LELY_LITTLE_ENDIAN
65#error Unable to determine byte order or byte order is not supported.
66#endif
67
68#ifdef __cplusplus
69extern "C" {
70#endif
71
72#ifndef htobe16
74LELY_UTIL_ENDIAN_INLINE uint_least16_t htobe16(uint_least16_t x);
75#endif
76
77#ifndef betoh16
79LELY_UTIL_ENDIAN_INLINE uint_least16_t betoh16(uint_least16_t x);
80#endif
81
82#ifndef htole16
84LELY_UTIL_ENDIAN_INLINE uint_least16_t htole16(uint_least16_t x);
85#endif
86
87#ifndef letoh16
89LELY_UTIL_ENDIAN_INLINE uint_least16_t letoh16(uint_least16_t x);
90#endif
91
92#ifndef htobe32
94LELY_UTIL_ENDIAN_INLINE uint_least32_t htobe32(uint_least32_t x);
95#endif
96
97#ifndef betoh32
99LELY_UTIL_ENDIAN_INLINE uint_least32_t betoh32(uint_least32_t x);
100#endif
101
102#ifndef htole32
104LELY_UTIL_ENDIAN_INLINE uint_least32_t htole32(uint_least32_t x);
105#endif
106
107#ifndef letoh32
109LELY_UTIL_ENDIAN_INLINE uint_least32_t letoh32(uint_least32_t x);
110#endif
111
112#ifndef htobe64
114LELY_UTIL_ENDIAN_INLINE uint_least64_t htobe64(uint_least64_t x);
115#endif
116
117#ifndef betoh64
119LELY_UTIL_ENDIAN_INLINE uint_least64_t betoh64(uint_least64_t x);
120#endif
121
122#ifndef htole64
124LELY_UTIL_ENDIAN_INLINE uint_least64_t htole64(uint_least64_t x);
125#endif
126
127#ifndef letoh64
129LELY_UTIL_ENDIAN_INLINE uint_least64_t letoh64(uint_least64_t x);
130#endif
131
133LELY_UTIL_ENDIAN_INLINE void stbe_i16(void *ptr, int_least16_t x);
134
136LELY_UTIL_ENDIAN_INLINE int_least16_t ldbe_i16(const void *ptr);
137
139LELY_UTIL_ENDIAN_INLINE void stbe_u16(void *ptr, uint_least16_t x);
140
142LELY_UTIL_ENDIAN_INLINE uint_least16_t ldbe_u16(const void *ptr);
143
145LELY_UTIL_ENDIAN_INLINE void stle_i16(void *ptr, int_least16_t x);
146
148LELY_UTIL_ENDIAN_INLINE int_least16_t ldle_i16(const void *ptr);
149
151LELY_UTIL_ENDIAN_INLINE void stle_u16(void *ptr, uint_least16_t x);
152
154LELY_UTIL_ENDIAN_INLINE uint_least16_t ldle_u16(const void *ptr);
155
157LELY_UTIL_ENDIAN_INLINE void stbe_i32(void *ptr, int_least32_t x);
158
160LELY_UTIL_ENDIAN_INLINE int_least32_t ldbe_i32(const void *ptr);
161
163LELY_UTIL_ENDIAN_INLINE void stbe_u32(void *ptr, uint_least32_t x);
164
166LELY_UTIL_ENDIAN_INLINE uint_least32_t ldbe_u32(const void *ptr);
167
169LELY_UTIL_ENDIAN_INLINE void stle_i32(void *ptr, int_least32_t x);
170
172LELY_UTIL_ENDIAN_INLINE int_least32_t ldle_i32(const void *ptr);
173
175LELY_UTIL_ENDIAN_INLINE void stle_u32(void *ptr, uint_least32_t x);
176
178LELY_UTIL_ENDIAN_INLINE uint_least32_t ldle_u32(const void *ptr);
179
181LELY_UTIL_ENDIAN_INLINE void stbe_i64(void *ptr, int_least64_t x);
182
184LELY_UTIL_ENDIAN_INLINE int_least64_t ldbe_i64(const void *ptr);
185
187LELY_UTIL_ENDIAN_INLINE void stbe_u64(void *ptr, uint_least64_t x);
188
190LELY_UTIL_ENDIAN_INLINE uint_least64_t ldbe_u64(const void *ptr);
191
193LELY_UTIL_ENDIAN_INLINE void stle_i64(void *ptr, int_least64_t x);
194
196LELY_UTIL_ENDIAN_INLINE int_least64_t ldle_i64(const void *ptr);
197
199LELY_UTIL_ENDIAN_INLINE void stle_u64(void *ptr, uint_least64_t x);
200
202LELY_UTIL_ENDIAN_INLINE uint_least64_t ldle_u64(const void *ptr);
203
204#ifdef LELY_FLT16_TYPE
205
210LELY_UTIL_ENDIAN_INLINE void stbe_flt16(void *ptr, flt16_t x);
211
216LELY_UTIL_ENDIAN_INLINE flt16_t ldbe_flt16(const void *ptr);
217
222LELY_UTIL_ENDIAN_INLINE void stle_flt16(void *ptr, flt16_t x);
223
228LELY_UTIL_ENDIAN_INLINE flt16_t ldle_flt16(const void *ptr);
229
230#endif // LELY_FLT16_TYPE
231
232#ifdef LELY_FLT32_TYPE
233
238LELY_UTIL_ENDIAN_INLINE void stbe_flt32(void *ptr, flt32_t x);
239
244LELY_UTIL_ENDIAN_INLINE flt32_t ldbe_flt32(const void *ptr);
245
250LELY_UTIL_ENDIAN_INLINE void stle_flt32(void *ptr, flt32_t x);
251
256LELY_UTIL_ENDIAN_INLINE flt32_t ldle_flt32(const void *ptr);
257
258#endif // LELY_FLT32_TYPE
259
260#ifdef LELY_FLT64_TYPE
261
266LELY_UTIL_ENDIAN_INLINE void stbe_flt64(void *ptr, flt64_t x);
267
272LELY_UTIL_ENDIAN_INLINE flt64_t ldbe_flt64(const void *ptr);
273
278LELY_UTIL_ENDIAN_INLINE void stle_flt64(void *ptr, flt64_t x);
279
284LELY_UTIL_ENDIAN_INLINE flt64_t ldle_flt64(const void *ptr);
285
286#endif // LELY_FLT64_TYPE
287
299void bcpybe(void *dst, int dstbit, const void *src, int srcbit, size_t n);
300
312void bcpyle(void *dst, int dstbit, const void *src, int srcbit, size_t n);
313
314#ifndef htobe16
315inline uint_least16_t
316htobe16(uint_least16_t x)
317{
318 x &= UINT16_C(0xffff);
319#if LELY_BIG_ENDIAN
320 return x;
321#elif LELY_LITTLE_ENDIAN
322 return bswap16(x);
323#endif
324}
325#endif
326
327#ifndef betoh16
328inline uint_least16_t
329betoh16(uint_least16_t x)
330{
331 return htobe16(x);
332}
333#endif
334
335#ifndef htole16
336inline uint_least16_t
337htole16(uint_least16_t x)
338{
339 x &= UINT16_C(0xffff);
340#if LELY_BIG_ENDIAN
341 return bswap16(x);
342#elif LELY_LITTLE_ENDIAN
343 return x;
344#endif
345}
346#endif
347
348#ifndef letoh16
349inline uint_least16_t
350letoh16(uint_least16_t x)
351{
352 return htole16(x);
353}
354#endif
355
356#ifndef htobe32
357inline uint_least32_t
358htobe32(uint_least32_t x)
359{
360 x &= UINT32_C(0xffffffff);
361#if LELY_BIG_ENDIAN
362 return x;
363#elif LELY_LITTLE_ENDIAN
364 return bswap32(x);
365#endif
366}
367#endif
368
369#ifndef betoh32
370inline uint_least32_t
371betoh32(uint_least32_t x)
372{
373 return htobe32(x);
374}
375#endif
376
377#ifndef htole32
378inline uint_least32_t
379htole32(uint_least32_t x)
380{
381 x &= UINT32_C(0xffffffff);
382#if LELY_BIG_ENDIAN
383 return bswap32(x);
384#elif LELY_LITTLE_ENDIAN
385 return x;
386#endif
387}
388#endif
389
390#ifndef letoh32
391inline uint_least32_t
392letoh32(uint_least32_t x)
393{
394 return htole32(x);
395}
396#endif
397
398#ifndef htobe64
399inline uint_least64_t
400htobe64(uint_least64_t x)
401{
402 x &= UINT64_C(0xffffffffffffffff);
403#if LELY_BIG_ENDIAN
404 return x;
405#elif LELY_LITTLE_ENDIAN
406 return bswap64(x);
407#endif
408}
409#endif
410
411#ifndef betoh64
412inline uint_least64_t
413betoh64(uint_least64_t x)
414{
415 return htobe64(x);
416}
417#endif
418
419#ifndef htole64
420inline uint_least64_t
421htole64(uint_least64_t x)
422{
423 x &= UINT64_C(0xffffffffffffffff);
424#if LELY_BIG_ENDIAN
425 return bswap64(x);
426#elif LELY_LITTLE_ENDIAN
427 return x;
428#endif
429}
430#endif
431
432#ifndef letoh64
433inline uint_least64_t
434letoh64(uint_least64_t x)
435{
436 return htole64(x);
437}
438#endif
439
440inline void
441stbe_i16(void *ptr, int_least16_t x)
442{
443 stbe_u16(ptr, x);
444}
445
446inline int_least16_t
447ldbe_i16(const void *ptr)
448{
449 return ldbe_u16(ptr);
450}
451
452inline void
453stbe_u16(void *ptr, uint_least16_t x)
454{
455 x = htobe16(x);
456 memcpy(ptr, &x, sizeof(x));
457}
458
459inline uint_least16_t
460ldbe_u16(const void *ptr)
461{
462 uint_least16_t x = 0;
463 memcpy(&x, ptr, sizeof(x));
464 return betoh16(x);
465}
466
467inline void
468stle_i16(void *ptr, int_least16_t x)
469{
470 stle_u16(ptr, x);
471}
472
473inline int_least16_t
474ldle_i16(const void *ptr)
475{
476 return ldle_u16(ptr);
477}
478
479inline void
480stle_u16(void *ptr, uint_least16_t x)
481{
482 x = htole16(x);
483 memcpy(ptr, &x, sizeof(x));
484}
485
486inline uint_least16_t
487ldle_u16(const void *ptr)
488{
489 uint_least16_t x = 0;
490 memcpy(&x, ptr, sizeof(x));
491 return letoh16(x);
492}
493
494inline void
495stbe_i32(void *ptr, int_least32_t x)
496{
497 stbe_u32(ptr, x);
498}
499
500inline int_least32_t
501ldbe_i32(const void *ptr)
502{
503 return ldbe_u32(ptr);
504}
505
506inline void
507stbe_u32(void *ptr, uint_least32_t x)
508{
509 x = htobe32(x);
510 memcpy(ptr, &x, sizeof(x));
511}
512
513inline uint_least32_t
514ldbe_u32(const void *ptr)
515{
516 uint_least32_t x = 0;
517 memcpy(&x, ptr, sizeof(x));
518 return betoh32(x);
519}
520
521inline void
522stle_i32(void *ptr, int_least32_t x)
523{
524 stle_u32(ptr, x);
525}
526
527inline int_least32_t
528ldle_i32(const void *ptr)
529{
530 return ldle_u32(ptr);
531}
532
533inline void
534stle_u32(void *ptr, uint_least32_t x)
535{
536 x = htole32(x);
537 memcpy(ptr, &x, sizeof(x));
538}
539
540inline uint_least32_t
541ldle_u32(const void *ptr)
542{
543 uint_least32_t x = 0;
544 memcpy(&x, ptr, sizeof(x));
545 return letoh32(x);
546}
547
548inline void
549stbe_i64(void *ptr, int_least64_t x)
550{
551 stbe_u64(ptr, x);
552}
553
554inline int_least64_t
555ldbe_i64(const void *ptr)
556{
557 return ldbe_u64(ptr);
558}
559
560inline void
561stbe_u64(void *ptr, uint_least64_t x)
562{
563 x = htobe64(x);
564 memcpy(ptr, &x, sizeof(x));
565}
566
567inline uint_least64_t
568ldbe_u64(const void *ptr)
569{
570 uint_least64_t x = 0;
571 memcpy(&x, ptr, sizeof(x));
572 return betoh64(x);
573}
574
575inline void
576stle_i64(void *ptr, int_least64_t x)
577{
578 stle_u64(ptr, x);
579}
580
581inline int_least64_t
582ldle_i64(const void *ptr)
583{
584 return ldle_u64(ptr);
585}
586
587inline void
588stle_u64(void *ptr, uint_least64_t x)
589{
590 x = htole64(x);
591 memcpy(ptr, &x, sizeof(x));
592}
593
594inline uint_least64_t
595ldle_u64(const void *ptr)
596{
597 uint_least64_t x = 0;
598 memcpy(&x, ptr, sizeof(x));
599 return letoh64(x);
600}
601
602#ifdef LELY_FLT16_TYPE
603
604inline void
605stbe_flt16(void *ptr, flt16_t x)
606{
607 uint_least16_t tmp = 0;
608 memcpy(&tmp, &x, sizeof(x));
609 stbe_u16(ptr, tmp);
610}
611
612inline flt16_t
613ldbe_flt16(const void *ptr)
614{
615 flt16_t x = 0;
616 uint_least16_t tmp = ldbe_u16(ptr);
617 memcpy(&x, &tmp, sizeof(x));
618 return x;
619}
620
621inline void
622stle_flt16(void *ptr, flt16_t x)
623{
624 uint_least16_t tmp = 0;
625 memcpy(&tmp, &x, sizeof(x));
626 stle_u16(ptr, tmp);
627}
628
629inline flt16_t
630ldle_flt16(const void *ptr)
631{
632 flt16_t x = 0;
633 uint_least16_t tmp = ldle_u16(ptr);
634 memcpy(&x, &tmp, sizeof(x));
635 return x;
636}
637
638#endif // LELY_FLT16_TYPE
639
640#ifdef LELY_FLT32_TYPE
641
642inline void
643stbe_flt32(void *ptr, flt32_t x)
644{
645 uint_least32_t tmp = 0;
646 memcpy(&tmp, &x, sizeof(x));
647 stbe_u32(ptr, tmp);
648}
649
650inline flt32_t
651ldbe_flt32(const void *ptr)
652{
653 flt32_t x = 0;
654 uint_least32_t tmp = ldbe_u32(ptr);
655 memcpy(&x, &tmp, sizeof(x));
656 return x;
657}
658
659inline void
660stle_flt32(void *ptr, flt32_t x)
661{
662 uint_least32_t tmp = 0;
663 memcpy(&tmp, &x, sizeof(x));
664 stle_u32(ptr, tmp);
665}
666
667inline flt32_t
668ldle_flt32(const void *ptr)
669{
670 flt32_t x = 0;
671 uint_least32_t tmp = ldle_u32(ptr);
672 memcpy(&x, &tmp, sizeof(x));
673 return x;
674}
675
676#endif // LELY_FLT32_TYPE
677
678#ifdef LELY_FLT64_TYPE
679
680inline void
681stbe_flt64(void *ptr, flt64_t x)
682{
683 uint_least64_t tmp = 0;
684 memcpy(&tmp, &x, sizeof(x));
685 stbe_u64(ptr, tmp);
686}
687
688inline flt64_t
689ldbe_flt64(const void *ptr)
690{
691 flt64_t x = 0;
692 uint_least64_t tmp = ldbe_u64(ptr);
693 memcpy(&x, &tmp, sizeof(x));
694 return x;
695}
696
697inline void
698stle_flt64(void *ptr, flt64_t x)
699{
700 uint_least64_t tmp = 0;
701 memcpy(&tmp, &x, sizeof(x));
702 stle_u64(ptr, tmp);
703}
704
705inline flt64_t
706ldle_flt64(const void *ptr)
707{
708 flt64_t x = 0;
709 uint_least64_t tmp = ldle_u64(ptr);
710 memcpy(&x, &tmp, sizeof(x));
711 return x;
712}
713
714#endif // LELY_FLT64_TYPE
715
716#ifdef __cplusplus
717}
718#endif
719
720#endif // !LELY_UTIL_ENDIAN_H_
This header file is part of the utilities library; it contains the bit function definitions.
uint_least64_t bswap64(uint_least64_t x)
Reverses the byte order of the 64-bit unsigned integer x.
Definition: bits.h:308
uint_least16_t bswap16(uint_least16_t x)
Reverses the byte order of the 16-bit unsigned integer x.
Definition: bits.h:284
uint_least32_t bswap32(uint_least32_t x)
Reverses the byte order of the 32-bit unsigned integer x.
Definition: bits.h:296
uint_least16_t betoh16(uint_least16_t x)
Converts a 16-bit unsigned integer from big-endian to host byte order.
Definition: endian.h:329
void stbe_u64(void *ptr, uint_least64_t x)
Stores a 64-bit unsigned integer in big-endian byte order.
Definition: endian.h:561
uint_least32_t betoh32(uint_least32_t x)
Converts a 32-bit unsigned integer from big-endian to host byte order.
Definition: endian.h:371
int_least64_t ldbe_i64(const void *ptr)
Loads a 64-bit signed integer in big-endian byte order.
Definition: endian.h:555
void stbe_i64(void *ptr, int_least64_t x)
Stores a 64-bit signed integer in big-endian byte order.
Definition: endian.h:549
uint_least16_t htole16(uint_least16_t x)
Converts a 16-bit unsigned integer from host to little-endian byte order.
Definition: endian.h:337
void stle_u16(void *ptr, uint_least16_t x)
Stores a 16-bit unsigned integer in little-endian byte order.
Definition: endian.h:480
void stle_i32(void *ptr, int_least32_t x)
Stores a 32-bit signed integer in little-endian byte order.
Definition: endian.h:522
uint_least64_t betoh64(uint_least64_t x)
Converts a 64-bit unsigned integer from big-endian to host byte order.
Definition: endian.h:413
int_least32_t ldbe_i32(const void *ptr)
Loads a 32-bit signed integer in big-endian byte order.
Definition: endian.h:501
uint_least32_t htole32(uint_least32_t x)
Converts a 32-bit unsigned integer from host to little-endian byte order.
Definition: endian.h:379
void stbe_u16(void *ptr, uint_least16_t x)
Stores a 16-bit unsigned integer in big-endian byte order.
Definition: endian.h:453
void stbe_u32(void *ptr, uint_least32_t x)
Stores a 32-bit unsigned integer in big-endian byte order.
Definition: endian.h:507
void stbe_i16(void *ptr, int_least16_t x)
Stores a 16-bit signed integer in big-endian byte order.
Definition: endian.h:441
uint_least32_t letoh32(uint_least32_t x)
Converts a 32-bit unsigned integer from little-endian to host byte order.
Definition: endian.h:392
void bcpybe(void *dst, int dstbit, const void *src, int srcbit, size_t n)
Copies n bits from a source to a destination buffer.
Definition: endian.c:38
uint_least16_t htobe16(uint_least16_t x)
Converts a 16-bit unsigned integer from host to big-endian byte order.
Definition: endian.h:316
void stbe_i32(void *ptr, int_least32_t x)
Stores a 32-bit signed integer in big-endian byte order.
Definition: endian.h:495
int_least32_t ldle_i32(const void *ptr)
Loads a 32-bit signed integer in little-endian byte order.
Definition: endian.h:528
uint_least64_t letoh64(uint_least64_t x)
Converts a 64-bit unsigned integer from little-endian to host byte order.
Definition: endian.h:434
void bcpyle(void *dst, int dstbit, const void *src, int srcbit, size_t n)
Copies n bits from a source to a destination buffer.
Definition: endian.c:128
int_least16_t ldle_i16(const void *ptr)
Loads a 16-bit signed integer in little-endian byte order.
Definition: endian.h:474
int_least64_t ldle_i64(const void *ptr)
Loads a 64-bit signed integer in little-endian byte order.
Definition: endian.h:582
uint_least32_t htobe32(uint_least32_t x)
Converts a 32-bit unsigned integer from host to big-endian byte order.
Definition: endian.h:358
void stle_u64(void *ptr, uint_least64_t x)
Stores a 64-bit unsigned integer in little-endian byte order.
Definition: endian.h:588
void stle_u32(void *ptr, uint_least32_t x)
Stores a 32-bit unsigned integer in little-endian byte order.
Definition: endian.h:534
uint_least32_t ldbe_u32(const void *ptr)
Loads a 32-bit unsigned integer in big-endian byte order.
Definition: endian.h:514
uint_least32_t ldle_u32(const void *ptr)
Loads a 32-bit unsigned integer in little-endian byte order.
Definition: endian.h:541
uint_least64_t htobe64(uint_least64_t x)
Converts a 64-bit unsigned integer from host to big-endian byte order.
Definition: endian.h:400
uint_least64_t htole64(uint_least64_t x)
Converts a 64-bit unsigned integer from host to little-endian byte order.
Definition: endian.h:421
void stle_i64(void *ptr, int_least64_t x)
Stores a 64-bit signed integer in little-endian byte order.
Definition: endian.h:576
void stle_i16(void *ptr, int_least16_t x)
Stores a 16-bit signed integer in little-endian byte order.
Definition: endian.h:468
int_least16_t ldbe_i16(const void *ptr)
Loads a 16-bit signed integer in big-endian byte order.
Definition: endian.h:447
uint_least64_t ldbe_u64(const void *ptr)
Loads a 64-bit unsigned integer in big-endian byte order.
Definition: endian.h:568
uint_least16_t letoh16(uint_least16_t x)
Converts a 16-bit unsigned integer from little-endian to host byte order.
Definition: endian.h:350
uint_least64_t ldle_u64(const void *ptr)
Loads a 64-bit unsigned integer in little-endian byte order.
Definition: endian.h:595
uint_least16_t ldle_u16(const void *ptr)
Loads a 16-bit unsigned integer in little-endian byte order.
Definition: endian.h:487
uint_least16_t ldbe_u16(const void *ptr)
Loads a 16-bit unsigned integer in big-endian byte order.
Definition: endian.h:460
This header file is part of the utilities library; it contains the IEEE 754 floating-point format typ...
This header file is part of the C11 and POSIX compatibility library; it includes <string....