Home | History | Annotate | Download | only in util
      1 // Copyright (c) 2015-2016 The Khronos Group Inc.
      2 //
      3 // Permission is hereby granted, free of charge, to any person obtaining a
      4 // copy of this software and/or associated documentation files (the
      5 // "Materials"), to deal in the Materials without restriction, including
      6 // without limitation the rights to use, copy, modify, merge, publish,
      7 // distribute, sublicense, and/or sell copies of the Materials, and to
      8 // permit persons to whom the Materials are furnished to do so, subject to
      9 // the following conditions:
     10 //
     11 // The above copyright notice and this permission notice shall be included
     12 // in all copies or substantial portions of the Materials.
     13 //
     14 // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
     15 // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
     16 // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
     17 //    https://www.khronos.org/registry/
     18 //
     19 // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     22 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     23 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     24 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     25 // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
     26 
     27 #ifndef LIBSPIRV_UTIL_BITUTILS_H_
     28 #define LIBSPIRV_UTIL_BITUTILS_H_
     29 
     30 #include <cstdint>
     31 #include <cstring>
     32 
     33 namespace spvutils {
     34 
     35 // Performs a bitwise copy of source to the destination type Dest.
     36 template <typename Dest, typename Src>
     37 Dest BitwiseCast(Src source) {
     38   Dest dest;
     39   static_assert(sizeof(source) == sizeof(dest),
     40                 "BitwiseCast: Source and destination must have the same size");
     41   std::memcpy(&dest, &source, sizeof(dest));
     42   return dest;
     43 }
     44 
     45 // SetBits<T, First, Num> returns an integer of type <T> with bits set
     46 // for position <First> through <First + Num - 1>, counting from the least
     47 // significant bit. In particular when Num == 0, no positions are set to 1.
     48 // A static assert will be triggered if First + Num > sizeof(T) * 8, that is,
     49 // a bit that will not fit in the underlying type is set.
     50 template <typename T, size_t First = 0, size_t Num = 0>
     51 struct SetBits {
     52   static_assert(First < sizeof(T) * 8,
     53                 "Tried to set a bit that is shifted too far.");
     54   const static T get = (T(1) << First) | SetBits<T, First + 1, Num - 1>::get;
     55 };
     56 
     57 template <typename T, size_t Last>
     58 struct SetBits<T, Last, 0> {
     59   const static T get = T(0);
     60 };
     61 
     62 // This is all compile-time so we can put our tests right here.
     63 static_assert(SetBits<uint32_t, 0, 0>::get == uint32_t(0x00000000),
     64               "SetBits failed");
     65 static_assert(SetBits<uint32_t, 0, 1>::get == uint32_t(0x00000001),
     66               "SetBits failed");
     67 static_assert(SetBits<uint32_t, 31, 1>::get == uint32_t(0x80000000),
     68               "SetBits failed");
     69 static_assert(SetBits<uint32_t, 1, 2>::get == uint32_t(0x00000006),
     70               "SetBits failed");
     71 static_assert(SetBits<uint32_t, 30, 2>::get == uint32_t(0xc0000000),
     72               "SetBits failed");
     73 static_assert(SetBits<uint32_t, 0, 31>::get == uint32_t(0x7FFFFFFF),
     74               "SetBits failed");
     75 static_assert(SetBits<uint32_t, 0, 32>::get == uint32_t(0xFFFFFFFF),
     76               "SetBits failed");
     77 static_assert(SetBits<uint32_t, 16, 16>::get == uint32_t(0xFFFF0000),
     78               "SetBits failed");
     79 
     80 static_assert(SetBits<uint64_t, 0, 1>::get == uint64_t(0x0000000000000001LL),
     81               "SetBits failed");
     82 static_assert(SetBits<uint64_t, 63, 1>::get == uint64_t(0x8000000000000000LL),
     83               "SetBits failed");
     84 static_assert(SetBits<uint64_t, 62, 2>::get == uint64_t(0xc000000000000000LL),
     85               "SetBits failed");
     86 static_assert(SetBits<uint64_t, 31, 1>::get == uint64_t(0x0000000080000000LL),
     87               "SetBits failed");
     88 static_assert(SetBits<uint64_t, 16, 16>::get == uint64_t(0x00000000FFFF0000LL),
     89               "SetBits failed");
     90 
     91 }  // namespace spvutils
     92 
     93 #endif  // LIBSPIRV_UTIL_BITUTILS_H_
     94