/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include #include #include #include #include namespace facebook::yoga::details { constexpr uint8_t log2ceilFn(uint8_t n) { return n < 1 ? 0 : (1 + log2ceilFn(n / 2)); } constexpr uint32_t mask(uint8_t bitWidth, uint8_t index) { return ((1u << bitWidth) - 1u) << index; } } // namespace facebook::yoga::details namespace facebook::yoga { // The number of bits necessary to represent enums defined with YG_ENUM_SEQ_DECL template < typename Enum, std::enable_if_t<(ordinalCount() > 0), bool> = true> constexpr uint8_t minimumBitCount() { return details::log2ceilFn(ordinalCount() - 1); } template constexpr Enum getEnumData(uint32_t flags, uint8_t index) { return static_cast( (flags & details::mask(minimumBitCount(), index)) >> index); } template void setEnumData(uint32_t& flags, uint8_t index, Value newValue) { flags = (flags & ~static_cast(details::mask(minimumBitCount(), index))) | ((static_cast(newValue) << index) & (details::mask(minimumBitCount(), index))); } constexpr bool getBooleanData(uint32_t flags, uint8_t index) { return (flags >> index) & 1; } inline void setBooleanData(uint32_t& flags, uint8_t index, bool value) { if (value) { flags |= 1 << index; } else { flags &= ~(1 << index); } } } // namespace facebook::yoga