Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2017 The Dawn & Tint Authors |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // 3. Neither the name of the copyright holder nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 27 | |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 28 | #ifndef WEBGPU_ENUM_CLASS_BITMASKS_H_ |
| 29 | #define WEBGPU_ENUM_CLASS_BITMASKS_H_ |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 30 | |
| 31 | #include <type_traits> |
| 32 | |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 33 | // The operators in wgpu:: namespace need be introduced into other namespaces with |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 34 | // using-declarations for C++ Argument Dependent Lookup to work. |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 35 | #define WGPU_IMPORT_BITMASK_OPERATORS \ |
| 36 | using wgpu::operator|; \ |
| 37 | using wgpu::operator&; \ |
| 38 | using wgpu::operator^; \ |
| 39 | using wgpu::operator~; \ |
| 40 | using wgpu::operator&=; \ |
| 41 | using wgpu::operator|=; \ |
| 42 | using wgpu::operator^=; \ |
| 43 | using wgpu::HasZeroOrOneBits; |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 44 | |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 45 | namespace wgpu { |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 46 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 47 | template <typename T> |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 48 | struct IsWGPUBitmask { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 49 | static constexpr bool enable = false; |
| 50 | }; |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 51 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 52 | template <typename T, typename Enable = void> |
| 53 | struct LowerBitmask { |
| 54 | static constexpr bool enable = false; |
| 55 | }; |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 56 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 57 | template <typename T> |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 58 | struct LowerBitmask<T, typename std::enable_if<IsWGPUBitmask<T>::enable>::type> { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | static constexpr bool enable = true; |
| 60 | using type = T; |
| 61 | constexpr static T Lower(T t) { return t; } |
| 62 | }; |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 63 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 64 | template <typename T> |
| 65 | struct BoolConvertible { |
| 66 | using Integral = typename std::underlying_type<T>::type; |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 67 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 68 | // NOLINTNEXTLINE(runtime/explicit) |
Corentin Wallez | 40358fd | 2023-01-30 13:51:32 +0000 | [diff] [blame] | 69 | explicit constexpr BoolConvertible(Integral value) : value(value) {} |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 70 | constexpr operator bool() const { return value != 0; } |
| 71 | constexpr operator T() const { return static_cast<T>(value); } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 72 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 73 | Integral value; |
| 74 | }; |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 75 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 76 | template <typename T> |
| 77 | struct LowerBitmask<BoolConvertible<T>> { |
| 78 | static constexpr bool enable = true; |
| 79 | using type = T; |
| 80 | static constexpr type Lower(BoolConvertible<T> t) { return t; } |
| 81 | }; |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 82 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 83 | template < |
| 84 | typename T1, |
| 85 | typename T2, |
| 86 | typename = typename std::enable_if<LowerBitmask<T1>::enable && LowerBitmask<T2>::enable>::type> |
| 87 | constexpr BoolConvertible<typename LowerBitmask<T1>::type> operator|(T1 left, T2 right) { |
| 88 | using T = typename LowerBitmask<T1>::type; |
| 89 | using Integral = typename std::underlying_type<T>::type; |
Corentin Wallez | 40358fd | 2023-01-30 13:51:32 +0000 | [diff] [blame] | 90 | return BoolConvertible<T>(static_cast<Integral>(LowerBitmask<T1>::Lower(left)) | |
| 91 | static_cast<Integral>(LowerBitmask<T2>::Lower(right))); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 92 | } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 93 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 94 | template < |
| 95 | typename T1, |
| 96 | typename T2, |
| 97 | typename = typename std::enable_if<LowerBitmask<T1>::enable && LowerBitmask<T2>::enable>::type> |
| 98 | constexpr BoolConvertible<typename LowerBitmask<T1>::type> operator&(T1 left, T2 right) { |
| 99 | using T = typename LowerBitmask<T1>::type; |
| 100 | using Integral = typename std::underlying_type<T>::type; |
Corentin Wallez | 40358fd | 2023-01-30 13:51:32 +0000 | [diff] [blame] | 101 | return BoolConvertible<T>(static_cast<Integral>(LowerBitmask<T1>::Lower(left)) & |
| 102 | static_cast<Integral>(LowerBitmask<T2>::Lower(right))); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 103 | } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 104 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 105 | template < |
| 106 | typename T1, |
| 107 | typename T2, |
| 108 | typename = typename std::enable_if<LowerBitmask<T1>::enable && LowerBitmask<T2>::enable>::type> |
| 109 | constexpr BoolConvertible<typename LowerBitmask<T1>::type> operator^(T1 left, T2 right) { |
| 110 | using T = typename LowerBitmask<T1>::type; |
| 111 | using Integral = typename std::underlying_type<T>::type; |
Corentin Wallez | 40358fd | 2023-01-30 13:51:32 +0000 | [diff] [blame] | 112 | return BoolConvertible<T>(static_cast<Integral>(LowerBitmask<T1>::Lower(left)) ^ |
| 113 | static_cast<Integral>(LowerBitmask<T2>::Lower(right))); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 114 | } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 115 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 116 | template <typename T1> |
| 117 | constexpr BoolConvertible<typename LowerBitmask<T1>::type> operator~(T1 t) { |
| 118 | using T = typename LowerBitmask<T1>::type; |
| 119 | using Integral = typename std::underlying_type<T>::type; |
Corentin Wallez | 40358fd | 2023-01-30 13:51:32 +0000 | [diff] [blame] | 120 | return BoolConvertible<T>(~static_cast<Integral>(LowerBitmask<T1>::Lower(t))); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 121 | } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 122 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 123 | template < |
| 124 | typename T, |
| 125 | typename T2, |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 126 | typename = typename std::enable_if<IsWGPUBitmask<T>::enable && LowerBitmask<T2>::enable>::type> |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 127 | constexpr T& operator&=(T& l, T2 right) { |
| 128 | T r = LowerBitmask<T2>::Lower(right); |
| 129 | l = l & r; |
| 130 | return l; |
| 131 | } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 132 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 133 | template < |
| 134 | typename T, |
| 135 | typename T2, |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 136 | typename = typename std::enable_if<IsWGPUBitmask<T>::enable && LowerBitmask<T2>::enable>::type> |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 137 | constexpr T& operator|=(T& l, T2 right) { |
| 138 | T r = LowerBitmask<T2>::Lower(right); |
| 139 | l = l | r; |
| 140 | return l; |
| 141 | } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 142 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 143 | template < |
| 144 | typename T, |
| 145 | typename T2, |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 146 | typename = typename std::enable_if<IsWGPUBitmask<T>::enable && LowerBitmask<T2>::enable>::type> |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 147 | constexpr T& operator^=(T& l, T2 right) { |
| 148 | T r = LowerBitmask<T2>::Lower(right); |
| 149 | l = l ^ r; |
| 150 | return l; |
| 151 | } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 152 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 153 | template <typename T> |
| 154 | constexpr bool HasZeroOrOneBits(T value) { |
| 155 | using Integral = typename std::underlying_type<T>::type; |
| 156 | return (static_cast<Integral>(value) & (static_cast<Integral>(value) - 1)) == 0; |
| 157 | } |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 158 | |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 159 | } // namespace wgpu |
Ben Clayton | 9fb7a51 | 2022-02-04 18:18:18 +0000 | [diff] [blame] | 160 | |
Kai Ninomiya | 6889227 | 2024-01-04 10:02:49 +0000 | [diff] [blame] | 161 | #endif // WEBGPU_ENUM_CLASS_BITMASKS_H_ |