Path: blob/master/thirdparty/glslang/SPIRV/bitutils.h
9904 views
// Copyright (c) 2015-2016 The Khronos Group Inc.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#ifndef LIBSPIRV_UTIL_BITUTILS_H_15#define LIBSPIRV_UTIL_BITUTILS_H_1617#include <cstdint>18#include <cstring>1920namespace spvutils {2122// Performs a bitwise copy of source to the destination type Dest.23template <typename Dest, typename Src>24Dest BitwiseCast(Src source) {25Dest dest;26static_assert(sizeof(source) == sizeof(dest),27"BitwiseCast: Source and destination must have the same size");28std::memcpy(static_cast<void*>(&dest), &source, sizeof(dest));29return dest;30}3132// SetBits<T, First, Num> returns an integer of type <T> with bits set33// for position <First> through <First + Num - 1>, counting from the least34// significant bit. In particular when Num == 0, no positions are set to 1.35// A static assert will be triggered if First + Num > sizeof(T) * 8, that is,36// a bit that will not fit in the underlying type is set.37template <typename T, size_t First = 0, size_t Num = 0>38struct SetBits {39static_assert(First < sizeof(T) * 8,40"Tried to set a bit that is shifted too far.");41const static T get = (T(1) << First) | SetBits<T, First + 1, Num - 1>::get;42};4344template <typename T, size_t Last>45struct SetBits<T, Last, 0> {46const static T get = T(0);47};4849// This is all compile-time so we can put our tests right here.50static_assert(SetBits<uint32_t, 0, 0>::get == uint32_t(0x00000000),51"SetBits failed");52static_assert(SetBits<uint32_t, 0, 1>::get == uint32_t(0x00000001),53"SetBits failed");54static_assert(SetBits<uint32_t, 31, 1>::get == uint32_t(0x80000000),55"SetBits failed");56static_assert(SetBits<uint32_t, 1, 2>::get == uint32_t(0x00000006),57"SetBits failed");58static_assert(SetBits<uint32_t, 30, 2>::get == uint32_t(0xc0000000),59"SetBits failed");60static_assert(SetBits<uint32_t, 0, 31>::get == uint32_t(0x7FFFFFFF),61"SetBits failed");62static_assert(SetBits<uint32_t, 0, 32>::get == uint32_t(0xFFFFFFFF),63"SetBits failed");64static_assert(SetBits<uint32_t, 16, 16>::get == uint32_t(0xFFFF0000),65"SetBits failed");6667static_assert(SetBits<uint64_t, 0, 1>::get == uint64_t(0x0000000000000001LL),68"SetBits failed");69static_assert(SetBits<uint64_t, 63, 1>::get == uint64_t(0x8000000000000000LL),70"SetBits failed");71static_assert(SetBits<uint64_t, 62, 2>::get == uint64_t(0xc000000000000000LL),72"SetBits failed");73static_assert(SetBits<uint64_t, 31, 1>::get == uint64_t(0x0000000080000000LL),74"SetBits failed");75static_assert(SetBits<uint64_t, 16, 16>::get == uint64_t(0x00000000FFFF0000LL),76"SetBits failed");7778} // namespace spvutils7980#endif // LIBSPIRV_UTIL_BITUTILS_H_818283