integers.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* integers.h: Definitions for integers with arbitrary endianness
  2. Copyright (C) 2002-2003 Sebastian Reichelt
  3. Copyright (C) 2007 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #ifndef INTEGERS_H
  16. #define INTEGERS_H
  17. #include "generic.h"
  18. #define TARGET_BIG_ENDIAN
  19. // *** Zero Integer Definitions ***
  20. typedef I1 ZI1;
  21. typedef I2 ZI2;
  22. typedef I4 ZI4;
  23. #define IsZero(I) (!(I))
  24. #ifdef __i386__
  25. // This only works on targets which don't care about alignment.
  26. #define IsZeroI1(I) (IsZero (*((ZI1 *) &(I))))
  27. #define IsZeroI2(I) (IsZero (*((ZI2 *) &(I))))
  28. #define IsZeroI4(I) (IsZero (*((ZI4 *) &(I))))
  29. #else
  30. // Use only bytewise reads for targets which do care about alignment.
  31. #define IsZeroI1(I) (IsZero (ReadTI1 (*(TI1*)&(I))))
  32. #define IsZeroI2(I) (IsZero (ReadTI2 (*(TI2*)&(I))))
  33. #define IsZeroI4(I) (IsZero (ReadTI4 (*(TI4*)&(I))))
  34. #endif
  35. // *** Target Integer Definitions ***
  36. // 1 Byte Target Integer
  37. // Defined as structure to avoid unguarded reading/writing.
  38. typedef struct ATTRIBUTE_PACKED {
  39. I1 Val;
  40. } TI1_struct;
  41. typedef TI1_struct ATTRIBUTE_MAY_ALIAS TI1;
  42. // 2 Byte Target Integer
  43. typedef struct ATTRIBUTE_PACKED {
  44. #ifdef TARGET_BIG_ENDIAN
  45. TI1 Hi, Lo;
  46. #else /* !TARGET_BIG_ENDIAN */
  47. TI1 Lo, Hi;
  48. #endif /* !TARGET_BIG_ENDIAN */
  49. } TI2_struct;
  50. typedef TI2_struct ATTRIBUTE_MAY_ALIAS TI2;
  51. // 4 Byte Target Integer
  52. typedef struct ATTRIBUTE_PACKED {
  53. #ifdef TARGET_BIG_ENDIAN
  54. TI2 Hi, Lo;
  55. #else /* !TARGET_BIG_ENDIAN */
  56. TI2 Lo, Hi;
  57. #endif /* !TARGET_BIG_ENDIAN */
  58. } TI4_struct;
  59. typedef TI4_struct ATTRIBUTE_MAY_ALIAS TI4;
  60. // *** Host Integer Definitions ***
  61. // 1 Byte Host Integer
  62. // Defined as structure to avoid unguarded reading/writing.
  63. typedef struct ATTRIBUTE_PACKED {
  64. unsigned char Val;
  65. } HI1_struct;
  66. typedef HI1_struct ATTRIBUTE_MAY_ALIAS HI1;
  67. // 2 Byte Host Integer
  68. typedef struct ATTRIBUTE_PACKED {
  69. #ifdef HOST_BIG_ENDIAN
  70. HI1 Hi, Lo;
  71. #else /* !HOST_BIG_ENDIAN */
  72. HI1 Lo, Hi;
  73. #endif /* !HOST_BIG_ENDIAN */
  74. } HI2_struct;
  75. typedef HI2_struct ATTRIBUTE_MAY_ALIAS HI2;
  76. // 4 Byte Host Integer
  77. typedef struct ATTRIBUTE_PACKED {
  78. #ifdef HOST_BIG_ENDIAN
  79. HI2 Hi, Lo;
  80. #else /* !HOST_BIG_ENDIAN */
  81. HI2 Lo, Hi;
  82. #endif /* !HOST_BIG_ENDIAN */
  83. } HI4_struct;
  84. typedef HI4_struct ATTRIBUTE_MAY_ALIAS HI4;
  85. // *** Macros to read arbitrary integers ***
  86. #define ReadI1(I) ((I).Val)
  87. #define ReadI2(I) ((ReadI1 ((I).Hi) << 8) | ReadI1 ((I).Lo))
  88. #define ReadI4(I) ((ReadI2 ((I).Hi) << 16) | ReadI2 ((I).Lo))
  89. // *** Functions to read integers ***
  90. #define DEFINE_INT_SIZE(size) I##size ReadTI##size (const TI##size I); I##size ReadHI##size (const HI##size I);
  91. #include "int_def.inc"
  92. #undef DEFINE_INT_SIZE
  93. // *** Macros to write arbitrary integers ***
  94. #define WriteI1(I,V) ((I).Val = (V))
  95. #define WriteI2(I,V) ({ register I2 __V2 = (V); WriteI1 ((I).Hi, (__V2) >> 8); WriteI1 ((I).Lo, (__V2)); })
  96. #define WriteI4(I,V) ({ register I4 __V4 = (V); WriteI2 ((I).Hi, (__V4) >> 16); WriteI2 ((I).Lo, (__V4)); })
  97. // *** Functions to write integers ***
  98. #define DEFINE_INT_SIZE(size) void WriteTI##size##_ (TI##size *I, I##size V); void WriteHI##size##_ (HI##size *I, I##size V);
  99. #include "int_def.inc"
  100. #undef DEFINE_INT_SIZE
  101. #define WriteTI1(I,V) (WriteTI1_ (&(I), (V)))
  102. #define WriteTI2(I,V) (WriteTI2_ (&(I), (V)))
  103. #define WriteTI4(I,V) (WriteTI4_ (&(I), (V)))
  104. #define WriteHI1(I,V) (WriteHI1_ (&(I), (V)))
  105. #define WriteHI2(I,V) (WriteHI2_ (&(I), (V)))
  106. #define WriteHI4(I,V) (WriteHI4_ (&(I), (V)))
  107. // *** Functions to read arbitrary-length target integers ***
  108. // Read an unsigned target integer of specified size from a
  109. // data buffer.
  110. IMAX ReadTI (const void *Data, SIZE Size);
  111. // Read a signed target integer of specified size from a
  112. // data buffer.
  113. SIMAX ReadSTI (const void *Data, SIZE Size);
  114. // *** Functions to write arbitrary-length target integers ***
  115. // Write a target integer of specified size to a data buffer.
  116. // The integer may be interpreted as a signed or unsigned
  117. // value based on the AllowSigned and AllowUnsigned parameters.
  118. // If the value does not fit into the contents, FALSE is
  119. // returned.
  120. BOOLEAN WriteTI (void *Data, SIZE Size, SIMAX Value, BOOLEAN AllowSigned, BOOLEAN AllowUnsigned);
  121. // Add a target integer of specified size to a data buffer.
  122. // The integer may be interpreted as a signed or unsigned
  123. // value based on the AllowSigned and AllowUnsigned parameters.
  124. // If the value does not fit into the contents, FALSE is
  125. // returned.
  126. BOOLEAN AddTI (void *Data, SIZE Size, SIMAX Value, BOOLEAN AllowSigned, BOOLEAN AllowUnsigned);
  127. #endif