Tizen 2.0 Release
[external/tizen-coreutils.git] / lib / u64.h
1 /* uint64_t-like operations that work even on hosts lacking uint64_t
2
3    Copyright (C) 2006 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by the
7    Free Software Foundation; either version 2, or (at your option) any
8    later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Paul Eggert.  */
20
21 #include <stddef.h>
22 #include <stdint.h>
23
24 /* Return X rotated left by N bits, where 0 < N < 64.  */
25 #define u64rol(x, n) u64or (u64shl (x, n), u64shr (x, 64 - n))
26
27 #ifdef UINT64_MAX
28
29 /* Native implementations are trivial.  See below for comments on what
30    these operations do.  */
31 typedef uint64_t u64;
32 # define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo)))
33 # define u64init(hi, lo) u64hilo (hi, lo)
34 # define u64lo(x) ((u64) (x))
35 # define u64lt(x, y) ((x) < (y))
36 # define u64and(x, y) ((x) & (y))
37 # define u64or(x, y) ((x) | (y))
38 # define u64xor(x, y) ((x) ^ (y))
39 # define u64plus(x, y) ((x) + (y))
40 # define u64shl(x, n) ((x) << (n))
41 # define u64shr(x, n) ((x) >> (n))
42
43 #else
44
45 /* u64 is a 64-bit unsigned integer value.
46    u64init (HI, LO), is like u64hilo (HI, LO), but for use in
47    initializer contexts.  */
48 # ifdef WORDS_BIGENDIAN
49 typedef struct { uint32_t hi, lo; } u64;
50 #  define u64init(hi, lo) { hi, lo }
51 # else
52 typedef struct { uint32_t lo, hi; } u64;
53 #  define u64init(hi, lo) { lo, hi }
54 # endif
55
56 /* Given the high and low-order 32-bit quantities HI and LO, return a u64
57    value representing (HI << 32) + LO.  */
58 static inline u64
59 u64hilo (uint32_t hi, uint32_t lo)
60 {
61   u64 r;
62   r.hi = hi;
63   r.lo = lo;
64   return r;
65 }
66
67 /* Return a u64 value representing LO.  */
68 static inline u64
69 u64lo (uint32_t lo)
70 {
71   u64 r;
72   r.hi = 0;
73   r.lo = lo;
74   return r;
75 }
76
77 /* Return X < Y.  */
78 static inline int
79 u64lt (u64 x, u64 y)
80 {
81   return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo);
82 }
83
84 /* Return X & Y.  */
85 static inline u64
86 u64and (u64 x, u64 y)
87 {
88   u64 r;
89   r.hi = x.hi & y.hi;
90   r.lo = x.lo & y.lo;
91   return r;
92 }
93
94 /* Return X | Y.  */
95 static inline u64
96 u64or (u64 x, u64 y)
97 {
98   u64 r;
99   r.hi = x.hi | y.hi;
100   r.lo = x.lo | y.lo;
101   return r;
102 }
103
104 /* Return X ^ Y.  */
105 static inline u64
106 u64xor (u64 x, u64 y)
107 {
108   u64 r;
109   r.hi = x.hi ^ y.hi;
110   r.lo = x.lo ^ y.lo;
111   return r;
112 }
113
114 /* Return X + Y.  */
115 static inline u64
116 u64plus (u64 x, u64 y)
117 {
118   u64 r;
119   r.lo = x.lo + y.lo;
120   r.hi = x.hi + y.hi + (r.lo < x.lo);
121   return r;
122 }
123
124 /* Return X << N.  */
125 static inline u64
126 u64shl (u64 x, int n)
127 {
128   u64 r;
129   if (n < 32)
130     {
131       r.hi = (x.hi << n) | (x.lo >> (32 - n));
132       r.lo = x.lo << n;
133     }
134   else
135     {
136       r.hi = x.lo << (n - 32);
137       r.lo = 0;
138     }
139   return r;
140 }
141
142 /* Return X >> N.  */
143 static inline u64
144 u64shr (u64 x, int n)
145 {
146   u64 r;
147   if (n < 32)
148     {
149       r.hi = x.hi >> n;
150       r.lo = (x.hi << (32 - n)) | (x.lo >> n);
151     }
152   else
153     {
154       r.hi = 0;
155       r.lo = x.hi >> (n - 32);
156     }
157   return r;
158 }
159
160 #endif