1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef TypedArrayType_h
27#define TypedArrayType_h
28
29#include "JSType.h"
30#include <wtf/PrintStream.h>
31
32namespace JSC {
33
34struct ClassInfo;
35
36enum TypedArrayType {
37    NotTypedArray,
38    TypeInt8,
39    TypeUint8,
40    TypeUint8Clamped,
41    TypeInt16,
42    TypeUint16,
43    TypeInt32,
44    TypeUint32,
45    TypeFloat32,
46    TypeFloat64,
47    TypeDataView
48};
49
50#define NUMBER_OF_TYPED_ARRAY_TYPES TypeDataView
51
52inline unsigned toIndex(TypedArrayType type)
53{
54    return static_cast<unsigned>(type) - 1;
55}
56
57inline TypedArrayType indexToTypedArrayType(unsigned index)
58{
59    TypedArrayType result = static_cast<TypedArrayType>(index + 1);
60    ASSERT(result >= TypeInt8 && result <= TypeDataView);
61    return result;
62}
63
64inline bool isTypedView(TypedArrayType type)
65{
66    switch (type) {
67    case NotTypedArray:
68    case TypeDataView:
69        return false;
70    default:
71        return true;
72    }
73}
74
75inline unsigned logElementSize(TypedArrayType type)
76{
77    switch (type) {
78    case NotTypedArray:
79        break;
80    case TypeInt8:
81    case TypeUint8:
82    case TypeUint8Clamped:
83    case TypeDataView:
84        return 0;
85    case TypeInt16:
86    case TypeUint16:
87        return 1;
88    case TypeInt32:
89    case TypeUint32:
90    case TypeFloat32:
91        return 2;
92    case TypeFloat64:
93        return 3;
94    }
95    RELEASE_ASSERT_NOT_REACHED();
96    return 0;
97}
98
99inline size_t elementSize(TypedArrayType type)
100{
101    return static_cast<size_t>(1) << logElementSize(type);
102}
103
104const ClassInfo* constructorClassInfoForType(TypedArrayType);
105JSType typeForTypedArrayType(TypedArrayType);
106
107inline bool isInt(TypedArrayType type)
108{
109    switch (type) {
110    case TypeInt8:
111    case TypeUint8:
112    case TypeUint8Clamped:
113    case TypeInt16:
114    case TypeUint16:
115    case TypeInt32:
116    case TypeUint32:
117        return true;
118    default:
119        return false;
120    }
121}
122
123inline bool isFloat(TypedArrayType type)
124{
125    switch (type) {
126    case TypeFloat32:
127    case TypeFloat64:
128        return true;
129    default:
130        return false;
131    }
132}
133
134inline bool isSigned(TypedArrayType type)
135{
136    switch (type) {
137    case TypeInt8:
138    case TypeInt16:
139    case TypeInt32:
140    case TypeFloat32:
141    case TypeFloat64:
142        return true;
143    default:
144        return false;
145    }
146}
147
148inline bool isClamped(TypedArrayType type)
149{
150    return type == TypeUint8Clamped;
151}
152
153} // namespace JSC
154
155namespace WTF {
156
157void printInternal(PrintStream&, JSC::TypedArrayType);
158
159} // namespace WTF
160
161#endif // TypedArrayType_h
162
163