1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
//------------------------------------------------------------------------------
// GB_bitwise.h: definitions for bitwise operators
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
#ifndef GB_BITWISE_H
#define GB_BITWISE_H
//------------------------------------------------------------------------------
// bitget, bitset, bitclr
//------------------------------------------------------------------------------
// bitget (x,k) returns a single bit from x, as 0 or 1, whose position is given
// by k. k = 1 is the least significant bit, and k = bits (64 for uint64)
// is the most significant bit. If k is outside this range, the result is zero.
#define GB_BITGET(x,k,type,bits) \
( \
(k >= 1 && k <= bits) ? \
( \
/* get the kth bit */ \
((x & (((type) 1) << (k-1))) ? 1 : 0) \
) \
: \
( \
0 \
) \
)
// bitset (x,k) returns x modified by setting a bit from x to 1, whose position
// is given by k. If k is in the range 1 to bits, then k gives the position
// of the bit to set. If k is outside the range 1 to bits, then z = x is
// returned, unmodified.
#define GB_BITSET(x,k,type,bits) \
( \
(k >= 1 && k <= bits) ? \
( \
/* set the kth bit to 1 */ \
(x | (((type) 1) << (k-1))) \
) \
: \
( \
x \
) \
)
// bitclr (x,k) returns x modified by setting a bit from x to 0, whose position
// is given by k. If k is in the range 1 to bits, then k gives the position of
// the bit to clear. If k is outside the range 1 to GB_BITS, then z = x is
// returned, unmodified.
#define GB_BITCLR(x,k,type,bits) \
( \
(k >= 1 && k <= bits) ? \
( \
/* set the kth bit to 0 */ \
(x & ~(((type) 1) << (k-1))) \
) \
: \
( \
x \
) \
)
//------------------------------------------------------------------------------
// z = bitshift (x,y) when x and z are unsigned
//------------------------------------------------------------------------------
inline uint8_t GB_bitshift_uint8 (uint8_t x, int8_t k)
{
if (k == 0)
{
// no shift to do at all
return (x) ;
}
else if (k >= 8 || k <= -8)
{
// ANSI C11 states that the result of x << k is undefined if k is
// negative or if k is greater than the # of bits in x. Here, the
// result is defined to be zero (the same as if shifting left
// or right by 8).
return (0) ;
}
else if (k > 0)
{
// left shift x by k bits. z is defined by ANSI C11 as
// (x * (2^k)) mod (uintmax + 1).
return (x << k) ;
}
else
{
k = -k ;
// right shift x by k bits. z is defined by ANSI C11 as the
// integral part of the quotient of x / (2^k).
return (x >> k) ;
}
}
inline uint16_t GB_bitshift_uint16 (uint16_t x, int8_t k)
{
if (k == 0)
{
// no shift
return (x) ;
}
else if (k >= 16 || k <= -16)
{
return (0) ;
}
else if (k > 0)
{
// left shift
return (x << k) ;
}
else
{
// right shift
return (x >> (-k)) ;
}
}
inline uint32_t GB_bitshift_uint32 (uint32_t x, int8_t k)
{
if (k == 0)
{
// no shift
return (x) ;
}
else if (k >= 32 || k <= -32)
{
return (0) ;
}
else if (k > 0)
{
// left shift
return (x << k) ;
}
else
{
// right shift
return (x >> (-k)) ;
}
}
inline uint64_t GB_bitshift_uint64 (uint64_t x, int8_t k)
{
if (k == 0)
{
// no shift
return (x) ;
}
else if (k >= 64 || k <= -64)
{
return (0) ;
}
else if (k > 0)
{
// left shift
return (x << k) ;
}
else
{
// right shift
return (x >> (-k)) ;
}
}
//------------------------------------------------------------------------------
// z = bitshift (x,y) when x and z are signed
//------------------------------------------------------------------------------
inline int8_t GB_bitshift_int8 (int8_t x, int8_t k)
{
if (k == 0)
{
// no shift to do at all
return (x) ;
}
else if (k >= 8)
{
// ANSI C11 states that z = x << k is undefined if k is greater
// than the # of bits in x. Here, the result is defined to be zero.
return (0) ;
}
else if (k <= -8)
{
// ANSI C11 states that z = x >> (-k) is undefined if (-k) is
// greater than the # of bits in x. Here, the result is defined to
// be the sign of x (z = 0 if x >= 0 and z = -1 if x is negative).
return ((x >= 0) ? 0 : -1) ;
}
else if (k > 0)
{
// left shift x by k bits (where k is in range 1 to #bits - 1).
// ANSI C11 states that z is defined only if x is non-negative and
// x * (2^k) is representable. This computation assumes x and z
// are represented in 2's complement. The result depends on the
// underlying machine architecture and the compiler.
return (x << k) ;
}
else
{
k = -k ;
// right shift x by k bits (where k is in range 1 to 8)
if (x >= 0)
{
// ANSI C11 defines z as the integral part of the quotient
// of x / (2^k).
return (x >> k) ;
}
else
{
// ANSI C11 states that the result is implementation-defined if
// x is negative. This computation assumes x and z are in 2's
// complement, so 1-bits are shifted in on the left, and thus
// the sign bit is always preserved. The result depends on the
// underlying machine architecture and the compiler.
return ((x >> k) | (~(UINT8_MAX >> k))) ;
}
}
}
inline int16_t GB_bitshift_int16 (int16_t x, int8_t k)
{
if (k == 0)
{
// no shift
return (x) ;
}
else if (k >= 16)
{
return (0) ;
}
else if (k <= -16)
{
return ((x >= 0) ? 0 : -1) ;
}
else if (k > 0)
{
// left shift
return (x << k) ;
}
else
{
// right shift
k = -k ;
if (x >= 0)
{
return (x >> k) ;
}
else
{
return ((x >> k) | (~(UINT16_MAX >> k))) ;
}
}
}
inline int32_t GB_bitshift_int32 (int32_t x, int8_t k)
{
if (k == 0)
{
// no shift
return (x) ;
}
else if (k >= 32)
{
return (0) ;
}
else if (k <= -32)
{
return ((x >= 0) ? 0 : -1) ;
}
else if (k > 0)
{
// left shift
return (x << k) ;
}
else
{
// right shift
k = -k ;
if (x >= 0)
{
return (x >> k) ;
}
else
{
return ((x >> k) | (~(UINT32_MAX >> k))) ;
}
}
}
inline int64_t GB_bitshift_int64 (int64_t x, int8_t k)
{
if (k == 0)
{
// no shift
return (x) ;
}
else if (k >= 64)
{
return (0) ;
}
else if (k <= -64)
{
return ((x >= 0) ? 0 : -1) ;
}
else if (k > 0)
{
// left shift
return (x << k) ;
}
else
{
// right shift
k = -k ;
if (x >= 0)
{
return (x >> k) ;
}
else
{
return ((x >> k) | (~(UINT64_MAX >> k))) ;
}
}
}
#endif
|