/* * Copyright (C) 2016 The Qt Company Ltd. * Copyright (c) Meta Platforms, Inc. and affiliates. * * SPDX-License-Identifier: MIT */ #pragma once #include #include #include #include namespace facebook { namespace yoga { namespace detail { // std::bitset with one bit for each option defined in YG_ENUM_SEQ_DECL template using EnumBitset = std::bitset()>; constexpr size_t log2ceilFn(size_t n) { return n < 1 ? 0 : (1 + log2ceilFn(n / 2)); } constexpr int mask(size_t bitWidth, size_t index) { return ((1 << bitWidth) - 1) << index; } // The number of bits necessary to represent enums defined with YG_ENUM_SEQ_DECL template constexpr size_t bitWidthFn() { static_assert( enums::count() > 0, "Enums must have at least one entries"); return log2ceilFn(enums::count() - 1); } template constexpr Enum getEnumData(int flags, size_t index) { return static_cast((flags & mask(bitWidthFn(), index)) >> index); } template void setEnumData(uint32_t& flags, size_t index, int newValue) { flags = (flags & ~mask(bitWidthFn(), index)) | ((newValue << index) & (mask(bitWidthFn(), index))); } template void setEnumData(uint8_t& flags, size_t index, int newValue) { flags = (flags & ~static_cast(mask(bitWidthFn(), index))) | ((newValue << index) & (static_cast(mask(bitWidthFn(), index)))); } constexpr bool getBooleanData(int flags, size_t index) { return (flags >> index) & 1; } inline void setBooleanData(uint8_t& flags, size_t index, bool value) { if (value) { flags |= 1 << index; } else { flags &= ~(1 << index); } } } // namespace detail } // namespace yoga } // namespace facebook