//===--- Enum.h - Enum implementation runtime declarations ------*- C++ -*-===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// // // Enum implementation details declarations relevant to the ABI. // //===----------------------------------------------------------------------===// #ifndef SWIFT_ABI_ENUM_H #define SWIFT_ABI_ENUM_H #include namespace swift { struct EnumTagCounts { unsigned numTags, numTagBytes; }; inline EnumTagCounts getEnumTagCounts(size_t size, unsigned emptyCases, unsigned payloadCases) { // We can use the payload area with a tag bit set somewhere outside of the // payload area to represent cases. See how many bytes we need to cover // all the empty cases. unsigned numTags = payloadCases; if (emptyCases > 0) { if (size >= 4) // Assume that one tag bit is enough if the precise calculation overflows // an int32. numTags += 1; else { unsigned bits = size * 8U; unsigned casesPerTagBitValue = 1U << bits; numTags += ((emptyCases + (casesPerTagBitValue-1U)) >> bits); } } unsigned numTagBytes = (numTags <= 1 ? 0 : numTags < 256 ? 1 : numTags < 65536 ? 2 : 4); return {numTags, numTagBytes}; } } // namespace swift #endif // SWIFT_ABI_ENUM_H