5#ifndef RUNTIME_PLATFORM_UTILS_H_
6#define RUNTIME_PLATFORM_UTILS_H_
38 ASSERT(
x != std::numeric_limits<T>::min());
52 if (
x == std::numeric_limits<T>::min()) {
53 return std::numeric_limits<T>::max();
62 return ((
x & (
x - 1)) == 0) && (
x != 0);
82 return (
x & (alignment - 1)) ==
offset;
95 return (
x & -alignment);
100 return reinterpret_cast<T*
>(
104 template <
typename T>
112 template <
typename T>
114 return reinterpret_cast<T*
>(
127#if defined(ARCH_IS_64_BIT)
135#if __GNUC__ && !defined(HOST_ARCH_IA32) && !defined(HOST_ARCH_X64)
136 return __builtin_popcountll(
x);
138 x =
x - ((
x >> 1) & 0x5555555555555555);
139 x = (
x & 0x3333333333333333) + ((
x >> 2) & 0x3333333333333333);
140 x = (((
x + (
x >> 4)) & 0x0f0f0f0f0f0f0f0f) * 0x0101010101010101) >> 56;
147#if __GNUC__ && !defined(HOST_ARCH_IA32) && !defined(HOST_ARCH_X64)
148 return __builtin_popcount(
x);
152 x =
x - ((
x >> 1) & 0x55555555);
153 x = (
x & 0x33333333) + ((
x >> 2) & 0x33333333);
154 x = (
x + (
x >> 4)) & 0x0F0F0F0F;
157 return static_cast<int>(
x & 0x0000003F);
171 uint64_t
x =
static_cast<uint64_t
>((v > 0) ? v : -v);
174 if ((t =
x >> 32) != 0) {
178 if ((t =
x >> 16) != 0) {
182 if ((t =
x >> 8) != 0) {
186 if ((t =
x >> 4) != 0) {
190 if ((t =
x >> 2) != 0) {
205#if defined(DART_HOST_OS_WINDOWS)
206 unsigned long position;
207 return (_BitScanReverse(&position,
x) == 0)
209 : 31 -
static_cast<int>(position);
211 return x == 0 ? 32 : __builtin_clz(
x);
215#if defined(DART_HOST_OS_WINDOWS)
216#if defined(ARCH_IS_32_BIT)
217 const uint32_t x_hi =
static_cast<uint32_t
>(
x >> 32);
223 unsigned long position;
224 return (_BitScanReverse64(&position,
x) == 0)
226 : 63 -
static_cast<int>(position);
229 return x == 0 ? 64 : __builtin_clzll(
x);
241#if defined(DART_HOST_OS_WINDOWS)
242 unsigned long position;
243 return (_BitScanForward(&position,
x) == 0) ? 32
244 :
static_cast<int>(position);
246 return x == 0 ? 32 : __builtin_ctz(
x);
250#if defined(DART_HOST_OS_WINDOWS)
251#if defined(ARCH_IS_32_BIT)
252 const uint32_t x_lo =
static_cast<uint32_t
>(
x);
258 unsigned long position;
259 return (_BitScanForward64(&position,
x) == 0) ? 64
260 :
static_cast<int>(position);
263 return x == 0 ? 64 : __builtin_ctzll(
x);
297 template <
typename T>
298 static inline bool IsInt(intptr_t
N,
T value) {
300 constexpr intptr_t value_size_in_bits =
kBitsPerByte *
sizeof(
T);
301 if constexpr (std::is_signed<T>::value) {
302 if (
N >= value_size_in_bits)
return true;
303 const T limit =
static_cast<T>(1) << (
N - 1);
306 if (
N > value_size_in_bits)
return true;
307 const T limit =
static_cast<T>(1) << (
N - 1);
308 return value < limit;
312 template <
typename T>
315 constexpr intptr_t value_size_in_bits =
kBitsPerByte *
sizeof(
T);
316 if constexpr (std::is_signed<T>::value) {
317 if (
value < 0)
return false;
318 if (
N >= value_size_in_bits - 1) {
322 if (
N >= value_size_in_bits)
return true;
324 const T limit = (
static_cast<T>(1) <<
N) - 1;
325 return value <= limit;
336 template <
typename T>
339 if constexpr (std::is_signed<T>::value) {
340 using Unsigned =
typename std::make_unsigned<T>::type;
347 return static_cast<int32_t
>(
value & 0xffff);
351 return static_cast<int32_t
>(
value >> 16);
355 return static_cast<int32_t
>(
value);
359 return static_cast<int32_t
>(
value >> 32);
363 return (
static_cast<uint64_t
>(high) << 32) | (low & 0x0ffffffffLL);
367 return (c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') ||
372 return (
'0' <= c) && (c <=
'9');
377 ((
'a' <= c) && (c <=
'f'));
383 if ((
'A' <= c) && (c <=
'F'))
return 10 + (c -
'A');
384 return 10 + (c -
'a');
389 if (i < 10)
return static_cast<char>(
'0' + i);
390 return static_cast<char>(
'A' + (i - 10));
415 template <
typename T =
int64_t>
418 using Unsigned =
typename std::make_unsigned<T>::type;
419 return static_cast<T>(
static_cast<Unsigned
>(
a) +
static_cast<Unsigned
>(
b));
424 template <
typename T =
int64_t>
427 using Unsigned =
typename std::make_unsigned<T>::type;
428 return static_cast<T>(
static_cast<Unsigned
>(
a) -
static_cast<Unsigned
>(
b));
433 template <
typename T =
int64_t>
436 using Unsigned =
typename std::make_unsigned<T>::type;
437 return static_cast<T>(
static_cast<Unsigned
>(
a) *
static_cast<Unsigned
>(
b));
440 template <
typename T =
int64_t>
443 using Unsigned =
typename std::make_unsigned<T>::type;
444 return static_cast<T>(-
static_cast<Unsigned
>(
a));
455 return static_cast<int64_t
>(
static_cast<uint64_t
>(
a) <<
b);
458 template <
typename T>
463 using Unsigned =
typename std::make_unsigned<T>::type;
467 template <
typename T>
472 using Unsigned =
typename std::make_unsigned<T>::type;
511 return bit_cast<int64_t, double>(
a) == bit_cast<int64_t, double>(
b);
517 template <
typename T>
519 const double min =
static_cast<double>(std::numeric_limits<T>::min());
520 const double max =
static_cast<double>(std::numeric_limits<T>::max());
521 return (
min <= v && v <=
max) ?
static_cast<T>(v)
522 : std::numeric_limits<T>::min();
528 return ((-0x20000000000000LL <=
value) && (
value <= 0x20000000000000LL));
532 template <
typename T = uword>
534 using Unsigned =
typename std::make_unsigned<T>::type;
536 assert(n <=
sizeof(
T) * kBitsPerT);
537 return static_cast<T>(n == kBitsPerT ? std::numeric_limits<Unsigned>::max()
538 : (
static_cast<Unsigned
>(1) << n) - 1);
541 template <
typename T = uword>
542 static constexpr T Bit(
size_t n) {
548 template <
typename T>
549 static constexpr bool TestBit(
T mask,
size_t position) {
551 return ((mask >> position) & 1) != 0;
554 template <
typename T>
557 explicit BitsIterator(uint32_t bits) : bits_(bits), bit_(bits & -bits) {}
560 return static_cast<T>(BitPosition(bit_));
564 return bits_ == other.bits_ && bit_ == other.bit_;
568 return !(*
this == other);
573 bit_ = bits_ & -bits_;
580 static DART_FORCE_INLINE intptr_t BitPosition(uint32_t bit) {
581#if defined(DART_HOST_OS_WINDOWS)
582 unsigned long position;
583 BitScanForward(&position, bit);
584 return static_cast<int>(position);
586 return __builtin_ctz(bit);
594 template <
typename T>
611 static intptr_t
StrNLen(
const char*
s, intptr_t n);
613 return strncmp(
s, prefix, strlen(prefix)) == 0;
617 static size_t Read(
int filedes,
void* buf,
size_t nbyte);
656 char**
error =
nullptr);
667 char**
error =
nullptr);
674 char**
error =
nullptr);
676#if defined(DART_HOST_OS_LINUX)
677 static bool IsWindowsSubsystemForLinux();
683#if defined(DART_HOST_OS_ANDROID)
685#elif defined(DART_HOST_OS_FUCHSIA)
687#elif defined(DART_HOST_OS_LINUX)
689#elif defined(DART_HOST_OS_MACOS)
691#elif defined(DART_HOST_OS_WINDOWS)
694#error Unknown target os.
static bool rotate(const SkDCubic &cubic, int zero, int index, SkDCubic &rotPath)
DART_FORCE_INLINE BitsIterator & operator++()
BitsIterator(uint32_t bits)
DART_FORCE_INLINE T operator*() const
DART_FORCE_INLINE bool operator==(const BitsIterator &other) const
DART_FORCE_INLINE bool operator!=(const BitsIterator &other) const
BitsIterator< T > begin()
static constexpr bool IsAligned(T *x, uintptr_t alignment, uintptr_t offset=0)
static T AbsWithSaturation(T x)
static uint64_t HostToBigEndian64(uint64_t host_value)
static constexpr T Bit(size_t n)
static bool IsInt(intptr_t N, T value)
static int CountLeadingZeros64(uint64_t x)
static T * RoundUp(T *x, uintptr_t alignment, uintptr_t offset=0)
static bool IsJavaScriptInt(int64_t value)
static constexpr uintptr_t RoundUpToPowerOfTwo(uintptr_t x)
static uint64_t HostToLittleEndian64(uint64_t host_value)
static void CalculateMagicAndShiftForDivRem(int64_t divisor, int64_t *magic, int64_t *shift)
static int HexDigitToInt(char c)
static T MulWithWrapAround(T a, T b)
static void * LoadDynamicLibrary(const char *library_path, char **error=nullptr)
static int32_t Low32Bits(int64_t value)
static int32_t Low16Bits(int32_t value)
static T SafeDoubleToInt(double v)
static uword ReverseBitsWord(uword x)
static constexpr int CountOneBitsWord(uword x)
static constexpr T Maximum(T x, T y)
static int SNPrint(char *str, size_t size, const char *format,...) PRINTF_ATTRIBUTE(3
static bool MagnitudeIsUint(intptr_t N, T value)
static constexpr bool IsDecimalDigit(uint32_t c)
static constexpr int ShiftForPowerOfTwo(T x)
static int CountLeadingZeros32(uint32_t x)
static char * StrDup(const char *s)
static CStringUniquePtr CreateCStringUniquePtr(char *str)
static bool StrStartsWith(const char *s, const char *prefix)
static int CountTrailingZeros64(uint64_t x)
static constexpr T NBitMask(size_t n)
static double DivideAllowZero(double a, double b)
static uint64_t LittleEndianToHost64(uint64_t le_value)
static char static char * VSCreate(const char *format, va_list args)
static int CountTrailingZeros32(uint32_t x)
static constexpr bool IsAlphaNumeric(uint32_t c)
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)
static int32_t High32Bits(int64_t value)
static uint32_t WordHash(intptr_t key)
static bool WillSubOverflow(int64_t a, int64_t b)
static T Minimum(T x, T y)
std::unique_ptr< char, decltype(std::free) * > CStringUniquePtr
static T AddWithWrapAround(T a, T b)
static uint16_t HostToLittleEndian16(uint16_t host_value)
static T RotateLeft(T value, uint8_t rotate)
static T SubWithWrapAround(T a, T b)
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
static size_t Read(int filedes, void *buf, size_t nbyte)
static uint32_t StringHash(const void *data, int length)
static int CountLeadingZerosWord(uword x)
static constexpr size_t BitLength(int64_t value)
static int64_t LowHighTo64Bits(uint32_t low, int32_t high)
static bool WillAddOverflow(int64_t a, int64_t b)
static uint32_t BigEndianToHost32(uint32_t be_value)
static void UnloadDynamicLibrary(void *library_handle, char **error=nullptr)
static intptr_t StrNLen(const char *s, intptr_t n)
static constexpr int CountOneBits32(uint32_t x)
static constexpr bool TestBit(T mask, size_t position)
static char * StrError(int err, char *buffer, size_t bufsize)
static bool IsUint(intptr_t N, T value)
static int Close(int fildes)
static int CountTrailingZerosWord(uword x)
static uint32_t HostToLittleEndian32(uint32_t host_value)
static int Unlink(const char *path)
static T NegWithWrapAround(T a)
static float DivideAllowZero(float a, float b)
static constexpr int CountOneBits64(uint64_t x)
static char IntToHexDigit(int i)
static constexpr T RoundDown(T x, intptr_t alignment)
static bool RangeCheck(intptr_t offset, intptr_t count, intptr_t length)
static int32_t High16Bits(int32_t value)
static char * SCreate(const char *format,...) PRINTF_ATTRIBUTE(1
static void * ResolveSymbolInDynamicLibrary(void *library_handle, const char *symbol, char **error=nullptr)
static constexpr bool IsAligned(T x, uintptr_t alignment, uintptr_t offset=0)
static bool DoublesBitEqual(const double a, const double b)
static T RotateRight(T value, uint8_t rotate)
static int64_t ShiftLeftWithTruncation(int64_t a, int64_t b)
static bool IsHexDigit(char c)
static uint16_t HostToBigEndian16(uint16_t host_value)
static uint32_t HostToBigEndian32(uint32_t host_value)
static constexpr size_t HighestBit(int64_t v)
static uint32_t ReverseBits32(uint32_t x)
static uint64_t ReverseBits64(uint64_t x)
static T * RoundDown(T *x, intptr_t alignment)
static constexpr bool IsPowerOfTwo(T x)
static char * StrNDup(const char *s, intptr_t n)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
const uint8_t uint32_t uint32_t GError ** error
uint32_t uint32_t * format
static float max(float r, float g, float b)
static float min(float r, float g, float b)
__attribute__((visibility("default"))) int RunBenchmarks(int argc
constexpr int64_t kMaxInt64
constexpr int64_t kMinInt64
constexpr intptr_t kBitsPerByte
static int8_t data[kExtLength]
constexpr intptr_t kBitsPerInt64