this repo has no description
at trunk 190 lines 6.7 kB view raw
1/* Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) */ 2#pragma once 3 4#include <cstddef> 5#include <cstdint> 6#include <cstring> 7#include <limits> 8#include <type_traits> 9 10typedef unsigned char byte; 11typedef intptr_t word; 12typedef uintptr_t uword; 13 14static_assert(sizeof(word) == sizeof(size_t), 15 "word must be the same size as size_t"); 16 17const int kBoolSize = sizeof(bool); 18const int kByteSize = sizeof(byte); 19const int kDoubleSize = sizeof(double); 20const int kFloatSize = sizeof(float); 21const int kIntSize = sizeof(int); 22const int kLongSize = sizeof(long); 23const int kLongLongSize = sizeof(long long); 24const int kPointerSize = sizeof(void*); 25const int kShortSize = sizeof(short); 26const int kWcharSize = sizeof(wchar_t); 27const int kWordSize = sizeof(word); 28 29const int kWordSizeLog2 = 3; 30 31const int kUwordDigits10 = 19; 32const uword kUwordDigits10Pow = 10000000000000000000ul; 33const int kBitsPerHexDigit = 4; 34const int kBitsPerOctDigit = 3; 35 36const int kBitsPerByte = 8; 37const int kBitsPerPointer = kBitsPerByte * kWordSize; 38const int kBitsPerWord = kBitsPerByte * kWordSize; 39const int kBitsPerDouble = kBitsPerByte * kDoubleSize; 40 41const int kDoubleMantissaBits = 52; 42const double kDoubleNaN = std::numeric_limits<double>::quiet_NaN(); 43const double kDoubleInfinity = std::numeric_limits<double>::infinity(); 44const auto kDoubleMinExp = std::numeric_limits<double>::min_exponent; 45const auto kDoubleMaxExp = std::numeric_limits<double>::max_exponent; 46const auto kDoubleDigits = std::numeric_limits<double>::digits; 47 48const int16_t kMaxInt16 = INT16_MAX; 49const int16_t kMinInt16 = INT16_MIN; 50const int32_t kMaxInt32 = INT32_MAX; 51const int32_t kMinInt32 = INT32_MIN; 52const int64_t kMaxInt64 = INT64_MAX; 53const int64_t kMinInt64 = INT64_MIN; 54const uint16_t kMaxUint16 = UINT16_MAX; 55const uint32_t kMaxUint32 = UINT32_MAX; 56const uint64_t kMaxUint64 = UINT64_MAX; 57 58const byte kMaxByte = 0xFF; 59 60const long kMinLong = std::numeric_limits<long>::min(); 61const long kMaxLong = std::numeric_limits<long>::max(); 62 63const word kMinWord = INTPTR_MIN; 64const word kMaxWord = INTPTR_MAX; 65const uword kMaxUword = UINTPTR_MAX; 66 67const int kMaxASCII = 127; 68const int kMaxUnicode = 0x10ffff; 69const int32_t kReplacementCharacter = 0xFFFD; 70 71const int kKiB = 1024; 72const int kMiB = kKiB * kKiB; 73const int kGiB = kKiB * kKiB * kKiB; 74 75const int kMillisecondsPerSecond = 1000; 76const int kMicrosecondsPerMillisecond = 1000; 77const int kMicrosecondsPerSecond = 78 kMillisecondsPerSecond * kMicrosecondsPerMillisecond; 79const int kNanosecondsPerMicrosecond = 1000; 80const int kNanosecondsPerSecond = 81 kMicrosecondsPerSecond * kNanosecondsPerMicrosecond; 82 83// Equivalent to _PyHash_BITS. This is NOT the maximum size of a hash value, 84// that would is either RawHeader::kHashCodeBits or SmallInt::kMaxValue 85// depending on whether the hash value is cached in the object header. 86const word kArithmeticHashBits = 61; 87// Equivalent to _PyHASH_MODULUS. Should be a mersenne prime. 88const word kArithmeticHashModulus = ((word{1} << kArithmeticHashBits) - 1); 89const word kHashInf = 314159; 90const word kHashNan = 0; 91const word kHashImag = 1000003; 92 93#ifndef __has_builtin 94#define __has_builtin(x) 0 95#endif 96#ifndef __has_cpp_atttribute 97#define __has_cpp_atttribute(x) 0 98#endif 99 100#if __GNUG__ && __GNUC__ < 5 101#define IS_TRIVIALLY_COPYABLE(T) __is_trivially_copyable(T) 102#else 103#define IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value 104#endif 105 106template <typename D, typename S> 107static inline D bit_cast(const S& src) { 108 static_assert(sizeof(S) == sizeof(D), "src and dst must be the same size"); 109 static_assert(IS_TRIVIALLY_COPYABLE(S), "src must be trivially copyable"); 110 static_assert(std::is_trivial<D>::value, "dst must be trivial"); 111 D dst; 112 std::memcpy(&dst, &src, sizeof(dst)); 113 return dst; 114} 115 116#define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0])) 117 118#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 119 TypeName(const TypeName&) = delete; \ 120 void operator=(const TypeName&) = delete 121 122#define DISALLOW_HEAP_ALLOCATION() \ 123 void* operator new(size_t size) = delete; \ 124 void* operator new[](size_t size) = delete 125 126#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ 127 TypeName() = delete; \ 128 DISALLOW_COPY_AND_ASSIGN(TypeName) 129 130#define PY_EXPORT extern "C" __attribute__((visibility("default"))) 131 132// FORMAT_ATTRIBUTE allows typechecking by the compiler. 133// string_index: The function argument index where the format index is. 134// this has the implicit index 1. 135// varargs_index: The function argument index where varargs start. 136// The archetype chosen is printf since this is being used for output strings. 137#define FORMAT_ATTRIBUTE(string_index, first_to_check) \ 138 __attribute__((format(printf, string_index, first_to_check))) 139 140// Branch prediction hints for the compiler. Use in performance critial code 141// which almost always branches one way. 142#define LIKELY(x) __builtin_expect(!!(x), 1) 143#define UNLIKELY(x) __builtin_expect(!!(x), 0) 144 145// Old-school version of offsetof() that works with non-standard layout classes. 146#define OFFSETOF(ty, memb) \ 147 (reinterpret_cast<char*>(&reinterpret_cast<ty*>(16)->memb) - \ 148 reinterpret_cast<char*>(16)) 149 150#if __has_cpp_atttribute(nodiscard) || \ 151 (__GNUC__ >= 5 || __GNUC__ == 4 && __GNUC_MINOR__ >= 8) 152#define NODISCARD [[nodiscard]] 153#else 154#define NODISCARD __attribute__((warn_unused_result)) 155#endif 156 157#define ALIGN_16 __attribute__((aligned(16))) 158 159#define ALWAYS_INLINE inline __attribute__((always_inline)) 160 161#if defined(__clang__) 162#define FALLTHROUGH [[clang::fallthrough]] 163#elif defined(__GNUC__) 164#define FALLTHROUGH __attribute__((fallthrough)) 165#else 166#define FALLTHROUGH \ 167 do { \ 168 } while (0) 169#endif 170 171#define NEVER_INLINE __attribute__((noinline)) 172 173#define USED __attribute__((used)) 174 175#define UNUSED __attribute__((unused)) 176 177#define WARN_UNUSED __attribute__((warn_unused)) 178 179// Endian enum (as proposed in the C++20 draft). 180enum class endian { 181// Implementation for gcc + clang compilers. 182#if defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && \ 183 defined(__BYTE_ORDER__) 184 little = __ORDER_LITTLE_ENDIAN__, 185 big = __ORDER_BIG_ENDIAN__, 186 native = __BYTE_ORDER__ 187#else 188#error "endian class not implemented for this compiler" 189#endif 190};