| 1 |
/*=========================================================================== |
| 2 |
* |
| 3 |
* PUBLIC DOMAIN NOTICE |
| 4 |
* National Center for Biotechnology Information |
| 5 |
* |
| 6 |
* This software/database is a "United States Government Work" under the |
| 7 |
* terms of the United States Copyright Act. It was written as part of |
| 8 |
* the author's official duties as a United States Government employee and |
| 9 |
* thus cannot be copyrighted. This software/database is freely available |
| 10 |
* to the public for use. The National Library of Medicine and the U.S. |
| 11 |
* Government have not placed any restriction on its use or reproduction. |
| 12 |
* |
| 13 |
* Although all reasonable efforts have been taken to ensure the accuracy |
| 14 |
* and reliability of the software and data, the NLM and the U.S. |
| 15 |
* Government do not and cannot warrant the performance or results that |
| 16 |
* may be obtained by using this software or data. The NLM and the U.S. |
| 17 |
* Government disclaim all warranties, express or implied, including |
| 18 |
* warranties of performance, merchantability or fitness for any particular |
| 19 |
* purpose. |
| 20 |
* |
| 21 |
* Please cite the author in any work or product based on this material. |
| 22 |
* |
| 23 |
* =========================================================================== |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef _h_arch_impl_ |
| 28 |
#define _h_arch_impl_ |
| 29 |
|
| 30 |
#include <stdint.h> |
| 31 |
|
| 32 |
#ifndef USE_GCC_BUILTIN |
| 33 |
#define USE_GCC_BUILTIN 1 |
| 34 |
#endif |
| 35 |
|
| 36 |
#if USE_GCC_BUILTIN |
| 37 |
#include <strings.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#ifdef __cplusplus |
| 41 |
extern "C" { |
| 42 |
#endif |
| 43 |
|
| 44 |
static __inline__ |
| 45 |
int16_t uint16_lsbit ( uint16_t self ) |
| 46 |
{ |
| 47 |
return ( int16_t ) __builtin_ffs ( self ) - 1; |
| 48 |
} |
| 49 |
|
| 50 |
static __inline__ |
| 51 |
int32_t uint32_lsbit ( uint32_t self ) |
| 52 |
{ |
| 53 |
return __builtin_ffs ( self ) - 1; |
| 54 |
} |
| 55 |
|
| 56 |
static __inline__ |
| 57 |
int32_t uint64_lsbit ( uint64_t self ) |
| 58 |
{ |
| 59 |
return __builtin_ffsll ( self ) - 1; |
| 60 |
} |
| 61 |
|
| 62 |
static __inline__ |
| 63 |
int16_t uint16_msbit ( uint16_t self ) |
| 64 |
{ |
| 65 |
if ( self != 0 ) |
| 66 |
return ( int16_t ) 31 - __builtin_clz ( ( uint32_t ) self ); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
static __inline__ |
| 71 |
int32_t uint32_msbit ( uint32_t self ) |
| 72 |
{ |
| 73 |
if ( self != 0 ) |
| 74 |
return 31 - __builtin_clz ( self ); |
| 75 |
return -1; |
| 76 |
} |
| 77 |
|
| 78 |
static __inline__ |
| 79 |
int32_t uint64_msbit ( uint64_t self ) |
| 80 |
{ |
| 81 |
if ( self != 0 ) |
| 82 |
return 63 - __builtin_clzll ( self ); |
| 83 |
return -1; |
| 84 |
} |
| 85 |
|
| 86 |
typedef struct int128_t int128_t; |
| 87 |
struct int128_t |
| 88 |
{ |
| 89 |
uint64_t lo; |
| 90 |
int64_t hi; |
| 91 |
}; |
| 92 |
|
| 93 |
static __inline__ |
| 94 |
int64_t int128_hi ( const int128_t *self ) |
| 95 |
{ |
| 96 |
return self -> hi; |
| 97 |
} |
| 98 |
|
| 99 |
static __inline__ |
| 100 |
uint64_t int128_lo ( const int128_t *self ) |
| 101 |
{ |
| 102 |
return self -> lo; |
| 103 |
} |
| 104 |
|
| 105 |
static __inline__ |
| 106 |
void int128_sethi ( int128_t *self, int64_t i ) |
| 107 |
{ |
| 108 |
self -> hi = i; |
| 109 |
} |
| 110 |
|
| 111 |
static __inline__ |
| 112 |
void int128_setlo ( int128_t *self, uint64_t i ) |
| 113 |
{ |
| 114 |
self -> lo = i; |
| 115 |
} |
| 116 |
|
| 117 |
typedef struct uint128_t uint128_t; |
| 118 |
struct uint128_t |
| 119 |
{ |
| 120 |
uint64_t lo; |
| 121 |
uint64_t hi; |
| 122 |
}; |
| 123 |
|
| 124 |
static __inline__ |
| 125 |
uint64_t uint128_hi ( const uint128_t *self ) |
| 126 |
{ |
| 127 |
return self -> hi; |
| 128 |
} |
| 129 |
|
| 130 |
static __inline__ |
| 131 |
uint64_t uint128_lo ( const uint128_t *self ) |
| 132 |
{ |
| 133 |
return self -> lo; |
| 134 |
} |
| 135 |
|
| 136 |
static __inline__ |
| 137 |
void uint128_sethi ( uint128_t *self, uint64_t i ) |
| 138 |
{ |
| 139 |
self -> hi = i; |
| 140 |
} |
| 141 |
|
| 142 |
static __inline__ |
| 143 |
void uint128_setlo ( uint128_t *self, uint64_t i ) |
| 144 |
{ |
| 145 |
self -> lo = i; |
| 146 |
} |
| 147 |
|
| 148 |
static __inline |
| 149 |
void int128_add ( int128_t *self, const int128_t *i ) |
| 150 |
{ |
| 151 |
uint64_t carry = ( ( const uint32_t* ) self ) [ 0 ] + ( ( const uint32_t* ) i ) [ 0 ]; |
| 152 |
self -> hi += i -> hi; |
| 153 |
carry = ( ( const uint32_t* ) self ) [ 1 ] + ( ( const uint32_t* ) i ) [ 1 ] + ( carry >> 32 ); |
| 154 |
self -> lo += i -> lo; |
| 155 |
self -> hi += carry >> 32; |
| 156 |
} |
| 157 |
|
| 158 |
static __inline |
| 159 |
void int128_sub ( int128_t *self, const int128_t *i ) |
| 160 |
{ |
| 161 |
int carry = i -> lo > self -> lo; |
| 162 |
self -> hi -= i -> hi; |
| 163 |
self -> lo -= i -> lo; |
| 164 |
self -> hi -= carry; |
| 165 |
} |
| 166 |
|
| 167 |
static __inline |
| 168 |
void int128_sar ( int128_t *self, uint32_t i ) |
| 169 |
{ |
| 170 |
if ( i < 64 ) |
| 171 |
{ |
| 172 |
self -> lo = ( self -> hi << ( 64 - i ) ) | ( self -> lo >> i ); |
| 173 |
self -> hi >>= i; |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
self -> lo = self -> hi >> ( i - 64 ); |
| 178 |
self -> hi >>= 63; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
static __inline |
| 183 |
void int128_shl ( int128_t *self, uint32_t i ) |
| 184 |
{ |
| 185 |
if ( i < 64 ) |
| 186 |
{ |
| 187 |
self -> hi = ( self -> hi << i ) | ( int64_t ) ( self -> lo >> ( 64 - i ) ); |
| 188 |
self -> lo <<= i; |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
self -> hi = ( int64_t ) ( self -> lo << ( i - 64 ) ); |
| 193 |
self -> lo = 0; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
static __inline |
| 198 |
void uint128_and ( uint128_t *self, const uint128_t *i ) |
| 199 |
{ |
| 200 |
self -> lo &= i -> lo; |
| 201 |
self -> hi &= i -> hi; |
| 202 |
} |
| 203 |
|
| 204 |
static __inline |
| 205 |
void uint128_or ( uint128_t *self, const uint128_t *i ) |
| 206 |
{ |
| 207 |
self -> lo |= i -> lo; |
| 208 |
self -> hi |= i -> hi; |
| 209 |
} |
| 210 |
|
| 211 |
static __inline |
| 212 |
void uint128_orlo ( uint128_t *self, uint64_t i ) |
| 213 |
{ |
| 214 |
self -> lo |= i; |
| 215 |
} |
| 216 |
|
| 217 |
static __inline |
| 218 |
void uint128_xor ( uint128_t *self, const uint128_t *i ) |
| 219 |
{ |
| 220 |
self -> lo ^= i -> lo; |
| 221 |
self -> hi ^= i -> hi; |
| 222 |
} |
| 223 |
|
| 224 |
static __inline |
| 225 |
void uint128_not ( uint128_t *self ) |
| 226 |
{ |
| 227 |
self -> lo = ~ self -> lo; |
| 228 |
self -> hi = ~ self -> hi; |
| 229 |
} |
| 230 |
|
| 231 |
static __inline |
| 232 |
void uint128_shr ( uint128_t *self, uint32_t i ) |
| 233 |
{ |
| 234 |
if ( i < 64 ) |
| 235 |
{ |
| 236 |
self -> lo = ( self -> hi << ( 64 - i ) ) | ( self -> lo >> i ); |
| 237 |
self -> hi >>= i; |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
self -> lo = self -> hi >> ( i - 64 ); |
| 242 |
self -> hi >>= 63; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
static __inline |
| 247 |
void uint128_shl ( uint128_t *self, uint32_t i ) |
| 248 |
{ |
| 249 |
if ( i < 64 ) |
| 250 |
{ |
| 251 |
self -> hi = ( self -> hi << i ) | ( self -> lo >> ( 64 - i ) ); |
| 252 |
self -> lo <<= i; |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
self -> hi = self -> lo << ( i - 64 ); |
| 257 |
self -> lo = 0; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
static __inline |
| 262 |
void uint128_bswap ( uint128_t *self ) |
| 263 |
{ |
| 264 |
uint64_t tmp = __builtin_bswap64 ( self -> lo ); |
| 265 |
self -> lo = __builtin_bswap64 ( self -> hi ); |
| 266 |
( ( uint64_t* ) self ) [ 1 ] = tmp; |
| 267 |
} |
| 268 |
|
| 269 |
static __inline |
| 270 |
void uint128_bswap_copy ( uint128_t *to, const uint128_t *from ) |
| 271 |
{ |
| 272 |
to -> lo = __builtin_bswap64 ( from -> hi ); |
| 273 |
to -> hi = __builtin_bswap64 ( from -> lo ); |
| 274 |
} |
| 275 |
|
| 276 |
static __inline__ |
| 277 |
uint32_t uint32_rol ( uint32_t val, uint8_t bits ) |
| 278 |
{ |
| 279 |
uint32_t rtn; |
| 280 |
rtn = ( val << bits ) | ( val >> ( 32 - bits ) ); |
| 281 |
return rtn; |
| 282 |
} |
| 283 |
|
| 284 |
static __inline__ |
| 285 |
uint32_t uint32_ror ( uint32_t val, uint8_t bits ) |
| 286 |
{ |
| 287 |
uint32_t rtn; |
| 288 |
rtn = ( val >> bits ) | ( val << ( 32 - bits ) ); |
| 289 |
return rtn; |
| 290 |
} |
| 291 |
|
| 292 |
static __inline__ |
| 293 |
uint64_t uint64_rol ( uint64_t val, uint8_t bits ) |
| 294 |
{ |
| 295 |
uint64_t rtn; |
| 296 |
rtn = ( val << bits ) | ( val >> ( 64 - bits ) ); |
| 297 |
return rtn; |
| 298 |
} |
| 299 |
|
| 300 |
static __inline__ |
| 301 |
uint64_t uint64_ror ( uint64_t val, uint8_t bits ) |
| 302 |
{ |
| 303 |
uint64_t rtn; |
| 304 |
rtn = ( val >> bits ) | ( val << ( 64 - bits ) ); |
| 305 |
return rtn; |
| 306 |
} |
| 307 |
|
| 308 |
#ifdef __cplusplus |
| 309 |
} |
| 310 |
#endif |
| 311 |
|
| 312 |
#endif /* _h_arch_impl_ */ |