2 /* ----------------------------------------------------------------------- *
4 * Copyright 2001-2004 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
9 * USA; either version 2 of the License, or (at your option) any later
10 * version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
17 * Basic operations on unaligned, littleendian integers
25 /* These are unaligned, littleendian integer types */
27 typedef uint8_t le8_t; /* 8-bit byte */
28 typedef uint8_t le16_t[2]; /* 16-bit word */
29 typedef uint8_t le32_t[4]; /* 32-bit dword */
31 /* Read/write these quantities */
33 static inline unsigned char
40 write8(le8_t *_p, unsigned char _v)
45 #if defined(__i386__) || defined(__x86_64__)
47 /* Littleendian architectures which support unaligned memory accesses */
49 static inline unsigned short
52 return *((unsigned short *)_p);
56 write16(le16_t *_p, unsigned short _v)
58 *((unsigned short *)_p) = _v;
61 static inline unsigned int
64 return *((unsigned int *)_p);
68 write32(le32_t *_p, unsigned int _v)
70 *((unsigned int *)_p) = _v;
75 /* Generic, mostly portable versions */
77 static inline unsigned short
88 write16(le16_t *_p, unsigned short _v)
91 _p[1] = (_v >> 8) & 0xFF;
94 static inline unsigned int
105 write32(le32_t *_p, unsigned int _v)
108 _p[1] = (_v >> 8) & 0xFF;
109 _p[2] = (_v >> 16) & 0xFF;
110 _p[3] = (_v >> 24) & 0xFF;