huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// 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 <limits>
#include <type_traits>
 
// 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<std::underlying_type<E>::type>::is_signed, "expected unscoped enum whose underlying type is not fixed to be unsigned");}
#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 <stdint.h>
   #define ALIGN(bytes, type) type __attribute__((aligned(bytes)))
#endif
 
namespace sw
{
   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(16, uint64_t) qword2[2];
   typedef ALIGN(4, uint8_t) byte4[4];
   typedef ALIGN(8, uint8_t) byte8[8];
   typedef ALIGN(16, uint8_t) byte16[16];
   typedef ALIGN(8, uint16_t) word4[4];
   typedef ALIGN(8, uint32_t) dword2[2];
   typedef ALIGN(16, uint32_t) dword4[4];
   typedef ALIGN(16, uint64_t) xword[2];
 
   typedef ALIGN(1, int8_t) sbyte;
   typedef ALIGN(4, int8_t) sbyte4[4];
   typedef ALIGN(8, int8_t) sbyte8[8];
   typedef ALIGN(16, int8_t) sbyte16[16];
   typedef ALIGN(8, short) short4[4];
   typedef ALIGN(8, unsigned short) ushort4[4];
   typedef ALIGN(16, short) short8[8];
   typedef ALIGN(16, unsigned short) ushort8[8];
   typedef ALIGN(8, int) int2[2];
   typedef ALIGN(8, unsigned int) uint2[2];
   typedef ALIGN(16, unsigned int) uint4[4];
 
   typedef ALIGN(8, float) float2[2];
 
   ALIGN(16, struct int4
   {
       int x;
       int y;
       int z;
       int w;
 
       int &operator[](int i)
       {
           return (&x)[i];
       }
 
       const int &operator[](int i) const
       {
           return (&x)[i];
       }
 
       bool operator!=(const int4 &rhs)
       {
           return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w;
       }
 
       bool operator==(const int4 &rhs)
       {
           return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
       }
   });
 
   ALIGN(16, struct float4
   {
       float x;
       float y;
       float z;
       float w;
 
       float &operator[](int i)
       {
           return (&x)[i];
       }
 
       const float &operator[](int i) const
       {
           return (&x)[i];
       }
 
       bool operator!=(const float4 &rhs)
       {
           return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w;
       }
 
       bool operator==(const float4 &rhs)
       {
           return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
       }
   });
 
   inline float4 vector(float x, float y, float z, float w)
   {
       float4 v;
 
       v.x = x;
       v.y = y;
       v.z = z;
       v.w = w;
 
       return v;
   }
 
   inline float4 replicate(float f)
   {
       float4 v;
 
       v.x = f;
       v.y = f;
       v.z = f;
       v.w = f;
 
       return v;
   }
 
   #define OFFSET(s,m) (int)(size_t)&reinterpret_cast<const volatile char&>((((s*)0)->m))
}
 
#endif   // sw_Types_hpp