simdjson/include/jsonparser/common_defs.h

60 lines
1.3 KiB
C
Raw Normal View History

2018-03-23 12:05:32 +08:00
#pragma once
2018-08-21 05:27:25 +08:00
#include <cassert>
2018-03-23 12:05:32 +08:00
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef signed char s8;
typedef signed short s16;
typedef signed int s32;
typedef signed long long s64;
2018-08-21 05:27:25 +08:00
#ifdef _MSC_VER
/* Microsoft C/C++-compatible compiler */
#include <intrin.h>
#else
2018-03-23 12:05:32 +08:00
#include <immintrin.h>
2018-08-21 05:27:25 +08:00
#include <x86intrin.h>
#endif
2018-03-23 12:05:32 +08:00
typedef __m128i m128;
typedef __m256i m256;
// Align to N-byte boundary
#define ROUNDUP_N(a, n) (((a) + ((n)-1)) & ~((n)-1))
#define ROUNDDOWN_N(a, n) ((a) & ~((n)-1))
2018-08-21 05:27:25 +08:00
#define ISALIGNED_N(ptr, n) (((uintptr_t)(ptr) & ((n)-1)) == 0)
2018-03-23 12:05:32 +08:00
2018-08-21 05:27:25 +08:00
#define really_inline inline __attribute__((always_inline, unused))
#define never_inline inline __attribute__((noinline, unused))
2018-03-23 12:05:32 +08:00
2018-08-21 05:27:25 +08:00
#define UNUSED __attribute__((unused))
2018-03-23 12:05:32 +08:00
#ifndef likely
2018-08-21 05:27:25 +08:00
#define likely(x) __builtin_expect(!!(x), 1)
2018-03-23 12:05:32 +08:00
#endif
#ifndef unlikely
2018-08-21 05:27:25 +08:00
#define unlikely(x) __builtin_expect(!!(x), 0)
2018-03-23 12:05:32 +08:00
#endif
2018-08-21 05:27:25 +08:00
static inline u32 ctz64(u64 x) {
assert(x); // behaviour not defined for x == 0
2018-03-23 12:05:32 +08:00
#if defined(_WIN64)
2018-08-21 05:27:25 +08:00
unsigned long r;
_BitScanForward64(&r, x);
return r;
2018-03-23 12:05:32 +08:00
#elif defined(_WIN32)
2018-08-21 05:27:25 +08:00
unsigned long r;
if (_BitScanForward(&r, (u32)x)) {
return (u32)r;
}
_BitScanForward(&r, x >> 32);
return (u32)(r + 32);
2018-03-23 12:05:32 +08:00
#else
2018-08-21 05:27:25 +08:00
return (u32)__builtin_ctzll(x);
2018-03-23 12:05:32 +08:00
#endif
}