Yet Another eXchange Tool  0.9.0
xt_arithmetic_util.h
Go to the documentation of this file.
1 
12 /*
13  * Keywords:
14  * Maintainer: Jörg Behrens <behrens@dkrz.de>
15  * Moritz Hanke <hanke@dkrz.de>
16  * Thomas Jahns <jahns@dkrz.de>
17  * URL: https://doc.redmine.dkrz.de/yaxt/html/
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions are
21  * met:
22  *
23  * Redistributions of source code must retain the above copyright notice,
24  * this list of conditions and the following disclaimer.
25  *
26  * Redistributions in binary form must reproduce the above copyright
27  * notice, this list of conditions and the following disclaimer in the
28  * documentation and/or other materials provided with the distribution.
29  *
30  * Neither the name of the DKRZ GmbH nor the names of its contributors
31  * may be used to endorse or promote products derived from this software
32  * without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
38  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  */
46 #ifndef XT_ARITHMETIC_UTIL_H
47 #define XT_ARITHMETIC_UTIL_H
48 
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 
53 #include <limits.h>
54 
55 #include "xt/xt_core.h"
56 
57 /* simple operations on Xt_int */
61 static inline Xt_int
63 {
64 #if (-1 >> 1) == -1
65  return ((x >> (sizeof (Xt_int) * CHAR_BIT - 1)) |
66  (Xt_int)((Xt_uint)(~x) >> (sizeof (Xt_int) * CHAR_BIT - 1)));
67 #else
68  return (Xt_int)((x >= 0) - (x < 0));
69 #endif
70 }
71 
75 static inline int
76 isign(int x)
77 {
78 #if (-1 >> 1) == -1
79  return ((x >> (sizeof (x) * CHAR_BIT - 1)) |
80  (int)((unsigned int)(~x) >> (sizeof (x) * CHAR_BIT - 1)));
81 #else
82  return (x >= 0) - (x < 0);
83 #endif
84 }
85 
89 static inline int
90 isign_mask(int x)
91 {
92 #if (-1 >> 1) == -1
93  return x >> (sizeof (int) * CHAR_BIT - 1);
94 #else
95 #warning Unusual behaviour of shift operator detected.
96  return (x < 0) * ~0;
97 #endif
98 }
99 
100 
104 static inline Xt_int
106 {
107 #if (-1 >> 1) == -1
108  return x >> (sizeof (x) * CHAR_BIT - 1);
109 #else
110 #warning Unusual behaviour of shift operator detected.
111  return (x < 0) * ~(Xt_int)0;
112 #endif
113 }
114 
118 static inline long long
119 llsign(long long x)
120 {
121 #if (-1 >> 1) == -1
122  return ((x >> (sizeof (x) * CHAR_BIT - 1)) |
123  (long long)((unsigned long long)(~x) >> (sizeof (x) * CHAR_BIT - 1)));
124 #else
125  return (x >= 0) - (x < 0);
126 #endif
127 }
128 
132 static inline long long
133 llsign_mask(long long x)
134 {
135 #if (-1 >> 1) == -1
136  return x >> (sizeof (x) * CHAR_BIT - 1);
137 #else
138 #warning Unusual behaviour of shift operator detected.
139  return (x < 0) * ~0LL;
140 #endif
141 }
142 
146 static inline int
147 imin(int a, int b)
148 {
149  return a <= b ? a : b;
150 }
151 
152 /* return number of leading zeroes in an Xt_uint */
153 #ifdef XT_INT_CLZ
154 #define xinlz(v) XT_INT_CLZ(v)
155 #else
156 static inline int
158 {
159  int c = 0;
160 #if SIZEOF_XT_INT * CHAR_BIT == 64
161  if (v <= UINT64_C(0x00000000ffffffff)) {c += 32; v <<= 32;}
162  if (v <= UINT64_C(0x0000ffffffffffff)) {c += 16; v <<= 16;}
163  if (v <= UINT64_C(0x00ffffffffffffff)) {c += 8; v <<= 8;}
164  if (v <= UINT64_C(0x0fffffffffffffff)) {c += 4; v <<= 4;}
165  if (v <= UINT64_C(0x3fffffffffffffff)) {c += 2; v <<= 2;}
166  if (v <= UINT64_C(0x7fffffffffffffff)) {c += 1;}
167 #elif SIZEOF_XT_INT * CHAR_BIT == 32
168  if (v <= 0x0000ffffUL) {c += 16; v <<= 16;}
169  if (v <= 0x00ffffffUL) {c += 8; v <<= 8;}
170  if (v <= 0x0fffffffUL) {c += 4; v <<= 4;}
171  if (v <= 0x3fffffffUL) {c += 2; v <<= 2;}
172  if (v <= 0x7fffffffUL) {c += 1;}
173 #elif SIZEOF_XT_INT * CHAR_BIT == 16
174  if (v <= 0x00ffU) {c += 8; v <<= 8;}
175  if (v <= 0x0fffU) {c += 4; v <<= 4;}
176  if (v <= 0x3fffU) {c += 2; v <<= 2;}
177  if (v <= 0x7fffU) {c += 1;}
178 #else
179 #error "Unexpected size of Xt_int.\n"
180 #endif
181  return c;
182 }
183 #endif
184 
185 
186 
187 #endif
188 
189 /*
190  * Local Variables:
191  * c-basic-offset: 2
192  * coding: utf-8
193  * indent-tabs-mode: nil
194  * show-trailing-whitespace: t
195  * require-trailing-newline: t
196  * End:
197  */
static int isign(int x)
static int isign_mask(int x)
static Xt_int Xt_isign_mask(Xt_int x)
static int xinlz(Xt_uint v)
static int imin(int a, int b)
static Xt_int Xt_isign(Xt_int x)
static long long llsign(long long x)
static long long llsign_mask(long long x)
base definitions header file
XT_INT Xt_int
Definition: xt_core.h:68
unsigned XT_INT Xt_uint
Definition: xt_core.h:70