endian.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*-
  2. * Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. /*
  7. * A mostly Linux/glibc-compatible endian.h
  8. */
  9. #ifndef _ENDIAN_H_
  10. #define _ENDIAN_H_
  11. /*
  12. * POSIX Issue 8 requires that endian.h define uint{16,32,64}_t. Although POSIX
  13. * allows stdint.h symbols here, be conservative and only define there required
  14. * ones. FreeBSD's sys/_endian.h doesn't need to expose those types since it
  15. * implements all the [bl]eXtoh hto[bl]eX interfaces as macros calling builtin
  16. * functions. POSIX allows functions, macros or both. We opt for macros only.
  17. */
  18. #include <sys/_types.h>
  19. #ifndef _UINT16_T_DECLARED
  20. typedef __uint16_t uint16_t;
  21. #define _UINT16_T_DECLARED
  22. #endif
  23. #ifndef _UINT32_T_DECLARED
  24. typedef __uint32_t uint32_t;
  25. #define _UINT32_T_DECLARED
  26. #endif
  27. #ifndef _UINT64_T_DECLARED
  28. typedef __uint64_t uint64_t;
  29. #define _UINT64_T_DECLARED
  30. #endif
  31. /*
  32. * FreeBSD's sys/_endian.h is very close to the interface provided on Linux by
  33. * glibc's endian.h as well as POSIX Issue 8's endian.h.
  34. */
  35. #include <sys/_endian.h>
  36. /*
  37. * glibc uses double underscore for these symbols. Define these unconditionally.
  38. * The compiler defines __BYTE_ORDER__ these days, so we don't do anything
  39. * with that since sys/endian.h defines _BYTE_ORDER based on it.
  40. */
  41. #define __BIG_ENDIAN _BIG_ENDIAN
  42. #define __BYTE_ORDER _BYTE_ORDER
  43. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  44. #define __PDP_ENDIAN _PDP_ENDIAN
  45. /*
  46. * FreeBSD's sys/endian.h and machine/endian.h doesn't define a separate
  47. * byte order for floats. Use the host non-float byte order.
  48. */
  49. #define __FLOAT_WORD_ORDER _BYTE_ORDER
  50. /*
  51. * We don't define BIG_ENDI, LITTLE_ENDI, HIGH_HALF and LOW_HALF macros that
  52. * glibc's endian.h defines since those appear to be internal to glibc.
  53. * We also don't try to emulate the various helper macros that glibc uses to
  54. * limit namespace visibility.
  55. */
  56. #endif /* _ENDIAN_H_ */