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 <assert.h>
19#include <limits>
20#include <type_traits>
21
22// 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.
23// 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.
24#if defined(__GNUC__) && !defined(__clang__)
25namespace {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");}
26#define ENUM_UNDERLYING_TYPE_UNSIGNED_INT
27#else
28#define ENUM_UNDERLYING_TYPE_UNSIGNED_INT : unsigned int
29#endif
30
31#if defined(_MSC_VER)
32 typedef signed __int8 int8_t;
33 typedef signed __int16 int16_t;
34 typedef signed __int32 int32_t;
35 typedef signed __int64 int64_t;
36 typedef unsigned __int8 uint8_t;
37 typedef unsigned __int16 uint16_t;
38 typedef unsigned __int32 uint32_t;
39 typedef unsigned __int64 uint64_t;
40 #define ALIGN(bytes, type) __declspec(align(bytes)) type
41#else
42 #include <stdint.h>
43 #define ALIGN(bytes, type) type __attribute__((aligned(bytes)))
44#endif
45
46namespace sw
47{
48 typedef ALIGN(1, uint8_t) byte;
49 typedef ALIGN(2, uint16_t) word;
50 typedef ALIGN(4, uint32_t) dword;
51 typedef ALIGN(8, uint64_t) qword;
52 typedef ALIGN(16, uint64_t) qword2[2];
53 typedef ALIGN(4, uint8_t) byte4[4];
54 typedef ALIGN(8, uint8_t) byte8[8];
55 typedef ALIGN(16, uint8_t) byte16[16];
56 typedef ALIGN(8, uint16_t) word4[4];
57 typedef ALIGN(8, uint32_t) dword2[2];
58 typedef ALIGN(16, uint32_t) dword4[4];
59 typedef ALIGN(16, uint64_t) xword[2];
60
61 typedef ALIGN(1, int8_t) sbyte;
62 typedef ALIGN(4, int8_t) sbyte4[4];
63 typedef ALIGN(8, int8_t) sbyte8[8];
64 typedef ALIGN(16, int8_t) sbyte16[16];
65 typedef ALIGN(8, short) short4[4];
66 typedef ALIGN(8, unsigned short) ushort4[4];
67 typedef ALIGN(16, short) short8[8];
68 typedef ALIGN(16, unsigned short) ushort8[8];
69 typedef ALIGN(8, int) int2[2];
70 typedef ALIGN(8, unsigned int) uint2[2];
71 typedef ALIGN(16, unsigned int) uint4[4];
72
73 typedef ALIGN(8, float) float2[2];
74
75 ALIGN(16, struct int4
76 {
77 int x;
78 int y;
79 int z;
80 int w;
81
82 int &operator[](int i)
83 {
84 return (&x)[i];
85 }
86
87 const int &operator[](int i) const
88 {
89 return (&x)[i];
90 }
91
92 bool operator!=(const int4 &rhs)
93 {
94 return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w;
95 }
96
97 bool operator==(const int4 &rhs)
98 {
99 return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
100 }
101 });
102
103 ALIGN(16, struct float4
104 {
105 float x;
106 float y;
107 float z;
108 float w;
109
110 float &operator[](int i)
111 {
112 return (&x)[i];
113 }
114
115 const float &operator[](int i) const
116 {
117 return (&x)[i];
118 }
119
120 bool operator!=(const float4 &rhs)
121 {
122 return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w;
123 }
124
125 bool operator==(const float4 &rhs)
126 {
127 return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
128 }
129 });
130
131 inline float4 vector(float x, float y, float z, float w)
132 {
133 float4 v;
134
135 v.x = x;
136 v.y = y;
137 v.z = z;
138 v.w = w;
139
140 return v;
141 }
142
143 inline float4 replicate(float f)
144 {
145 float4 v;
146
147 v.x = f;
148 v.y = f;
149 v.z = f;
150 v.w = f;
151
152 return v;
153 }
154
155 // The OFFSET macro is a generalization of the offsetof() macro defined in <cstddef>.
156 // It allows e.g. getting the offset of array elements, even when indexed dynamically.
157 // We cast the address '32' and subtract it again, because null-dereference is undefined behavior.
158 #define OFFSET(s,m) ((int)(size_t)&reinterpret_cast<const volatile char&>((((s*)32)->m)) - 32)
159}
160
161#endif // sw_Types_hpp
162