Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / js / public / ScalarType.h
blob3c98fa204cb6590bbc56715f93a47986bd44b3e7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* An enumeration of all possible element types in typed data. */
9 #ifndef js_ScalarType_h
10 #define js_ScalarType_h
12 #include "mozilla/Assertions.h" // MOZ_CRASH
14 #include <stddef.h> // size_t
16 namespace JS {
18 namespace Scalar {
20 // Scalar types that can appear in typed arrays.
21 // The enum values must be kept in sync with:
23 // * the TYPEDARRAY_KIND constants
24 // * the SCTAG_TYPED_ARRAY constants
25 // * JS_FOR_EACH_TYPED_ARRAY
26 // * JS_FOR_PROTOTYPES_
27 // * JS_FOR_EACH_UNIQUE_SCALAR_TYPE_REPR_CTYPE
28 // * JIT compilation
30 // and the existing entries here must not be renumbered, since they are
31 // necessary for backwards compatibility with structured clones from previous
32 // versions. (It is fine to add new entries and increment
33 // MaxTypedArrayViewType, or change anything at or after
34 // MaxTypedArrayViewType.)
35 enum Type {
36 Int8 = 0,
37 Uint8,
38 Int16,
39 Uint16,
40 Int32,
41 Uint32,
42 Float32,
43 Float64,
45 /**
46 * Special type that is a uint8_t, but assignments are clamped to [0, 256).
47 * Treat the raw data type as a uint8_t.
49 Uint8Clamped,
51 BigInt64,
52 BigUint64,
54 /**
55 * Types that don't have their own TypedArray equivalent, for now.
56 * E.g. DataView
58 MaxTypedArrayViewType,
60 Int64,
61 Simd128,
64 static inline size_t byteSize(Type atype) {
65 switch (atype) {
66 case Int8:
67 case Uint8:
68 case Uint8Clamped:
69 return 1;
70 case Int16:
71 case Uint16:
72 return 2;
73 case Int32:
74 case Uint32:
75 case Float32:
76 return 4;
77 case Int64:
78 case Float64:
79 case BigInt64:
80 case BigUint64:
81 return 8;
82 case Simd128:
83 return 16;
84 case MaxTypedArrayViewType:
85 break;
87 MOZ_CRASH("invalid scalar type");
90 static inline bool isSignedIntType(Type atype) {
91 switch (atype) {
92 case Int8:
93 case Int16:
94 case Int32:
95 case Int64:
96 case BigInt64:
97 return true;
98 case Uint8:
99 case Uint8Clamped:
100 case Uint16:
101 case Uint32:
102 case Float32:
103 case Float64:
104 case BigUint64:
105 case Simd128:
106 return false;
107 case MaxTypedArrayViewType:
108 break;
110 MOZ_CRASH("invalid scalar type");
113 static inline bool isBigIntType(Type atype) {
114 switch (atype) {
115 case BigInt64:
116 case BigUint64:
117 return true;
118 case Int8:
119 case Int16:
120 case Int32:
121 case Int64:
122 case Uint8:
123 case Uint8Clamped:
124 case Uint16:
125 case Uint32:
126 case Float32:
127 case Float64:
128 case Simd128:
129 return false;
130 case MaxTypedArrayViewType:
131 break;
133 MOZ_CRASH("invalid scalar type");
136 static inline bool isFloatingType(Type atype) {
137 switch (atype) {
138 case Int8:
139 case Uint8:
140 case Uint8Clamped:
141 case Int16:
142 case Uint16:
143 case Int32:
144 case Uint32:
145 case Int64:
146 case BigInt64:
147 case BigUint64:
148 return false;
149 case Float32:
150 case Float64:
151 case Simd128:
152 return true;
153 case MaxTypedArrayViewType:
154 break;
156 MOZ_CRASH("invalid scalar type");
159 static inline const char* name(Type atype) {
160 switch (atype) {
161 case Int8:
162 return "Int8";
163 case Uint8:
164 return "Uint8";
165 case Int16:
166 return "Int16";
167 case Uint16:
168 return "Uint16";
169 case Int32:
170 return "Int32";
171 case Uint32:
172 return "Uint32";
173 case Float32:
174 return "Float32";
175 case Float64:
176 return "Float64";
177 case Uint8Clamped:
178 return "Uint8Clamped";
179 case BigInt64:
180 return "BigInt64";
181 case BigUint64:
182 return "BigUint64";
183 case MaxTypedArrayViewType:
184 return "MaxTypedArrayViewType";
185 case Int64:
186 return "Int64";
187 case Simd128:
188 return "Simd128";
190 MOZ_CRASH("invalid scalar type");
193 static inline const char* byteSizeString(Type atype) {
194 switch (atype) {
195 case Int8:
196 case Uint8:
197 case Uint8Clamped:
198 return "1";
199 case Int16:
200 case Uint16:
201 return "2";
202 case Int32:
203 case Uint32:
204 case Float32:
205 return "4";
206 case Int64:
207 case Float64:
208 case BigInt64:
209 case BigUint64:
210 return "8";
211 case Simd128:
212 return "16";
213 case MaxTypedArrayViewType:
214 break;
216 MOZ_CRASH("invalid scalar type");
219 } // namespace Scalar
221 } // namespace JS
223 namespace js {
225 // This is aliased in NamespaceImports.h, but that is internal-only and
226 // inaccessible to Gecko code, which uses this type fairly heavily. Until such
227 // uses are changed, we need the alias here as well.
228 namespace Scalar = JS::Scalar;
230 } // namespace js
232 #endif // js_ScalarType_h