SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //===--- InlineBitfields.hpp - Inline Bitfields Helpers ---------*- C++ -*-===//
  2. // Part of the Sora project, licensed under the MIT license.
  3. // See LICENSE.txt in the project root for license information.
  4. //
  5. // Copyright (c) 2019 ???
  6. //===----------------------------------------------------------------------===//
  7.  
  8. #pragma once
  9.  
  10. #include "llvm/Support/Compiler.h"
  11. #include <cstdint>
  12.  
  13. /// NOTE: When passing the bit count to these macros, please do NOT precompute
  14. /// the total. Instead, sum the bit counts in field order. This makes visually
  15. /// verifying that all the fields are accounted for easier. For example:
  16. /// SORA_INLINE_BITFIELD(Foo, Bar, 1+3+7+2, w:1, x:3, y:7, z:2);
  17.  
  18. /// Define a base bitfield for type 'T' with 'C' bits used.
  19. ///
  20. /// Please note that the 'base' type does not need to be the root class in a
  21. /// hierarchy. If a superclass bitfield is full, then a subclass can start a new
  22. /// bitfield union for its subclasses to use.
  23. #define SORA_INLINE_BITFIELD_BASE(T, C, ...) \
  24.   LLVM_PACKED_START \
  25.   class T##Bitfield { \
  26.     friend class T; \
  27.     uint64_t __VA_ARGS__; \
  28.     uint64_t : 64 - (C); /* Better code gen */ \
  29.   } T; \
  30.   LLVM_PACKED_END \
  31.   enum { Num##T##Bits = (C) }; \
  32.   static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
  33.  
  34. /// Define an bitfield for type 'T' with parent class 'U' and 'C' bits used.
  35. #define SORA_INLINE_BITFIELD_TEMPLATE(T, U, C, HC, ...) \
  36.   LLVM_PACKED_START \
  37.   class T##Bitfield { \
  38.     friend class T; \
  39.     uint64_t : Num##U##Bits, __VA_ARGS__; \
  40.     uint64_t : 64 - (Num##U##Bits + (HC) + (C)); /* Better code gen */ \
  41.   } T; \
  42.   LLVM_PACKED_END \
  43.   enum { Num##T##Bits = Num##U##Bits + (C) }; \
  44.   static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
  45.  
  46. #define SORA_INLINE_BITFIELD(T, U, C, ...) \
  47.   SORA_INLINE_BITFIELD_TEMPLATE(T, U, C, 0, __VA_ARGS__)
  48.  
  49. /// Define a full bitfield for type 'T' that uses all of the remaining bits in
  50. /// the inline bitfield.
  51. ///
  52. /// For optimal code gen, place naturally sized fields at the end, with the
  53. /// largest naturally sized field at the very end. For example:
  54. ///
  55. /// SORA_INLINE_BITFIELD_FULL(Foo, Bar, 1+8+16,
  56. ///   flag : 1,
  57. ///   : NumPadBits, // pad the center, not the end
  58. ///   x : 8,
  59. ///   y : 16
  60. /// );
  61. #define SORA_INLINE_BITFIELD_FULL(T, U, C, ...) \
  62.   LLVM_PACKED_START \
  63.   class T##Bitfield { \
  64.     friend class T; \
  65.     enum { NumPadBits = 64 - (Num##U##Bits + (C)) }; \
  66.     uint64_t : Num##U##Bits, __VA_ARGS__; \
  67.   } T; \
  68.   LLVM_PACKED_END \
  69.   static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
  70.  
  71. /// Define an empty bitfield for type 'T'.
  72. #define SORA_INLINE_BITFIELD_EMPTY(T, U) \
  73.   enum { Num##T##Bits = Num##U##Bits }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top