Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
8.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: Align.hxx
  7. //
  8. // Contents: Alignment routines (for MIPS)
  9. //
  10. // History: 14-Jun-93 KyleP Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #if !defined( __ALIGN_HXX__ )
  14. #define __ALIGN_HXX__
  15. //+-------------------------------------------------------------------------
  16. //
  17. // Function: AlignWCHAR, public
  18. //
  19. // Synopsis: Half-word aligns argument
  20. //
  21. // Arguments: [pb] -- Pointer to half-word align
  22. // [cbAlign] -- # Bytes moved to satisfy alignment
  23. //
  24. // Returns: [pb] incremented forward as necessary for half-word alignment
  25. //
  26. // History: 14-Jun-93 KyleP Created
  27. //
  28. //--------------------------------------------------------------------------
  29. inline unsigned short * AlignWCHAR( unsigned char * pb,
  30. unsigned * pcbAlign = 0 );
  31. inline unsigned short * AlignUSHORT( unsigned char * pb,
  32. unsigned * pcbAlign = 0 );
  33. inline unsigned short * AlignWCHAR( unsigned char * pb,
  34. unsigned * pcbAlign )
  35. {
  36. return( AlignUSHORT( pb, pcbAlign ) );
  37. }
  38. inline unsigned short * AlignUSHORT( unsigned char * pb,
  39. unsigned * pcbAlign )
  40. {
  41. if ( pcbAlign )
  42. *pcbAlign = (unsigned)((sizeof(unsigned short) -
  43. ((LONG_PTR)pb & (sizeof(unsigned short) - 1))) %
  44. sizeof(unsigned short));
  45. return( (unsigned short *)( ((LONG_PTR)pb + sizeof(unsigned short) - 1) &
  46. ~((LONG_PTR)sizeof(unsigned short) - 1 ) ) );
  47. }
  48. //+-------------------------------------------------------------------------
  49. //
  50. // Function: AlignULONG, public
  51. //
  52. // Synopsis: Word aligns argument
  53. //
  54. // Arguments: [pb] -- Pointer to word align
  55. // [cbAlign] -- # Bytes moved to satisfy alignment
  56. //
  57. // Returns: [pb] incremented forward as necessary for word alignment
  58. //
  59. // History: 14-Jun-93 KyleP Created
  60. //
  61. //--------------------------------------------------------------------------
  62. inline unsigned long * AlignULONG( unsigned char * pb,
  63. unsigned * pcbAlign = 0 );
  64. inline unsigned long * AlignULONG( unsigned char * pb,
  65. unsigned * pcbAlign )
  66. {
  67. if ( pcbAlign )
  68. *pcbAlign = (unsigned)((sizeof(unsigned long) -
  69. ((LONG_PTR)pb & (sizeof(unsigned long) - 1))) %
  70. sizeof(unsigned long));
  71. return( (unsigned long *)( ((LONG_PTR)pb + sizeof(unsigned long) - 1) &
  72. ~((LONG_PTR)sizeof(unsigned long) - 1 ) ) );
  73. }
  74. inline long * AlignLong( unsigned char * pb,
  75. unsigned * pcbAlign = 0 );
  76. inline long * AlignLong( unsigned char * pb,
  77. unsigned * pcbAlign )
  78. {
  79. if ( pcbAlign )
  80. *pcbAlign = (unsigned)((sizeof(long) -
  81. ((LONG_PTR)pb & (sizeof(long) - 1))) %
  82. sizeof(long));
  83. return( (long *)( ((LONG_PTR)pb + sizeof(long) - 1) &
  84. ~((LONG_PTR)sizeof(long) - 1 ) ) );
  85. }
  86. inline LONGLONG * AlignLONGLONG( unsigned char * pb,
  87. unsigned * pcbAlign = 0 );
  88. inline LONGLONG * AlignLONGLONG( unsigned char * pb,
  89. unsigned * pcbAlign )
  90. {
  91. if ( pcbAlign )
  92. *pcbAlign = (unsigned)((sizeof(LONGLONG) -
  93. ((LONG_PTR)pb & (sizeof(LONGLONG) - 1))) %
  94. sizeof(LONGLONG));
  95. return( (LONGLONG *)( ((LONG_PTR)pb + sizeof(LONGLONG) - 1) &
  96. ~((LONG_PTR)sizeof(LONGLONG) - 1 ) ) );
  97. }
  98. inline float * AlignFloat( unsigned char * pb,
  99. unsigned * pcbAlign = 0 );
  100. inline float * AlignFloat( unsigned char * pb,
  101. unsigned * pcbAlign )
  102. {
  103. if ( pcbAlign )
  104. *pcbAlign = (unsigned)((sizeof(float) -
  105. ((LONG_PTR)pb & (sizeof(float) - 1))) %
  106. sizeof(float));
  107. return( (float *)( ((LONG_PTR)pb + sizeof(float) - 1) &
  108. ~((LONG_PTR)sizeof(float) - 1 ) ) );
  109. }
  110. inline double * AlignDouble( unsigned char * pb,
  111. unsigned * pcbAlign = 0 );
  112. inline double * AlignDouble( unsigned char * pb,
  113. unsigned * pcbAlign )
  114. {
  115. if ( pcbAlign )
  116. *pcbAlign = (unsigned)((sizeof(double) -
  117. ((LONG_PTR)pb & (sizeof(double) - 1))) %
  118. sizeof(double));
  119. return( (double *)( ((LONG_PTR)pb + sizeof(double) - 1) &
  120. ~((LONG_PTR)sizeof(double) - 1 ) ) );
  121. }
  122. //+-------------------------------------------------------------------------
  123. //
  124. // Function: AlignGUID, public
  125. //
  126. // Synopsis: 8-byte aligns argument
  127. //
  128. // Arguments: [pb] -- Pointer to word align
  129. // [cbAlign] -- # Bytes moved to satisfy alignment
  130. //
  131. // Returns: [pb] incremented forward as necessary for word alignment
  132. //
  133. // History: 29-Sep-93 KyleP Created
  134. //
  135. // Notes: This is somewhat error-prone code. 16-byte GUIDs are
  136. // only 8-byte aligned, so we use sizeof(double) and not
  137. // sizeof(GUID).
  138. //
  139. //--------------------------------------------------------------------------
  140. inline unsigned char * AlignGUID( unsigned char * pb,
  141. unsigned * pcbAlign = 0 );
  142. inline unsigned char * AlignGUID( unsigned char * pb,
  143. unsigned * pcbAlign )
  144. {
  145. if ( pcbAlign )
  146. *pcbAlign = (unsigned)((sizeof(double) -
  147. ((LONG_PTR)pb & (sizeof(double) - 1))) %
  148. sizeof(double));
  149. return( (unsigned char *)( ((LONG_PTR)pb + sizeof(double) - 1) &
  150. ~((LONG_PTR)sizeof(double) - 1 ) ) );
  151. }
  152. //+-------------------------------------------------------------------------
  153. //
  154. // Function: StoreWCHAR, public
  155. //
  156. // Synopsis: Stores half word in byte-aligned memory
  157. //
  158. // Arguments: [pb] -- Buffer
  159. // [hw] -- Half-word
  160. //
  161. // Returns: Number of bytes to increment [pb].
  162. //
  163. // History: 14-Jun-93 KyleP Created
  164. //
  165. //--------------------------------------------------------------------------
  166. inline int StoreUSHORT( unsigned char * pb, unsigned short hw )
  167. {
  168. *((UNALIGNED unsigned short *)pb) = hw;
  169. return( sizeof( unsigned short ) );
  170. }
  171. inline int StoreWCHAR( unsigned char * pb, unsigned short hw )
  172. {
  173. return( StoreUSHORT( pb, hw ) );
  174. }
  175. //+-------------------------------------------------------------------------
  176. //
  177. // Function: StoreULONG, public
  178. //
  179. // Synopsis: Stores word in byte-aligned memory
  180. //
  181. // Arguments: [pb] -- Buffer
  182. // [w] -- Word
  183. //
  184. // Returns: Number of bytes to increment [pb].
  185. //
  186. // History: 14-Jun-93 KyleP Created
  187. //
  188. //--------------------------------------------------------------------------
  189. inline int StoreULONG( unsigned char * pb, unsigned long w )
  190. {
  191. *((UNALIGNED unsigned long *)pb) = w;
  192. return( sizeof( unsigned long ) );
  193. }
  194. inline int StoreUINT( unsigned char * pb, unsigned int w )
  195. {
  196. *((UNALIGNED unsigned int *)pb) = w;
  197. return( sizeof( int ) );
  198. }
  199. //+-------------------------------------------------------------------------
  200. //
  201. // Function: LoadWCHAR, public
  202. //
  203. // Synopsis: Loads half word from byte-aligned memory
  204. //
  205. // Arguments: [pb] -- Buffer
  206. // [hw] -- Half-word
  207. //
  208. // Returns: Number of bytes to increment [pb].
  209. //
  210. // History: 14-Jun-93 KyleP Created
  211. //
  212. //--------------------------------------------------------------------------
  213. inline int LoadUSHORT( unsigned char const * const pb, unsigned short & hw )
  214. {
  215. hw = *((UNALIGNED unsigned short const *)pb);
  216. return( sizeof( unsigned short ) );
  217. }
  218. inline int LoadWCHAR( unsigned char const * const pb, unsigned short & hw )
  219. {
  220. return( LoadUSHORT( pb, hw ) );
  221. }
  222. //+-------------------------------------------------------------------------
  223. //
  224. // Function: LoadULONG, public
  225. //
  226. // Synopsis: Loads word from byte-aligned memory
  227. //
  228. // Arguments: [pb] -- Buffer
  229. // [w] -- Word
  230. //
  231. // Returns: Number of bytes to increment [pb].
  232. //
  233. // History: 14-Jun-93 KyleP Created
  234. //
  235. //--------------------------------------------------------------------------
  236. inline int LoadULONG( unsigned char const * const pb, unsigned long & w )
  237. {
  238. w = *((UNALIGNED unsigned long const *)pb);
  239. return( sizeof( unsigned long ) );
  240. }
  241. inline int LoadUINT( unsigned char const * const pb, unsigned int & w )
  242. {
  243. w = *((UNALIGNED unsigned int const *)pb);
  244. return( sizeof( int ) );
  245. }
  246. #endif // __ALIGN_HXX__