syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / libfat / ulint.h
1 #ident "$Id: ulint.h,v 1.3 2004/12/20 04:57:44 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
3  *   
4  *   Copyright 2001-2004 H. Peter Anvin - All Rights Reserved
5  *
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.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * ulint.h
16  *
17  * Basic operations on unaligned, littleendian integers 
18  */
19
20 #ifndef ULINT_H
21 #define ULINT_H
22
23 #include <inttypes.h>
24
25 /* These are unaligned, littleendian integer types */
26
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 */
30
31 /* Read/write these quantities */
32
33 static inline unsigned char
34 read8(le8_t *_p)
35 {
36   return *_p;
37 }
38
39 static inline void
40 write8(le8_t *_p, uint8_t _v)
41 {
42   *_p = _v;
43 }
44
45 #if defined(__i386__) || defined(__x86_64__)
46
47 /* Littleendian architectures which support unaligned memory accesses */
48
49 static inline unsigned short
50 read16(le16_t *_p)
51 {
52   return *((const uint16_t *)_p);
53 }
54
55 static inline void
56 write16(le16_t *_p, unsigned short _v)
57 {
58   *((uint16_t *)_p) = _v;
59 }
60
61 static inline unsigned int
62 read32(le32_t *_p)
63 {
64   return *((const uint32_t *)_p);
65 }
66
67 static inline void
68 write32(le32_t *_p, uint32_t _v)
69 {
70   *((uint32_t *)_p) = _v;
71 }
72
73 #else 
74
75 /* Generic, mostly portable versions */
76
77 static inline unsigned short
78 read16(le16_t *_p)
79 {
80   uint16_t _v;
81
82   _v  = p[0];
83   _v |= p[1] << 8;
84   return _v;
85 }
86
87 static inline void
88 write16(le16_t *_p, uint16_t _v)
89 {
90   _p[0] = _v & 0xFF;
91   _p[1] = (_v >> 8) & 0xFF;
92 }
93
94 static inline unsigned int
95 read32(le32_t *_p)
96 {
97   _v  = _p[0];
98   _v |= _p[1] << 8;
99   _v |= _p[2] << 16;
100   _v |= _p[3] << 24;
101   return _v;
102 }
103
104 static inline void
105 write32(le32_t *_p, uint32_t _v)
106 {
107   _p[0] = _v & 0xFF;
108   _p[1] = (_v >> 8) & 0xFF;
109   _p[2] = (_v >> 16) & 0xFF;
110   _p[3] = (_v >> 24) & 0xFF;
111 }
112
113 #endif
114
115 #endif /* ULINT_H */