1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if V8_TARGET_ARCH_ARM64
6
7#include "src/codegen/arm64/register-arm64.h"
8
9namespace v8 {
10namespace internal {
11
12VectorFormat VectorFormatHalfWidth(VectorFormat vform) {
13  DCHECK(vform == kFormat8H || vform == kFormat4S || vform == kFormat2D ||
14         vform == kFormatH || vform == kFormatS || vform == kFormatD);
15  switch (vform) {
16    case kFormat8H:
17      return kFormat8B;
18    case kFormat4S:
19      return kFormat4H;
20    case kFormat2D:
21      return kFormat2S;
22    case kFormatH:
23      return kFormatB;
24    case kFormatS:
25      return kFormatH;
26    case kFormatD:
27      return kFormatS;
28    default:
29      UNREACHABLE();
30  }
31}
32
33VectorFormat VectorFormatDoubleWidth(VectorFormat vform) {
34  DCHECK(vform == kFormat8B || vform == kFormat4H || vform == kFormat2S ||
35         vform == kFormatB || vform == kFormatH || vform == kFormatS);
36  switch (vform) {
37    case kFormat8B:
38      return kFormat8H;
39    case kFormat4H:
40      return kFormat4S;
41    case kFormat2S:
42      return kFormat2D;
43    case kFormatB:
44      return kFormatH;
45    case kFormatH:
46      return kFormatS;
47    case kFormatS:
48      return kFormatD;
49    default:
50      UNREACHABLE();
51  }
52}
53
54VectorFormat VectorFormatFillQ(VectorFormat vform) {
55  switch (vform) {
56    case kFormatB:
57    case kFormat8B:
58    case kFormat16B:
59      return kFormat16B;
60    case kFormatH:
61    case kFormat4H:
62    case kFormat8H:
63      return kFormat8H;
64    case kFormatS:
65    case kFormat2S:
66    case kFormat4S:
67      return kFormat4S;
68    case kFormatD:
69    case kFormat1D:
70    case kFormat2D:
71      return kFormat2D;
72    default:
73      UNREACHABLE();
74  }
75}
76
77VectorFormat VectorFormatHalfWidthDoubleLanes(VectorFormat vform) {
78  switch (vform) {
79    case kFormat4H:
80      return kFormat8B;
81    case kFormat8H:
82      return kFormat16B;
83    case kFormat2S:
84      return kFormat4H;
85    case kFormat4S:
86      return kFormat8H;
87    case kFormat1D:
88      return kFormat2S;
89    case kFormat2D:
90      return kFormat4S;
91    default:
92      UNREACHABLE();
93  }
94}
95
96VectorFormat VectorFormatDoubleLanes(VectorFormat vform) {
97  DCHECK(vform == kFormat8B || vform == kFormat4H || vform == kFormat2S);
98  switch (vform) {
99    case kFormat8B:
100      return kFormat16B;
101    case kFormat4H:
102      return kFormat8H;
103    case kFormat2S:
104      return kFormat4S;
105    default:
106      UNREACHABLE();
107  }
108}
109
110VectorFormat VectorFormatHalfLanes(VectorFormat vform) {
111  DCHECK(vform == kFormat16B || vform == kFormat8H || vform == kFormat4S);
112  switch (vform) {
113    case kFormat16B:
114      return kFormat8B;
115    case kFormat8H:
116      return kFormat4H;
117    case kFormat4S:
118      return kFormat2S;
119    default:
120      UNREACHABLE();
121  }
122}
123
124VectorFormat ScalarFormatFromLaneSize(int laneSize) {
125  switch (laneSize) {
126    case 8:
127      return kFormatB;
128    case 16:
129      return kFormatH;
130    case 32:
131      return kFormatS;
132    case 64:
133      return kFormatD;
134    default:
135      UNREACHABLE();
136  }
137}
138
139VectorFormat VectorFormatFillQ(int laneSize) {
140  return VectorFormatFillQ(ScalarFormatFromLaneSize(laneSize));
141}
142
143VectorFormat ScalarFormatFromFormat(VectorFormat vform) {
144  return ScalarFormatFromLaneSize(LaneSizeInBitsFromFormat(vform));
145}
146
147unsigned RegisterSizeInBytesFromFormat(VectorFormat vform) {
148  return RegisterSizeInBitsFromFormat(vform) / 8;
149}
150
151unsigned RegisterSizeInBitsFromFormat(VectorFormat vform) {
152  DCHECK_NE(vform, kFormatUndefined);
153  switch (vform) {
154    case kFormatB:
155      return kBRegSizeInBits;
156    case kFormatH:
157      return kHRegSizeInBits;
158    case kFormatS:
159      return kSRegSizeInBits;
160    case kFormatD:
161      return kDRegSizeInBits;
162    case kFormat8B:
163    case kFormat4H:
164    case kFormat2S:
165    case kFormat1D:
166      return kDRegSizeInBits;
167    default:
168      return kQRegSizeInBits;
169  }
170}
171
172unsigned LaneSizeInBitsFromFormat(VectorFormat vform) {
173  DCHECK_NE(vform, kFormatUndefined);
174  switch (vform) {
175    case kFormatB:
176    case kFormat8B:
177    case kFormat16B:
178      return 8;
179    case kFormatH:
180    case kFormat4H:
181    case kFormat8H:
182      return 16;
183    case kFormatS:
184    case kFormat2S:
185    case kFormat4S:
186      return 32;
187    case kFormatD:
188    case kFormat1D:
189    case kFormat2D:
190      return 64;
191    default:
192      UNREACHABLE();
193  }
194}
195
196int LaneSizeInBytesFromFormat(VectorFormat vform) {
197  return LaneSizeInBitsFromFormat(vform) / 8;
198}
199
200int LaneSizeInBytesLog2FromFormat(VectorFormat vform) {
201  DCHECK_NE(vform, kFormatUndefined);
202  switch (vform) {
203    case kFormatB:
204    case kFormat8B:
205    case kFormat16B:
206      return 0;
207    case kFormatH:
208    case kFormat4H:
209    case kFormat8H:
210      return 1;
211    case kFormatS:
212    case kFormat2S:
213    case kFormat4S:
214      return 2;
215    case kFormatD:
216    case kFormat1D:
217    case kFormat2D:
218      return 3;
219    default:
220      UNREACHABLE();
221  }
222}
223
224int LaneCountFromFormat(VectorFormat vform) {
225  DCHECK_NE(vform, kFormatUndefined);
226  switch (vform) {
227    case kFormat16B:
228      return 16;
229    case kFormat8B:
230    case kFormat8H:
231      return 8;
232    case kFormat4H:
233    case kFormat4S:
234      return 4;
235    case kFormat2S:
236    case kFormat2D:
237      return 2;
238    case kFormat1D:
239    case kFormatB:
240    case kFormatH:
241    case kFormatS:
242    case kFormatD:
243      return 1;
244    default:
245      UNREACHABLE();
246  }
247}
248
249int MaxLaneCountFromFormat(VectorFormat vform) {
250  DCHECK_NE(vform, kFormatUndefined);
251  switch (vform) {
252    case kFormatB:
253    case kFormat8B:
254    case kFormat16B:
255      return 16;
256    case kFormatH:
257    case kFormat4H:
258    case kFormat8H:
259      return 8;
260    case kFormatS:
261    case kFormat2S:
262    case kFormat4S:
263      return 4;
264    case kFormatD:
265    case kFormat1D:
266    case kFormat2D:
267      return 2;
268    default:
269      UNREACHABLE();
270  }
271}
272
273// Does 'vform' indicate a vector format or a scalar format?
274bool IsVectorFormat(VectorFormat vform) {
275  DCHECK_NE(vform, kFormatUndefined);
276  switch (vform) {
277    case kFormatB:
278    case kFormatH:
279    case kFormatS:
280    case kFormatD:
281      return false;
282    default:
283      return true;
284  }
285}
286
287int64_t MaxIntFromFormat(VectorFormat vform) {
288  return INT64_MAX >> (64 - LaneSizeInBitsFromFormat(vform));
289}
290
291int64_t MinIntFromFormat(VectorFormat vform) {
292  return INT64_MIN >> (64 - LaneSizeInBitsFromFormat(vform));
293}
294
295uint64_t MaxUintFromFormat(VectorFormat vform) {
296  return UINT64_MAX >> (64 - LaneSizeInBitsFromFormat(vform));
297}
298
299}  // namespace internal
300}  // namespace v8
301
302#endif  // V8_TARGET_ARCH_ARM64
303