Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //===--- InlineBitfields.hpp - Inline Bitfields Helpers ---------*- C++ -*-===//
- // Part of the Sora project, licensed under the MIT license.
- // See LICENSE.txt in the project root for license information.
- //
- // Copyright (c) 2019 ???
- //===----------------------------------------------------------------------===//
- #pragma once
- #include "llvm/Support/Compiler.h"
- #include <cstdint>
- /// NOTE: When passing the bit count to these macros, please do NOT precompute
- /// the total. Instead, sum the bit counts in field order. This makes visually
- /// verifying that all the fields are accounted for easier. For example:
- /// SORA_INLINE_BITFIELD(Foo, Bar, 1+3+7+2, w:1, x:3, y:7, z:2);
- /// Define a base bitfield for type 'T' with 'C' bits used.
- ///
- /// Please note that the 'base' type does not need to be the root class in a
- /// hierarchy. If a superclass bitfield is full, then a subclass can start a new
- /// bitfield union for its subclasses to use.
- #define SORA_INLINE_BITFIELD_BASE(T, C, ...) \
- LLVM_PACKED_START \
- class T##Bitfield { \
- friend class T; \
- uint64_t __VA_ARGS__; \
- uint64_t : 64 - (C); /* Better code gen */ \
- } T; \
- LLVM_PACKED_END \
- enum { Num##T##Bits = (C) }; \
- static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
- /// Define an bitfield for type 'T' with parent class 'U' and 'C' bits used.
- #define SORA_INLINE_BITFIELD_TEMPLATE(T, U, C, HC, ...) \
- LLVM_PACKED_START \
- class T##Bitfield { \
- friend class T; \
- uint64_t : Num##U##Bits, __VA_ARGS__; \
- uint64_t : 64 - (Num##U##Bits + (HC) + (C)); /* Better code gen */ \
- } T; \
- LLVM_PACKED_END \
- enum { Num##T##Bits = Num##U##Bits + (C) }; \
- static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
- #define SORA_INLINE_BITFIELD(T, U, C, ...) \
- SORA_INLINE_BITFIELD_TEMPLATE(T, U, C, 0, __VA_ARGS__)
- /// Define a full bitfield for type 'T' that uses all of the remaining bits in
- /// the inline bitfield.
- ///
- /// For optimal code gen, place naturally sized fields at the end, with the
- /// largest naturally sized field at the very end. For example:
- ///
- /// SORA_INLINE_BITFIELD_FULL(Foo, Bar, 1+8+16,
- /// flag : 1,
- /// : NumPadBits, // pad the center, not the end
- /// x : 8,
- /// y : 16
- /// );
- #define SORA_INLINE_BITFIELD_FULL(T, U, C, ...) \
- LLVM_PACKED_START \
- class T##Bitfield { \
- friend class T; \
- enum { NumPadBits = 64 - (Num##U##Bits + (C)) }; \
- uint64_t : Num##U##Bits, __VA_ARGS__; \
- } T; \
- LLVM_PACKED_END \
- static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
- /// Define an empty bitfield for type 'T'.
- #define SORA_INLINE_BITFIELD_EMPTY(T, U) \
- enum { Num##T##Bits = Num##U##Bits }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement