integers.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * integers.h
  3. *
  4. * defines integer types (or refers to their definitions)
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2017, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifndef INTEGERS_H
  45. #define INTEGERS_H
  46. /* use standard integer definitions, if they're available */
  47. #ifdef HAVE_STDLIB_H
  48. #include <stdlib.h>
  49. #endif
  50. #ifdef HAVE_STDINT_H
  51. #include <stdint.h>
  52. #endif
  53. #ifdef HAVE_INTTYPES_H
  54. #include <inttypes.h>
  55. #endif
  56. #ifdef HAVE_SYS_TYPES_H
  57. #include <sys/types.h>
  58. #endif
  59. #ifdef HAVE_SYS_INT_TYPES_H
  60. #include <sys/int_types.h> /* this exists on Sun OS */
  61. #endif
  62. #ifdef HAVE_MACHINE_TYPES_H
  63. #include <machine/types.h>
  64. #endif
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. /* Can we do 64 bit integers? */
  69. #if !defined(HAVE_UINT64_T)
  70. #if SIZEOF_UNSIGNED_LONG == 8
  71. typedef unsigned long uint64_t;
  72. #elif SIZEOF_UNSIGNED_LONG_LONG == 8
  73. typedef unsigned long long uint64_t;
  74. #else
  75. #define NO_64BIT_MATH 1
  76. #endif
  77. #endif
  78. /* Reasonable defaults for 32 bit machines - you may need to
  79. * edit these definitions for your own machine. */
  80. #ifndef HAVE_UINT8_T
  81. typedef unsigned char uint8_t;
  82. #endif
  83. #ifndef HAVE_UINT16_T
  84. typedef unsigned short int uint16_t;
  85. #endif
  86. #ifndef HAVE_UINT32_T
  87. typedef unsigned int uint32_t;
  88. #endif
  89. #ifndef HAVE_INT32_T
  90. typedef int int32_t;
  91. #endif
  92. #if defined(NO_64BIT_MATH) && defined(HAVE_CONFIG_H)
  93. typedef double uint64_t;
  94. /* assert that sizeof(double) == 8 */
  95. extern uint64_t make64(uint32_t high, uint32_t low);
  96. extern uint32_t high32(uint64_t value);
  97. extern uint32_t low32(uint64_t value);
  98. #endif
  99. /* These macros are to load and store 32-bit values from un-aligned
  100. addresses. This is required for processors that do not allow unaligned
  101. loads. */
  102. #ifdef ALIGNMENT_32BIT_REQUIRED
  103. /* Note that if it's in a variable, you can memcpy it */
  104. #ifdef WORDS_BIGENDIAN
  105. #define PUT_32(addr, value) \
  106. { \
  107. ((unsigned char *)(addr))[0] = (value >> 24); \
  108. ((unsigned char *)(addr))[1] = (value >> 16) & 0xff; \
  109. ((unsigned char *)(addr))[2] = (value >> 8) & 0xff; \
  110. ((unsigned char *)(addr))[3] = (value)&0xff; \
  111. }
  112. #define GET_32(addr) \
  113. ((((unsigned char *)(addr))[0] << 24) | \
  114. (((unsigned char *)(addr))[1] << 16) | \
  115. (((unsigned char *)(addr))[2] << 8) | (((unsigned char *)(addr))[3]))
  116. #else
  117. #define PUT_32(addr, value) \
  118. { \
  119. ((unsigned char *)(addr))[3] = (value >> 24); \
  120. ((unsigned char *)(addr))[2] = (value >> 16) & 0xff; \
  121. ((unsigned char *)(addr))[1] = (value >> 8) & 0xff; \
  122. ((unsigned char *)(addr))[0] = (value)&0xff; \
  123. }
  124. #define GET_32(addr) \
  125. ((((unsigned char *)(addr))[3] << 24) | \
  126. (((unsigned char *)(addr))[2] << 16) | \
  127. (((unsigned char *)(addr))[1] << 8) | (((unsigned char *)(addr))[0]))
  128. #endif // WORDS_BIGENDIAN
  129. #else
  130. #define PUT_32(addr, value) *(((uint32_t *) (addr)) = (value)
  131. #define GET_32(addr) (*(((uint32_t *) (addr)))
  132. #endif
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* INTEGERS_H */