Adding upstream version 3.20.
[syslinux-debian/hramrach.git] / libfat / ulint.h
blob76c6bdbd9a43647dce5b48a5829e2837d59609d6
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2001-2004 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
8 * USA; either version 2 of the License, or (at your option) any later
9 * version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * ulint.h
16 * Basic operations on unaligned, littleendian integers
19 #ifndef ULINT_H
20 #define ULINT_H
22 #include <inttypes.h>
24 /* These are unaligned, littleendian integer types */
26 typedef uint8_t le8_t; /* 8-bit byte */
27 typedef uint8_t le16_t[2]; /* 16-bit word */
28 typedef uint8_t le32_t[4]; /* 32-bit dword */
30 /* Read/write these quantities */
32 static inline unsigned char
33 read8(le8_t *_p)
35 return *_p;
38 static inline void
39 write8(le8_t *_p, uint8_t _v)
41 *_p = _v;
44 #if defined(__i386__) || defined(__x86_64__)
46 /* Littleendian architectures which support unaligned memory accesses */
48 static inline unsigned short
49 read16(le16_t *_p)
51 return *((const uint16_t *)_p);
54 static inline void
55 write16(le16_t *_p, unsigned short _v)
57 *((uint16_t *)_p) = _v;
60 static inline unsigned int
61 read32(le32_t *_p)
63 return *((const uint32_t *)_p);
66 static inline void
67 write32(le32_t *_p, uint32_t _v)
69 *((uint32_t *)_p) = _v;
72 #else
74 /* Generic, mostly portable versions */
76 static inline unsigned short
77 read16(le16_t *_p)
79 uint16_t _v;
81 _v = p[0];
82 _v |= p[1] << 8;
83 return _v;
86 static inline void
87 write16(le16_t *_p, uint16_t _v)
89 _p[0] = _v & 0xFF;
90 _p[1] = (_v >> 8) & 0xFF;
93 static inline unsigned int
94 read32(le32_t *_p)
96 _v = _p[0];
97 _v |= _p[1] << 8;
98 _v |= _p[2] << 16;
99 _v |= _p[3] << 24;
100 return _v;
103 static inline void
104 write32(le32_t *_p, uint32_t _v)
106 _p[0] = _v & 0xFF;
107 _p[1] = (_v >> 8) & 0xFF;
108 _p[2] = (_v >> 16) & 0xFF;
109 _p[3] = (_v >> 24) & 0xFF;
112 #endif
114 #endif /* ULINT_H */