// Copyright 2016 The SwiftShader Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef sw_Types_hpp #define sw_Types_hpp #include #include // GCC warns against bitfields not fitting the entire range of an enum with a fixed underlying type of unsigned int, which gets promoted to an error with -Werror and cannot be suppressed. // However, GCC already defaults to using unsigned int as the underlying type of an unscoped enum without a fixed underlying type. So we can just omit it. #if defined(__GNUC__) && !defined(__clang__) namespace { enum E { }; static_assert(!std::numeric_limits::type>::is_signed, "expected unscoped enum whose underlying type is not fixed to be unsigned"); } // namespace # define ENUM_UNDERLYING_TYPE_UNSIGNED_INT #else # define ENUM_UNDERLYING_TYPE_UNSIGNED_INT : unsigned int #endif #if defined(_MSC_VER) typedef signed __int8 int8_t; typedef signed __int16 int16_t; typedef signed __int32 int32_t; typedef signed __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; # define ALIGN(bytes, type) __declspec(align(bytes)) type #else # include # define ALIGN(bytes, type) type __attribute__((aligned(bytes))) #endif namespace sw { // https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 constexpr inline uint32_t bit_ceil(uint32_t i) { i--; i |= i >> 1; i |= i >> 2; i |= i >> 4; i |= i >> 8; i |= i >> 16; i++; return i; } typedef ALIGN(1, uint8_t) byte; typedef ALIGN(2, uint16_t) word; typedef ALIGN(4, uint32_t) dword; typedef ALIGN(8, uint64_t) qword; typedef ALIGN(1, int8_t) sbyte; template struct alignas(sizeof(T) * bit_ceil(N)) vec { vec() = default; constexpr explicit vec(T replicate) { for(int i = 0; i < N; i++) { v[i] = replicate; } } template constexpr vec(T arg0, ARGS... args) : v{ arg0, args... } { } // Require explicit use of replicate constructor. vec &operator=(T) = delete; T &operator[](int i) { return v[i]; } const T &operator[](int i) const { return v[i]; } T v[N]; }; template struct alignas(sizeof(T) * 4) vec { vec() = default; constexpr explicit vec(T replicate) : x(replicate) , y(replicate) , z(replicate) , w(replicate) { } constexpr vec(T x, T y, T z, T w) : x(x) , y(y) , z(z) , w(w) { } // Require explicit use of replicate constructor. vec &operator=(T) = delete; T &operator[](int i) { return v[i]; } const T &operator[](int i) const { return v[i]; } union { T v[4]; struct { T x; T y; T z; T w; }; }; }; template bool operator==(const vec &a, const vec &b) { for(int i = 0; i < N; i++) { if(a.v[i] != b.v[i]) { return false; } } return true; } template bool operator!=(const vec &a, const vec &b) { return !(a == b); } template using vec2 = vec; template using vec3 = vec; // aligned to 4 elements template using vec4 = vec; template using vec8 = vec; template using vec16 = vec; using int2 = vec2; using uint2 = vec2; using float2 = vec2; using dword2 = vec2; using qword2 = vec2; // Note: These vec3 types all use 4-element alignment - i.e. they have // identical memory layout to vec4, except they do not have a 4th component. using int3 = vec3; using uint3 = vec3; using float3 = vec3; using dword3 = vec3; using int4 = vec4; using uint4 = vec4; using float4 = vec4; using byte4 = vec4; using sbyte4 = vec4; using short4 = vec4; using ushort4 = vec4; using word4 = vec4; using dword4 = vec4; using byte8 = vec8; using sbyte8 = vec8; using short8 = vec8; using ushort8 = vec8; using byte16 = vec16; using sbyte16 = vec16; inline constexpr float4 vector(float x, float y, float z, float w) { return float4{ x, y, z, w }; } inline constexpr float4 replicate(float f) { return vector(f, f, f, f); } } // namespace sw #endif // sw_Types_hpp