Path: blob/master/src/hotspot/share/gc/z/zBitField.hpp
40957 views
/*1* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#ifndef SHARE_GC_Z_ZBITFIELD_HPP24#define SHARE_GC_Z_ZBITFIELD_HPP2526#include "memory/allocation.hpp"27#include "utilities/debug.hpp"2829//30// Example31// -------32//33// typedef ZBitField<uint64_t, uint8_t, 0, 2, 3> field_word_aligned_size;34// typedef ZBitField<uint64_t, uint32_t, 2, 30> field_length;35//36//37// 6 3 338// 3 2 1 2 1039// +-----------------------------------+---------------------------------+--+40// |11111111 11111111 11111111 11111111|11111111 11111111 11111111 111111|11|41// +-----------------------------------+---------------------------------+--+42// | | |43// | 31-2 field_length (30-bits) * |44// | |45// | 1-0 field_word_aligned_size (2-bits) *46// |47// * 63-32 Unused (32-bits)48//49//50// field_word_aligned_size::encode(16) = 251// field_length::encode(2342) = 936852//53// field_word_aligned_size::decode(9368 | 2) = 1654// field_length::decode(9368 | 2) = 234255//5657template <typename ContainerType, typename ValueType, int FieldShift, int FieldBits, int ValueShift = 0>58class ZBitField : public AllStatic {59private:60static const int ContainerBits = sizeof(ContainerType) * BitsPerByte;6162static_assert(FieldBits < ContainerBits, "Field too large");63static_assert(FieldShift + FieldBits <= ContainerBits, "Field too large");64static_assert(ValueShift + FieldBits <= ContainerBits, "Field too large");6566static const ContainerType FieldMask = (((ContainerType)1 << FieldBits) - 1);6768public:69static ValueType decode(ContainerType container) {70return (ValueType)(((container >> FieldShift) & FieldMask) << ValueShift);71}7273static ContainerType encode(ValueType value) {74assert(((ContainerType)value & (FieldMask << ValueShift)) == (ContainerType)value, "Invalid value");75return ((ContainerType)value >> ValueShift) << FieldShift;76}77};7879#endif // SHARE_GC_Z_ZBITFIELD_HPP808182