1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | #ifndef sw_Types_hpp |
16 | #define sw_Types_hpp |
17 | |
18 | #include <limits> |
19 | #include <type_traits> |
20 | |
21 | // 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. |
22 | // 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. |
23 | #if defined(__GNUC__) && !defined(__clang__) |
24 | namespace {enum E {}; static_assert(!std::numeric_limits<std::underlying_type<E>::type>::is_signed, "expected unscoped enum whose underlying type is not fixed to be unsigned" );} |
25 | #define ENUM_UNDERLYING_TYPE_UNSIGNED_INT |
26 | #else |
27 | #define ENUM_UNDERLYING_TYPE_UNSIGNED_INT : unsigned int |
28 | #endif |
29 | |
30 | #if defined(_MSC_VER) |
31 | typedef signed __int8 int8_t; |
32 | typedef signed __int16 int16_t; |
33 | typedef signed __int32 int32_t; |
34 | typedef signed __int64 int64_t; |
35 | typedef unsigned __int8 uint8_t; |
36 | typedef unsigned __int16 uint16_t; |
37 | typedef unsigned __int32 uint32_t; |
38 | typedef unsigned __int64 uint64_t; |
39 | #define ALIGN(bytes, type) __declspec(align(bytes)) type |
40 | #else |
41 | #include <stdint.h> |
42 | #define ALIGN(bytes, type) type __attribute__((aligned(bytes))) |
43 | #endif |
44 | |
45 | namespace sw |
46 | { |
47 | typedef ALIGN(1, uint8_t) byte; |
48 | typedef ALIGN(2, uint16_t) word; |
49 | typedef ALIGN(4, uint32_t) dword; |
50 | typedef ALIGN(8, uint64_t) qword; |
51 | typedef ALIGN(16, uint64_t) qword2[2]; |
52 | typedef ALIGN(4, uint8_t) byte4[4]; |
53 | typedef ALIGN(8, uint8_t) byte8[8]; |
54 | typedef ALIGN(16, uint8_t) byte16[16]; |
55 | typedef ALIGN(8, uint16_t) word4[4]; |
56 | typedef ALIGN(8, uint32_t) dword2[2]; |
57 | typedef ALIGN(16, uint32_t) dword4[4]; |
58 | typedef ALIGN(16, uint64_t) xword[2]; |
59 | |
60 | typedef ALIGN(1, int8_t) sbyte; |
61 | typedef ALIGN(4, int8_t) sbyte4[4]; |
62 | typedef ALIGN(8, int8_t) sbyte8[8]; |
63 | typedef ALIGN(16, int8_t) sbyte16[16]; |
64 | typedef ALIGN(8, short) short4[4]; |
65 | typedef ALIGN(8, unsigned short) ushort4[4]; |
66 | typedef ALIGN(16, short) short8[8]; |
67 | typedef ALIGN(16, unsigned short) ushort8[8]; |
68 | typedef ALIGN(8, int) int2[2]; |
69 | typedef ALIGN(8, unsigned int) uint2[2]; |
70 | typedef ALIGN(16, unsigned int) uint4[4]; |
71 | |
72 | typedef ALIGN(8, float) float2[2]; |
73 | |
74 | ALIGN(16, struct int4 |
75 | { |
76 | int x; |
77 | int y; |
78 | int z; |
79 | int w; |
80 | |
81 | int &operator[](int i) |
82 | { |
83 | return (&x)[i]; |
84 | } |
85 | |
86 | const int &operator[](int i) const |
87 | { |
88 | return (&x)[i]; |
89 | } |
90 | |
91 | bool operator!=(const int4 &rhs) |
92 | { |
93 | return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w; |
94 | } |
95 | |
96 | bool operator==(const int4 &rhs) |
97 | { |
98 | return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w; |
99 | } |
100 | }); |
101 | |
102 | ALIGN(16, struct float4 |
103 | { |
104 | float x; |
105 | float y; |
106 | float z; |
107 | float w; |
108 | |
109 | float &operator[](int i) |
110 | { |
111 | return (&x)[i]; |
112 | } |
113 | |
114 | const float &operator[](int i) const |
115 | { |
116 | return (&x)[i]; |
117 | } |
118 | |
119 | bool operator!=(const float4 &rhs) |
120 | { |
121 | return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w; |
122 | } |
123 | |
124 | bool operator==(const float4 &rhs) |
125 | { |
126 | return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w; |
127 | } |
128 | }); |
129 | |
130 | inline constexpr float4 vector(float x, float y, float z, float w) |
131 | { |
132 | return { x, y, z, w }; |
133 | } |
134 | |
135 | inline constexpr float4 replicate(float f) |
136 | { |
137 | return vector(f, f, f, f); |
138 | } |
139 | |
140 | #define OFFSET(s,m) (int)(size_t)&reinterpret_cast<const volatile char&>((((s*)0)->m)) |
141 | } |
142 | |
143 | #endif // sw_Types_hpp |
144 | |