1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #ifndef SHARE_GC_Z_ZBITFIELD_HPP
  25 #define SHARE_GC_Z_ZBITFIELD_HPP
  26 
  27 #include "memory/allocation.hpp"
  28 #include "utilities/debug.hpp"
  29 
  30 //
  31 //  Example
  32 //  -------
  33 //
  34 //  typedef ZBitField<uint64_t, uint8_t,  0,  2, 3> field_word_aligned_size;
  35 //  typedef ZBitField<uint64_t, uint32_t, 2, 30>    field_length;
  36 //
  37 //
  38 //   6                                 3 3
  39 //   3                                 2 1                               2 10
  40 //  +-----------------------------------+---------------------------------+--+
  41 //  |11111111 11111111 11111111 11111111|11111111 11111111 11111111 111111|11|
  42 //  +-----------------------------------+---------------------------------+--+
  43 //  |                                   |                                 |
  44 //  |       31-2 field_length (30-bits) *                                 |
  45 //  |                                                                     |
  46 //  |                                1-0 field_word_aligned_size (2-bits) *
  47 //  |
  48 //  * 63-32 Unused (32-bits)
  49 //
  50 //
  51 //  field_word_aligned_size::encode(16) = 2
  52 //  field_length::encode(2342) = 9368
  53 //
  54 //  field_word_aligned_size::decode(9368 | 2) = 16
  55 //  field_length::decode(9368 | 2) = 2342
  56 //
  57 
  58 template <typename ContainerType, typename ValueType, int FieldShift, int FieldBits, int ValueShift = 0>
  59 class ZBitField : public AllStatic {
  60 private:
  61   static const int ContainerBits = sizeof(ContainerType) * BitsPerByte;
  62 
  63   STATIC_ASSERT(FieldBits < ContainerBits);
  64   STATIC_ASSERT(FieldShift + FieldBits <= ContainerBits);
  65   STATIC_ASSERT(ValueShift + FieldBits <= ContainerBits);
  66 
  67   static const ContainerType FieldMask = (((ContainerType)1 << FieldBits) - 1);
  68 
  69 public:
  70   static ValueType decode(ContainerType container) {
  71     return (ValueType)(((container >> FieldShift) & FieldMask) << ValueShift);
  72   }
  73 
  74   static ContainerType encode(ValueType value) {
  75     assert(((ContainerType)value & (FieldMask << ValueShift)) == (ContainerType)value, "Invalid value");
  76     return ((ContainerType)value >> ValueShift) << FieldShift;
  77   }
  78 };
  79 
  80 #endif // SHARE_GC_Z_ZBITFIELD_HPP