ScalarType.h (4975B)
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/. */ 6 7 /* An enumeration of all possible element types in typed data. */ 8 9 #ifndef js_ScalarType_h 10 #define js_ScalarType_h 11 12 #include "mozilla/Assertions.h" // MOZ_CRASH 13 14 #include <stddef.h> // size_t 15 16 namespace JS { 17 18 namespace Scalar { 19 20 // Scalar types that can appear in typed arrays. 21 // The enum values must be kept in sync with: 22 // 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 29 // 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, 44 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. 48 */ 49 Uint8Clamped, 50 51 BigInt64, 52 BigUint64, 53 54 Float16, 55 56 /** 57 * Types that don't have their own TypedArray equivalent, for now. 58 * E.g. DataView 59 */ 60 MaxTypedArrayViewType, 61 62 Int64, 63 Simd128, 64 }; 65 66 static inline size_t byteSize(Type atype) { 67 switch (atype) { 68 case Int8: 69 case Uint8: 70 case Uint8Clamped: 71 return 1; 72 case Int16: 73 case Uint16: 74 case Float16: 75 return 2; 76 case Int32: 77 case Uint32: 78 case Float32: 79 return 4; 80 case Int64: 81 case Float64: 82 case BigInt64: 83 case BigUint64: 84 return 8; 85 case Simd128: 86 return 16; 87 case MaxTypedArrayViewType: 88 break; 89 } 90 MOZ_CRASH("invalid scalar type"); 91 } 92 93 static inline bool isSignedIntType(Type atype) { 94 switch (atype) { 95 case Int8: 96 case Int16: 97 case Int32: 98 case Int64: 99 case BigInt64: 100 return true; 101 case Uint8: 102 case Uint8Clamped: 103 case Uint16: 104 case Uint32: 105 case Float16: 106 case Float32: 107 case Float64: 108 case BigUint64: 109 case Simd128: 110 return false; 111 case MaxTypedArrayViewType: 112 break; 113 } 114 MOZ_CRASH("invalid scalar type"); 115 } 116 117 static inline bool isBigIntType(Type atype) { 118 switch (atype) { 119 case BigInt64: 120 case BigUint64: 121 return true; 122 case Int8: 123 case Int16: 124 case Int32: 125 case Int64: 126 case Uint8: 127 case Uint8Clamped: 128 case Uint16: 129 case Uint32: 130 case Float16: 131 case Float32: 132 case Float64: 133 case Simd128: 134 return false; 135 case MaxTypedArrayViewType: 136 break; 137 } 138 MOZ_CRASH("invalid scalar type"); 139 } 140 141 static inline bool isFloatingType(Type atype) { 142 switch (atype) { 143 case Int8: 144 case Uint8: 145 case Uint8Clamped: 146 case Int16: 147 case Uint16: 148 case Int32: 149 case Uint32: 150 case Int64: 151 case BigInt64: 152 case BigUint64: 153 return false; 154 case Float16: 155 case Float32: 156 case Float64: 157 case Simd128: 158 return true; 159 case MaxTypedArrayViewType: 160 break; 161 } 162 MOZ_CRASH("invalid scalar type"); 163 } 164 165 static inline const char* name(Type atype) { 166 switch (atype) { 167 case Int8: 168 return "Int8"; 169 case Uint8: 170 return "Uint8"; 171 case Int16: 172 return "Int16"; 173 case Uint16: 174 return "Uint16"; 175 case Int32: 176 return "Int32"; 177 case Uint32: 178 return "Uint32"; 179 case Float16: 180 return "Float16"; 181 case Float32: 182 return "Float32"; 183 case Float64: 184 return "Float64"; 185 case Uint8Clamped: 186 return "Uint8Clamped"; 187 case BigInt64: 188 return "BigInt64"; 189 case BigUint64: 190 return "BigUint64"; 191 case MaxTypedArrayViewType: 192 return "MaxTypedArrayViewType"; 193 case Int64: 194 return "Int64"; 195 case Simd128: 196 return "Simd128"; 197 } 198 MOZ_CRASH("invalid scalar type"); 199 } 200 201 static inline const char* byteSizeString(Type atype) { 202 switch (atype) { 203 case Int8: 204 case Uint8: 205 case Uint8Clamped: 206 return "1"; 207 case Int16: 208 case Uint16: 209 case Float16: 210 return "2"; 211 case Int32: 212 case Uint32: 213 case Float32: 214 return "4"; 215 case Int64: 216 case Float64: 217 case BigInt64: 218 case BigUint64: 219 return "8"; 220 case Simd128: 221 return "16"; 222 case MaxTypedArrayViewType: 223 break; 224 } 225 MOZ_CRASH("invalid scalar type"); 226 } 227 228 } // namespace Scalar 229 230 } // namespace JS 231 232 namespace js { 233 234 // This is aliased in NamespaceImports.h, but that is internal-only and 235 // inaccessible to Gecko code, which uses this type fairly heavily. Until such 236 // uses are changed, we need the alias here as well. 237 namespace Scalar = JS::Scalar; 238 239 } // namespace js 240 241 #endif // js_ScalarType_h