Lely core libraries  2.3.4
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 #ifdef __linux__
31 #include <endian.h>
32 #define betoh16 be16toh
33 #define letoh16 le16toh
34 #define betoh32 be32toh
35 #define letoh32 le32toh
36 #define betoh64 be64toh
37 #define letoh64 le64toh
38 #endif
39 
40 #ifndef LELY_UTIL_ENDIAN_INLINE
41 #define LELY_UTIL_ENDIAN_INLINE static inline
42 #endif
43 
44 #ifndef LELY_BIG_ENDIAN
45 #if defined(__BIG_ENDIAN__) || defined(__big_endian__) \
46  || (__GNUC__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
47  || defined(__ARMEB__) || defined(__AARCH64EB__) \
48  || defined(__THUMBEB__)
49 #define LELY_BIG_ENDIAN 1
51 #endif
52 #endif
53 
54 #if LELY_BIG_ENDIAN
55 #undef LELY_LITTLE_ENDIAN
56 #endif
57 
58 #ifndef LELY_LITTLE_ENDIAN
59 #if defined(__LITTLE_ENDIAN__) || defined(__little_endian__) \
60  || (__GNUC__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
61  || defined(__i386__) || defined(_M_IX86) \
62  || defined(__x86_64__) || defined(_M_AMD64) \
63  || defined(__ARMEL__) || defined(__AARCH64EL__) \
64  || defined(__THUMBEL__)
65 #define LELY_LITTLE_ENDIAN 1
67 #endif
68 #endif
69 
70 #if LELY_LITTLE_ENDIAN
71 #undef LELY_BIG_ENDIAN
72 #endif
73 
74 #if !LELY_BIG_ENDIAN && !LELY_LITTLE_ENDIAN
75 #error Unable to determine byte order or byte order is not supported.
76 #endif
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
82 #ifndef htobe16
83 LELY_UTIL_ENDIAN_INLINE uint_least16_t htobe16(uint_least16_t x);
85 #endif
86 
87 #ifndef betoh16
88 LELY_UTIL_ENDIAN_INLINE uint_least16_t betoh16(uint_least16_t x);
90 #endif
91 
92 #ifndef htole16
93 LELY_UTIL_ENDIAN_INLINE uint_least16_t htole16(uint_least16_t x);
95 #endif
96 
97 #ifndef letoh16
98 LELY_UTIL_ENDIAN_INLINE uint_least16_t letoh16(uint_least16_t x);
100 #endif
101 
102 #ifndef htobe32
103 LELY_UTIL_ENDIAN_INLINE uint_least32_t htobe32(uint_least32_t x);
105 #endif
106 
107 #ifndef betoh32
108 LELY_UTIL_ENDIAN_INLINE uint_least32_t betoh32(uint_least32_t x);
110 #endif
111 
112 #ifndef htole32
113 LELY_UTIL_ENDIAN_INLINE uint_least32_t htole32(uint_least32_t x);
115 #endif
116 
117 #ifndef letoh32
118 LELY_UTIL_ENDIAN_INLINE uint_least32_t letoh32(uint_least32_t x);
120 #endif
121 
122 #ifndef htobe64
123 LELY_UTIL_ENDIAN_INLINE uint_least64_t htobe64(uint_least64_t x);
125 #endif
126 
127 #ifndef betoh64
128 LELY_UTIL_ENDIAN_INLINE uint_least64_t betoh64(uint_least64_t x);
130 #endif
131 
132 #ifndef htole64
133 LELY_UTIL_ENDIAN_INLINE uint_least64_t htole64(uint_least64_t x);
135 #endif
136 
137 #ifndef letoh64
138 LELY_UTIL_ENDIAN_INLINE uint_least64_t letoh64(uint_least64_t x);
140 #endif
141 
143 LELY_UTIL_ENDIAN_INLINE void stbe_i16(uint_least8_t dst[2], int_least16_t x);
144 
146 LELY_UTIL_ENDIAN_INLINE int_least16_t ldbe_i16(const uint_least8_t src[2]);
147 
149 LELY_UTIL_ENDIAN_INLINE void stbe_u16(uint_least8_t dst[2], uint_least16_t x);
150 
152 LELY_UTIL_ENDIAN_INLINE uint_least16_t ldbe_u16(const uint_least8_t src[2]);
153 
155 LELY_UTIL_ENDIAN_INLINE void stle_i16(uint_least8_t dst[2], int_least16_t x);
156 
158 LELY_UTIL_ENDIAN_INLINE int_least16_t ldle_i16(const uint_least8_t src[2]);
159 
161 LELY_UTIL_ENDIAN_INLINE void stle_u16(uint_least8_t dst[2], uint_least16_t x);
162 
164 LELY_UTIL_ENDIAN_INLINE uint_least16_t ldle_u16(const uint_least8_t src[2]);
165 
167 LELY_UTIL_ENDIAN_INLINE void stbe_i32(uint_least8_t dst[4], int_least32_t x);
168 
170 LELY_UTIL_ENDIAN_INLINE int_least32_t ldbe_i32(const uint_least8_t src[4]);
171 
173 LELY_UTIL_ENDIAN_INLINE void stbe_u32(uint_least8_t dst[4], uint_least32_t x);
174 
176 LELY_UTIL_ENDIAN_INLINE uint_least32_t ldbe_u32(const uint_least8_t src[4]);
177 
179 LELY_UTIL_ENDIAN_INLINE void stle_i32(uint_least8_t dst[4], int_least32_t x);
180 
182 LELY_UTIL_ENDIAN_INLINE int_least32_t ldle_i32(const uint_least8_t src[4]);
183 
185 LELY_UTIL_ENDIAN_INLINE void stle_u32(uint_least8_t dst[4], uint_least32_t x);
186 
188 LELY_UTIL_ENDIAN_INLINE uint_least32_t ldle_u32(const uint_least8_t src[4]);
189 
191 LELY_UTIL_ENDIAN_INLINE void stbe_i64(uint_least8_t dst[8], int_least64_t x);
192 
194 LELY_UTIL_ENDIAN_INLINE int_least64_t ldbe_i64(const uint_least8_t src[8]);
195 
197 LELY_UTIL_ENDIAN_INLINE void stbe_u64(uint_least8_t dst[8], uint_least64_t x);
198 
200 LELY_UTIL_ENDIAN_INLINE uint_least64_t ldbe_u64(const uint_least8_t src[8]);
201 
203 LELY_UTIL_ENDIAN_INLINE void stle_i64(uint_least8_t dst[8], int_least64_t x);
204 
206 LELY_UTIL_ENDIAN_INLINE int_least64_t ldle_i64(const uint_least8_t src[8]);
207 
209 LELY_UTIL_ENDIAN_INLINE void stle_u64(uint_least8_t dst[8], uint_least64_t x);
210 
212 LELY_UTIL_ENDIAN_INLINE uint_least64_t ldle_u64(const uint_least8_t src[8]);
213 
214 #ifdef LELY_FLT16_TYPE
215 
220 LELY_UTIL_ENDIAN_INLINE void stbe_flt16(uint_least8_t dst[2], flt16_t x);
221 
226 LELY_UTIL_ENDIAN_INLINE flt16_t ldbe_flt16(const uint_least8_t src[2]);
227 
232 LELY_UTIL_ENDIAN_INLINE void stle_flt16(uint_least8_t dst[2], flt16_t x);
233 
238 LELY_UTIL_ENDIAN_INLINE flt16_t ldle_flt16(const uint_least8_t src[2]);
239 
240 #endif // LELY_FLT16_TYPE
241 
242 #ifdef LELY_FLT32_TYPE
243 
248 LELY_UTIL_ENDIAN_INLINE void stbe_flt32(uint_least8_t dst[4], flt32_t x);
249 
254 LELY_UTIL_ENDIAN_INLINE flt32_t ldbe_flt32(const uint_least8_t src[4]);
255 
260 LELY_UTIL_ENDIAN_INLINE void stle_flt32(uint_least8_t dst[4], flt32_t x);
261 
266 LELY_UTIL_ENDIAN_INLINE flt32_t ldle_flt32(const uint_least8_t src[4]);
267 
268 #endif // LELY_FLT32_TYPE
269 
270 #ifdef LELY_FLT64_TYPE
271 
276 LELY_UTIL_ENDIAN_INLINE void stbe_flt64(uint_least8_t dst[8], flt64_t x);
277 
282 LELY_UTIL_ENDIAN_INLINE flt64_t ldbe_flt64(const uint_least8_t src[8]);
283 
288 LELY_UTIL_ENDIAN_INLINE void stle_flt64(uint_least8_t dst[8], flt64_t x);
289 
294 LELY_UTIL_ENDIAN_INLINE flt64_t ldle_flt64(const uint_least8_t src[8]);
295 
296 #endif // LELY_FLT64_TYPE
297 
310 void bcpybe(uint_least8_t *dst, int dstbit, const uint_least8_t *src,
311  int srcbit, size_t n);
312 
325 void bcpyle(uint_least8_t *dst, int dstbit, const uint_least8_t *src,
326  int srcbit, size_t n);
327 
328 #ifndef htobe16
329 LELY_UTIL_ENDIAN_INLINE uint_least16_t
330 htobe16(uint_least16_t x)
331 {
332  x &= UINT16_C(0xffff);
333 #if LELY_BIG_ENDIAN
334  return x;
335 #elif LELY_LITTLE_ENDIAN
336  return bswap16(x);
337 #endif
338 }
339 #endif
340 
341 #ifndef betoh16
342 LELY_UTIL_ENDIAN_INLINE uint_least16_t
343 betoh16(uint_least16_t x)
344 {
345  return htobe16(x);
346 }
347 #endif
348 
349 #ifndef htole16
350 LELY_UTIL_ENDIAN_INLINE uint_least16_t
351 htole16(uint_least16_t x)
352 {
353  x &= UINT16_C(0xffff);
354 #if LELY_BIG_ENDIAN
355  return bswap16(x);
356 #elif LELY_LITTLE_ENDIAN
357  return x;
358 #endif
359 }
360 #endif
361 
362 #ifndef letoh16
363 LELY_UTIL_ENDIAN_INLINE uint_least16_t
364 letoh16(uint_least16_t x)
365 {
366  return htole16(x);
367 }
368 #endif
369 
370 #ifndef htobe32
371 LELY_UTIL_ENDIAN_INLINE uint_least32_t
372 htobe32(uint_least32_t x)
373 {
374  x &= UINT32_C(0xffffffff);
375 #if LELY_BIG_ENDIAN
376  return x;
377 #elif LELY_LITTLE_ENDIAN
378  return bswap32(x);
379 #endif
380 }
381 #endif
382 
383 #ifndef betoh32
384 LELY_UTIL_ENDIAN_INLINE uint_least32_t
385 betoh32(uint_least32_t x)
386 {
387  return htobe32(x);
388 }
389 #endif
390 
391 #ifndef htole32
392 LELY_UTIL_ENDIAN_INLINE uint_least32_t
393 htole32(uint_least32_t x)
394 {
395  x &= UINT32_C(0xffffffff);
396 #if LELY_BIG_ENDIAN
397  return bswap32(x);
398 #elif LELY_LITTLE_ENDIAN
399  return x;
400 #endif
401 }
402 #endif
403 
404 #ifndef letoh32
405 LELY_UTIL_ENDIAN_INLINE uint_least32_t
406 letoh32(uint_least32_t x)
407 {
408  return htole32(x);
409 }
410 #endif
411 
412 #ifndef htobe64
413 LELY_UTIL_ENDIAN_INLINE uint_least64_t
414 htobe64(uint_least64_t x)
415 {
416  x &= UINT64_C(0xffffffffffffffff);
417 #if LELY_BIG_ENDIAN
418  return x;
419 #elif LELY_LITTLE_ENDIAN
420  return bswap64(x);
421 #endif
422 }
423 #endif
424 
425 #ifndef betoh64
426 LELY_UTIL_ENDIAN_INLINE uint_least64_t
427 betoh64(uint_least64_t x)
428 {
429  return htobe64(x);
430 }
431 #endif
432 
433 #ifndef htole64
434 LELY_UTIL_ENDIAN_INLINE uint_least64_t
435 htole64(uint_least64_t x)
436 {
437  x &= UINT64_C(0xffffffffffffffff);
438 #if LELY_BIG_ENDIAN
439  return bswap64(x);
440 #elif LELY_LITTLE_ENDIAN
441  return x;
442 #endif
443 }
444 #endif
445 
446 #ifndef letoh64
447 LELY_UTIL_ENDIAN_INLINE uint_least64_t
448 letoh64(uint_least64_t x)
449 {
450  return htole64(x);
451 }
452 #endif
453 
454 LELY_UTIL_ENDIAN_INLINE void
455 stbe_i16(uint_least8_t dst[2], int_least16_t x)
456 {
457  stbe_u16(dst, x);
458 }
459 
460 LELY_UTIL_ENDIAN_INLINE int_least16_t
461 ldbe_i16(const uint_least8_t src[2])
462 {
463  return ldbe_u16(src);
464 }
465 
466 LELY_UTIL_ENDIAN_INLINE void
467 stbe_u16(uint_least8_t dst[2], uint_least16_t x)
468 {
469 #if CHAR_BIT == 8
470  x = htobe16(x);
471  memcpy(dst, &x, sizeof(x));
472 #else
473  dst[0] = (x >> 8) & 0xff;
474  dst[1] = x & 0xff;
475 #endif
476 }
477 
478 LELY_UTIL_ENDIAN_INLINE uint_least16_t
479 ldbe_u16(const uint_least8_t src[2])
480 {
481 #if CHAR_BIT == 8
482  uint_least16_t x = 0;
483  memcpy(&x, src, sizeof(x));
484  return betoh16(x);
485 #else
486  return ((uint_least16_t)(src[0] & 0xff) << 8)
487  | ((uint_least16_t)(src[1] & 0xff));
488 #endif
489 }
490 
491 LELY_UTIL_ENDIAN_INLINE void
492 stle_i16(uint_least8_t dst[2], int_least16_t x)
493 {
494  stle_u16(dst, x);
495 }
496 
497 LELY_UTIL_ENDIAN_INLINE int_least16_t
498 ldle_i16(const uint_least8_t src[2])
499 {
500  return ldle_u16(src);
501 }
502 
503 LELY_UTIL_ENDIAN_INLINE void
504 stle_u16(uint_least8_t dst[2], uint_least16_t x)
505 {
506 #if CHAR_BIT == 8
507  x = htole16(x);
508  memcpy(dst, &x, sizeof(x));
509 #else
510  dst[0] = x & 0xff;
511  dst[1] = (x >> 8) & 0xff;
512 #endif
513 }
514 
515 LELY_UTIL_ENDIAN_INLINE uint_least16_t
516 ldle_u16(const uint_least8_t src[2])
517 {
518 #if CHAR_BIT == 8
519  uint_least16_t x = 0;
520  memcpy(&x, src, sizeof(x));
521  return letoh16(x);
522 #else
523  return (uint_least16_t)(src[0] & 0xff)
524  | ((uint_least16_t)(src[1] & 0xff) << 8);
525 #endif
526 }
527 
528 LELY_UTIL_ENDIAN_INLINE void
529 stbe_i32(uint_least8_t dst[4], int_least32_t x)
530 {
531  stbe_u32(dst, x);
532 }
533 
534 LELY_UTIL_ENDIAN_INLINE int_least32_t
535 ldbe_i32(const uint_least8_t src[4])
536 {
537  return ldbe_u32(src);
538 }
539 
540 LELY_UTIL_ENDIAN_INLINE void
541 stbe_u32(uint_least8_t dst[4], uint_least32_t x)
542 {
543 #if CHAR_BIT == 8
544  x = htobe32(x);
545  memcpy(dst, &x, sizeof(x));
546 #else
547  dst[0] = (x >> 24) & 0xff;
548  dst[1] = (x >> 16) & 0xff;
549  dst[2] = (x >> 8) & 0xff;
550  dst[3] = x & 0xff;
551 #endif
552 }
553 
554 LELY_UTIL_ENDIAN_INLINE uint_least32_t
555 ldbe_u32(const uint_least8_t src[4])
556 {
557 #if CHAR_BIT == 8
558  uint_least32_t x = 0;
559  memcpy(&x, src, sizeof(x));
560  return betoh32(x);
561 #else
562  return ((uint_least32_t)(src[0] & 0xff) << 24)
563  | ((uint_least32_t)(src[1] & 0xff) << 16)
564  | ((uint_least32_t)(src[2] & 0xff) << 8)
565  | ((uint_least32_t)(src[3] & 0xff));
566 #endif
567 }
568 
569 LELY_UTIL_ENDIAN_INLINE void
570 stle_i32(uint_least8_t dst[4], int_least32_t x)
571 {
572  stle_u32(dst, x);
573 }
574 
575 LELY_UTIL_ENDIAN_INLINE int_least32_t
576 ldle_i32(const uint_least8_t src[4])
577 {
578  return ldle_u32(src);
579 }
580 
581 LELY_UTIL_ENDIAN_INLINE void
582 stle_u32(uint_least8_t dst[4], uint_least32_t x)
583 {
584 #if CHAR_BIT == 8
585  x = htole32(x);
586  memcpy(dst, &x, sizeof(x));
587 #else
588  dst[0] = x & 0xff;
589  dst[1] = (x >> 8) & 0xff;
590  dst[2] = (x >> 16) & 0xff;
591  dst[3] = (x >> 24) & 0xff;
592 #endif
593 }
594 
595 LELY_UTIL_ENDIAN_INLINE uint_least32_t
596 ldle_u32(const uint_least8_t src[4])
597 {
598 #if CHAR_BIT == 8
599  uint_least32_t x = 0;
600  memcpy(&x, src, sizeof(x));
601  return letoh32(x);
602 #else
603  return (uint_least32_t)(src[0] & 0xff)
604  | ((uint_least32_t)(src[1] & 0xff) << 8)
605  | ((uint_least32_t)(src[2] & 0xff) << 16)
606  | ((uint_least32_t)(src[3] & 0xff) << 24);
607 #endif
608 }
609 
610 LELY_UTIL_ENDIAN_INLINE void
611 stbe_i64(uint_least8_t dst[8], int_least64_t x)
612 {
613  stbe_u64(dst, x);
614 }
615 
616 LELY_UTIL_ENDIAN_INLINE int_least64_t
617 ldbe_i64(const uint_least8_t src[8])
618 {
619  return ldbe_u64(src);
620 }
621 
622 LELY_UTIL_ENDIAN_INLINE void
623 stbe_u64(uint_least8_t dst[8], uint_least64_t x)
624 {
625 #if CHAR_BIT == 8
626  x = htobe64(x);
627  memcpy(dst, &x, sizeof(x));
628 #else
629  dst[0] = (x >> 56) & 0xff;
630  dst[1] = (x >> 48) & 0xff;
631  dst[2] = (x >> 40) & 0xff;
632  dst[3] = (x >> 32) & 0xff;
633  dst[4] = (x >> 24) & 0xff;
634  dst[5] = (x >> 16) & 0xff;
635  dst[6] = (x >> 8) & 0xff;
636  dst[7] = x & 0xff;
637 #endif
638 }
639 
640 LELY_UTIL_ENDIAN_INLINE uint_least64_t
641 ldbe_u64(const uint_least8_t src[8])
642 {
643 #if CHAR_BIT == 8
644  uint_least64_t x = 0;
645  memcpy(&x, src, sizeof(x));
646  return betoh64(x);
647 #else
648  return ((uint_least64_t)(src[0] & 0xff) << 56)
649  | ((uint_least64_t)(src[1] & 0xff) << 48)
650  | ((uint_least64_t)(src[2] & 0xff) << 40)
651  | ((uint_least64_t)(src[3] & 0xff) << 32)
652  | ((uint_least64_t)(src[4] & 0xff) << 24)
653  | ((uint_least64_t)(src[5] & 0xff) << 16)
654  | ((uint_least64_t)(src[6] & 0xff) << 8)
655  | ((uint_least64_t)(src[7] & 0xff));
656 #endif
657 }
658 
659 LELY_UTIL_ENDIAN_INLINE void
660 stle_i64(uint_least8_t dst[8], int_least64_t x)
661 {
662  stle_u64(dst, x);
663 }
664 
665 LELY_UTIL_ENDIAN_INLINE int_least64_t
666 ldle_i64(const uint_least8_t src[8])
667 {
668  return ldle_u64(src);
669 }
670 
671 LELY_UTIL_ENDIAN_INLINE void
672 stle_u64(uint_least8_t dst[8], uint_least64_t x)
673 {
674 #if CHAR_BIT == 8
675  x = htole64(x);
676  memcpy(dst, &x, sizeof(x));
677 #else
678  dst[0] = x & 0xff;
679  dst[1] = (x >> 8) & 0xff;
680  dst[2] = (x >> 16) & 0xff;
681  dst[3] = (x >> 24) & 0xff;
682  dst[4] = (x >> 32) & 0xff;
683  dst[5] = (x >> 40) & 0xff;
684  dst[6] = (x >> 48) & 0xff;
685  dst[7] = (x >> 56) & 0xff;
686 #endif
687 }
688 
689 LELY_UTIL_ENDIAN_INLINE uint_least64_t
690 ldle_u64(const uint_least8_t src[8])
691 {
692 #if CHAR_BIT == 8
693  uint_least64_t x = 0;
694  memcpy(&x, src, sizeof(x));
695  return letoh64(x);
696 #else
697  return (uint_least64_t)(src[0] & 0xff)
698  | ((uint_least64_t)(src[1] & 0xff) << 8)
699  | ((uint_least64_t)(src[2] & 0xff) << 16)
700  | ((uint_least64_t)(src[3] & 0xff) << 24)
701  | ((uint_least64_t)(src[4] & 0xff) << 32)
702  | ((uint_least64_t)(src[5] & 0xff) << 40)
703  | ((uint_least64_t)(src[6] & 0xff) << 48)
704  | ((uint_least64_t)(src[7] & 0xff) << 56);
705 #endif
706 }
707 
708 #ifdef LELY_FLT16_TYPE
709 
710 LELY_UTIL_ENDIAN_INLINE void
711 stbe_flt16(uint_least8_t dst[2], flt16_t x)
712 {
713  uint_least16_t tmp = 0;
714  memcpy(&tmp, &x, sizeof(x));
715  stbe_u16(dst, tmp);
716 }
717 
718 LELY_UTIL_ENDIAN_INLINE flt16_t
719 ldbe_flt16(const uint_least8_t src[2])
720 {
721  flt16_t x = 0;
722  uint_least16_t tmp = ldbe_u16(src);
723  memcpy(&x, &tmp, sizeof(x));
724  return x;
725 }
726 
727 LELY_UTIL_ENDIAN_INLINE void
728 stle_flt16(uint_least8_t dst[2], flt16_t x)
729 {
730  uint_least16_t tmp = 0;
731  memcpy(&tmp, &x, sizeof(x));
732  stle_u16(dst, tmp);
733 }
734 
735 LELY_UTIL_ENDIAN_INLINE flt16_t
736 ldle_flt16(const uint_least8_t src[2])
737 {
738  flt16_t x = 0;
739  uint_least16_t tmp = ldle_u16(src);
740  memcpy(&x, &tmp, sizeof(x));
741  return x;
742 }
743 
744 #endif // LELY_FLT16_TYPE
745 
746 #ifdef LELY_FLT32_TYPE
747 
748 LELY_UTIL_ENDIAN_INLINE void
749 stbe_flt32(uint_least8_t dst[4], flt32_t x)
750 {
751  uint_least32_t tmp = 0;
752  memcpy(&tmp, &x, sizeof(x));
753  stbe_u32(dst, tmp);
754 }
755 
756 LELY_UTIL_ENDIAN_INLINE flt32_t
757 ldbe_flt32(const uint_least8_t src[4])
758 {
759  flt32_t x = 0;
760  uint_least32_t tmp = ldbe_u32(src);
761  memcpy(&x, &tmp, sizeof(x));
762  return x;
763 }
764 
765 LELY_UTIL_ENDIAN_INLINE void
766 stle_flt32(uint_least8_t dst[4], flt32_t x)
767 {
768  uint_least32_t tmp = 0;
769  memcpy(&tmp, &x, sizeof(x));
770  stle_u32(dst, tmp);
771 }
772 
773 LELY_UTIL_ENDIAN_INLINE flt32_t
774 ldle_flt32(const uint_least8_t src[4])
775 {
776  flt32_t x = 0;
777  uint_least32_t tmp = ldle_u32(src);
778  memcpy(&x, &tmp, sizeof(x));
779  return x;
780 }
781 
782 #endif // LELY_FLT32_TYPE
783 
784 #ifdef LELY_FLT64_TYPE
785 
786 LELY_UTIL_ENDIAN_INLINE void
787 stbe_flt64(uint_least8_t dst[8], flt64_t x)
788 {
789  uint_least64_t tmp = 0;
790  memcpy(&tmp, &x, sizeof(x));
791  stbe_u64(dst, tmp);
792 }
793 
794 LELY_UTIL_ENDIAN_INLINE flt64_t
795 ldbe_flt64(const uint_least8_t src[8])
796 {
797  flt64_t x = 0;
798  uint_least64_t tmp = ldbe_u64(src);
799  memcpy(&x, &tmp, sizeof(x));
800  return x;
801 }
802 
803 LELY_UTIL_ENDIAN_INLINE void
804 stle_flt64(uint_least8_t dst[8], flt64_t x)
805 {
806  uint_least64_t tmp = 0;
807  memcpy(&tmp, &x, sizeof(x));
808  stle_u64(dst, tmp);
809 }
810 
811 LELY_UTIL_ENDIAN_INLINE flt64_t
812 ldle_flt64(const uint_least8_t src[8])
813 {
814  flt64_t x = 0;
815  uint_least64_t tmp = ldle_u64(src);
816  memcpy(&x, &tmp, sizeof(x));
817  return x;
818 }
819 
820 #endif // LELY_FLT64_TYPE
821 
822 #ifdef __cplusplus
823 }
824 #endif
825 
826 #endif // !LELY_UTIL_ENDIAN_H_
htole64
uint_least64_t htole64(uint_least64_t x)
Converts a 64-bit unsigned integer from host to little-endian byte order.
Definition: endian.h:435
ldle_u64
uint_least64_t ldle_u64(const uint_least8_t src[8])
Loads a 64-bit unsigned integer in little-endian byte order.
Definition: endian.h:690
htobe64
uint_least64_t htobe64(uint_least64_t x)
Converts a 64-bit unsigned integer from host to big-endian byte order.
Definition: endian.h:414
bcpybe
void bcpybe(uint_least8_t *dst, int dstbit, const uint_least8_t *src, int srcbit, size_t n)
Copies n bits from a source to a destination buffer.
Definition: endian.c:39
string.h
float.h
ldle_i32
int_least32_t ldle_i32(const uint_least8_t src[4])
Loads a 32-bit signed integer in little-endian byte order.
Definition: endian.h:576
bswap16
uint_least16_t bswap16(uint_least16_t x)
Reverses the byte order of the 16-bit unsigned integer x.
Definition: bits.h:283
ldle_u32
uint_least32_t ldle_u32(const uint_least8_t src[4])
Loads a 32-bit unsigned integer in little-endian byte order.
Definition: endian.h:596
ldbe_i16
int_least16_t ldbe_i16(const uint_least8_t src[2])
Loads a 16-bit signed integer in big-endian byte order.
Definition: endian.h:461
ldle_i64
int_least64_t ldle_i64(const uint_least8_t src[8])
Loads a 64-bit signed integer in little-endian byte order.
Definition: endian.h:666
htobe16
uint_least16_t htobe16(uint_least16_t x)
Converts a 16-bit unsigned integer from host to big-endian byte order.
Definition: endian.h:330
ldbe_u64
uint_least64_t ldbe_u64(const uint_least8_t src[8])
Loads a 64-bit unsigned integer in big-endian byte order.
Definition: endian.h:641
stbe_u16
void stbe_u16(uint_least8_t dst[2], uint_least16_t x)
Stores a 16-bit unsigned integer in big-endian byte order.
Definition: endian.h:467
ldbe_u16
uint_least16_t ldbe_u16(const uint_least8_t src[2])
Loads a 16-bit unsigned integer in big-endian byte order.
Definition: endian.h:479
htole16
uint_least16_t htole16(uint_least16_t x)
Converts a 16-bit unsigned integer from host to little-endian byte order.
Definition: endian.h:351
bits.h
stle_i32
void stle_i32(uint_least8_t dst[4], int_least32_t x)
Stores a 32-bit signed integer in little-endian byte order.
Definition: endian.h:570
stbe_i32
void stbe_i32(uint_least8_t dst[4], int_least32_t x)
Stores a 32-bit signed integer in big-endian byte order.
Definition: endian.h:529
ldbe_u32
uint_least32_t ldbe_u32(const uint_least8_t src[4])
Loads a 32-bit unsigned integer in big-endian byte order.
Definition: endian.h:555
htobe32
uint_least32_t htobe32(uint_least32_t x)
Converts a 32-bit unsigned integer from host to big-endian byte order.
Definition: endian.h:372
bswap64
uint_least64_t bswap64(uint_least64_t x)
Reverses the byte order of the 64-bit unsigned integer x.
Definition: bits.h:308
stle_u32
void stle_u32(uint_least8_t dst[4], uint_least32_t x)
Stores a 32-bit unsigned integer in little-endian byte order.
Definition: endian.h:582
stbe_i64
void stbe_i64(uint_least8_t dst[8], int_least64_t x)
Stores a 64-bit signed integer in big-endian byte order.
Definition: endian.h:611
stle_u64
void stle_u64(uint_least8_t dst[8], uint_least64_t x)
Stores a 64-bit unsigned integer in little-endian byte order.
Definition: endian.h:672
stbe_u64
void stbe_u64(uint_least8_t dst[8], uint_least64_t x)
Stores a 64-bit unsigned integer in big-endian byte order.
Definition: endian.h:623
bswap32
uint_least32_t bswap32(uint_least32_t x)
Reverses the byte order of the 32-bit unsigned integer x.
Definition: bits.h:295
htole32
uint_least32_t htole32(uint_least32_t x)
Converts a 32-bit unsigned integer from host to little-endian byte order.
Definition: endian.h:393
bcpyle
void bcpyle(uint_least8_t *dst, int dstbit, const uint_least8_t *src, int srcbit, size_t n)
Copies n bits from a source to a destination buffer.
Definition: endian.c:123
ldle_i16
int_least16_t ldle_i16(const uint_least8_t src[2])
Loads a 16-bit signed integer in little-endian byte order.
Definition: endian.h:498
ldbe_i32
int_least32_t ldbe_i32(const uint_least8_t src[4])
Loads a 32-bit signed integer in big-endian byte order.
Definition: endian.h:535
stle_u16
void stle_u16(uint_least8_t dst[2], uint_least16_t x)
Stores a 16-bit unsigned integer in little-endian byte order.
Definition: endian.h:504
stle_i16
void stle_i16(uint_least8_t dst[2], int_least16_t x)
Stores a 16-bit signed integer in little-endian byte order.
Definition: endian.h:492
ldle_u16
uint_least16_t ldle_u16(const uint_least8_t src[2])
Loads a 16-bit unsigned integer in little-endian byte order.
Definition: endian.h:516
stle_i64
void stle_i64(uint_least8_t dst[8], int_least64_t x)
Stores a 64-bit signed integer in little-endian byte order.
Definition: endian.h:660
stbe_u32
void stbe_u32(uint_least8_t dst[4], uint_least32_t x)
Stores a 32-bit unsigned integer in big-endian byte order.
Definition: endian.h:541
ldbe_i64
int_least64_t ldbe_i64(const uint_least8_t src[8])
Loads a 64-bit signed integer in big-endian byte order.
Definition: endian.h:617
stbe_i16
void stbe_i16(uint_least8_t dst[2], int_least16_t x)
Stores a 16-bit signed integer in big-endian byte order.
Definition: endian.h:455
endian.h