/usr/include/htslib/hts_endian.h is in libhts-dev 1.7-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | /// @file hts_endian.h
/// Byte swapping and unaligned access functions.
/*
Copyright (C) 2017 Genome Research Ltd.
Author: Rob Davies <rmd@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#ifndef HTS_ENDIAN_H
#define HTS_ENDIAN_H
#include <stdint.h>
/*
* Compile-time endianness tests.
*
* Note that these tests may fail. They should only be used to enable
* faster versions of endian-neutral implementations. The endian-neutral
* version should always be available as a fall-back.
*
* See https://sourceforge.net/p/predef/wiki/Endianness/
*/
/* Save typing as both endian and unaligned tests want to know about x86 */
#if (defined(__i386__) || defined(__i386) || defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(__i686__) || defined(__i686)) && !defined(HTS_x86)
# define HTS_x86 /* x86 and x86_64 platform */
#endif
/** @def HTS_LITTLE_ENDIAN
* @brief Defined if platform is known to be little-endian
*/
#ifndef HTS_LITTLE_ENDIAN
# if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
|| defined(__LITTLE_ENDIAN__) \
|| defined(HTS_x86) \
|| defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) \
|| defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
# define HTS_LITTLE_ENDIAN
# endif
#endif
/** @def HTS_BIG_ENDIAN
* @brief Defined if platform is known to be big-endian
*/
#ifndef HTS_BIG_ENDIAN
# if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
|| defined(__BIG_ENDIAN__) \
|| defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AAARCHEB__) \
|| defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
# define HTS_BIG_ENDIAN
# endif
#endif
/** @def HTS_ENDIAN_NEUTRAL
* @brief Define this to disable any endian-specific optimizations
*/
#if defined(HTS_ENDIAN_NEUTRAL) || (defined(HTS_LITTLE_ENDIAN) && defined(HTS_BIG_ENDIAN))
/* Disable all endian-specific code. */
# undef HTS_LITTLE_ENDIAN
# undef HTS_BIG_ENDIAN
#endif
/** @def HTS_ALLOW_UNALIGNED
* @brief Control use of unaligned memory access.
*
* Defining HTS_ALLOW_UNALIGNED=1 converts shift-and-or to simple casts on
* little-endian platforms that can tolerate unaligned access (notably Intel
* x86).
*
* Defining HTS_ALLOW_UNALIGNED=0 forces shift-and-or.
*/
// Consider using AX_CHECK_ALIGNED_ACCESS_REQUIRED in autoconf.
#ifndef HTS_ALLOW_UNALIGNED
# if defined(HTS_x86)
# define HTS_ALLOW_UNALIGNED 1
# else
# define HTS_ALLOW_UNALIGNED 0
# endif
#endif
#if HTS_ALLOW_UNALIGNED != 0
# if defined (__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
// This prevents problems with gcc's vectoriser generating the wrong
// instructions for unaligned data.
typedef uint16_t uint16_u __attribute__ ((__aligned__ (1)));
typedef uint32_t uint32_u __attribute__ ((__aligned__ (1)));
typedef uint64_t uint64_u __attribute__ ((__aligned__ (1)));
#else
typedef uint16_t uint16_u;
typedef uint32_t uint32_u;
typedef uint64_t uint64_u;
# endif
#endif
/// Get a uint16_t value from an unsigned byte array
/** @param buf Pointer to source byte, may be unaligned
* @return A 16 bit unsigned integer
* The input is read in little-endian byte order.
*/
static inline uint16_t le_to_u16(const uint8_t *buf) {
#if defined(HTS_LITTLE_ENDIAN) && HTS_ALLOW_UNALIGNED != 0
return *((uint16_u *) buf);
#else
return (uint16_t) buf[0] | ((uint16_t) buf[1] << 8);
#endif
}
/// Get a uint32_t value from an unsigned byte array
/** @param buf Pointer to source byte array, may be unaligned
* @return A 32 bit unsigned integer
* The input is read in little-endian byte order.
*/
static inline uint32_t le_to_u32(const uint8_t *buf) {
#if defined(HTS_LITTLE_ENDIAN) && HTS_ALLOW_UNALIGNED != 0
return *((uint32_u *) buf);
#else
return ((uint32_t) buf[0] |
((uint32_t) buf[1] << 8) |
((uint32_t) buf[2] << 16) |
((uint32_t) buf[3] << 24));
#endif
}
/// Get a uint64_t value from an unsigned byte array
/** @param buf Pointer to source byte array, may be unaligned
* @return A 64 bit unsigned integer
* The input is read in little-endian byte order.
*/
static inline uint64_t le_to_u64(const uint8_t *buf) {
#if defined(HTS_LITTLE_ENDIAN) && HTS_ALLOW_UNALIGNED != 0
return *((uint64_u *) buf);
#else
return ((uint64_t) buf[0] |
((uint64_t) buf[1] << 8) |
((uint64_t) buf[2] << 16) |
((uint64_t) buf[3] << 24) |
((uint64_t) buf[4] << 32) |
((uint64_t) buf[5] << 40) |
((uint64_t) buf[6] << 48) |
((uint64_t) buf[7] << 56));
#endif
}
/// Store a uint16_t value in little-endian byte order
/** @param val The value to store
* @param buf Where to store it (may be unaligned)
*/
static inline void u16_to_le(uint16_t val, uint8_t *buf) {
#if defined(HTS_LITTLE_ENDIAN) && HTS_ALLOW_UNALIGNED != 0
*((uint16_u *) buf) = val;
#else
buf[0] = val & 0xff;
buf[1] = (val >> 8) & 0xff;
#endif
}
/// Store a uint32_t value in little-endian byte order
/** @param val The value to store
* @param buf Where to store it (may be unaligned)
*/
static inline void u32_to_le(uint32_t val, uint8_t *buf) {
#if defined(HTS_LITTLE_ENDIAN) && HTS_ALLOW_UNALIGNED != 0
*((uint32_u *) buf) = val;
#else
buf[0] = val & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[2] = (val >> 16) & 0xff;
buf[3] = (val >> 24) & 0xff;
#endif
}
/// Store a uint64_t value in little-endian byte order
/** @param val The value to store
* @param buf Where to store it (may be unaligned)
*/
static inline void u64_to_le(uint64_t val, uint8_t *buf) {
#if defined(HTS_LITTLE_ENDIAN) && HTS_ALLOW_UNALIGNED != 0
*((uint64_u *) buf) = val;
#else
buf[0] = val & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[2] = (val >> 16) & 0xff;
buf[3] = (val >> 24) & 0xff;
buf[4] = (val >> 32) & 0xff;
buf[5] = (val >> 40) & 0xff;
buf[6] = (val >> 48) & 0xff;
buf[7] = (val >> 56) & 0xff;
#endif
}
/* Signed values. Grab the data as unsigned, then convert to signed without
* triggering undefined behaviour. On any sensible platform, the conversion
* should optimise away to nothing.
*/
/// Get an int8_t value from an unsigned byte array
/** @param buf Pointer to source byte array, may be unaligned
* @return A 8 bit signed integer
* The input data is interpreted as 2's complement representation.
*/
static inline int8_t le_to_i8(const uint8_t *buf) {
return *buf < 0x80 ? *buf : -((int8_t) (0xff - *buf)) - 1;
}
/// Get an int16_t value from an unsigned byte array
/** @param buf Pointer to source byte array, may be unaligned
* @return A 16 bit signed integer
* The input data is interpreted as 2's complement representation in
* little-endian byte order.
*/
static inline int16_t le_to_i16(const uint8_t *buf) {
uint16_t v = le_to_u16(buf);
return v < 0x8000 ? v : -((int16_t) (0xffff - v)) - 1;
}
/// Get an int32_t value from an unsigned byte array
/** @param buf Pointer to source byte array, may be unaligned
* @return A 32 bit signed integer
* The input data is interpreted as 2's complement representation in
* little-endian byte order.
*/
static inline int32_t le_to_i32(const uint8_t *buf) {
uint32_t v = le_to_u32(buf);
return v < 0x80000000U ? v : -((int32_t) (0xffffffffU - v)) - 1;
}
/// Get an int64_t value from an unsigned byte array
/** @param buf Pointer to source byte array, may be unaligned
* @return A 64 bit signed integer
* The input data is interpreted as 2's complement representation in
* little-endian byte order.
*/
static inline int64_t le_to_i64(const uint8_t *buf) {
uint64_t v = le_to_u64(buf);
return (v < 0x8000000000000000ULL
? v : -((int64_t) (0xffffffffffffffffULL - v)) - 1);
}
// Converting the other way is easier as signed -> unsigned is well defined.
/// Store a uint16_t value in little-endian byte order
/** @param val The value to store
* @param buf Where to store it (may be unaligned)
*/
static inline void i16_to_le(int16_t val, uint8_t *buf) {
u16_to_le(val, buf);
}
/// Store a uint32_t value in little-endian byte order
/** @param val The value to store
* @param buf Where to store it (may be unaligned)
*/
static inline void i32_to_le(int32_t val, uint8_t *buf) {
u32_to_le(val, buf);
}
/// Store a uint64_t value in little-endian byte order
/** @param val The value to store
* @param buf Where to store it (may be unaligned)
*/
static inline void i64_to_le(int64_t val, uint8_t *buf) {
u64_to_le(val, buf);
}
/* Floating point. Assumptions:
* Platform uses IEEE 754 format
* sizeof(float) == sizeof(uint32_t)
* sizeof(double) == sizeof(uint64_t)
* Endian-ness is the same for both floating point and integer
* Type-punning via a union is allowed
*/
/// Get a float value from an unsigned byte array
/** @param buf Pointer to source byte array, may be unaligned
* @return A 32 bit floating point value
* The input is interpreted as an IEEE 754 format float in little-endian
* byte order.
*/
static inline float le_to_float(const uint8_t *buf) {
union {
uint32_t u;
float f;
} convert;
convert.u = le_to_u32(buf);
return convert.f;
}
/// Get a double value from an unsigned byte array
/** @param buf Pointer to source byte array, may be unaligned
* @return A 64 bit floating point value
* The input is interpreted as an IEEE 754 format double in little-endian
* byte order.
*/
static inline double le_to_double(const uint8_t *buf) {
union {
uint64_t u;
double f;
} convert;
convert.u = le_to_u64(buf);
return convert.f;
}
/// Store a float value in little-endian byte order
/** @param val The value to store
* @param buf Where to store it (may be unaligned)
*/
static inline void float_to_le(float val, uint8_t *buf) {
union {
uint32_t u;
float f;
} convert;
convert.f = val;
u32_to_le(convert.u, buf);
}
/// Store a double value in little-endian byte order
/** @param val The value to store
* @param buf Where to store it (may be unaligned)
*/
static inline void double_to_le(double val, uint8_t *buf) {
union {
uint64_t u;
double f;
} convert;
convert.f = val;
u64_to_le(convert.u, buf);
}
#endif /* HTS_ENDIAN_H */
|