summaryrefslogtreecommitdiff
path: root/deps/v8/src/builtins/typed-array.tq
blob: 59100736a5dc7bc9f5e5f83154d160fab45e9fe6 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include 'src/builtins/builtins-typed-array-gen.h'

namespace typed_array {
  // Naming convention from elements.cc. We have a similar intent but implement
  // fastpaths using generics instead of using a class hierarchy for elements
  // kinds specific implementations.
  type Uint8Elements;
  type Int8Elements;
  type Uint16Elements;
  type Int16Elements;
  type Uint32Elements;
  type Int32Elements;
  type Float32Elements;
  type Float64Elements;
  type Uint8ClampedElements;
  type BigUint64Elements;
  type BigInt64Elements;

  struct TypedArrayElementsInfo {
    // Calculates the number of bytes required for specified number of elements.
    CalculateByteLength(lengthSmi: PositiveSmi): uintptr labels IfInvalid {
      const length = Convert<uintptr>(lengthSmi);
      const byteLength = length << this.sizeLog2;
      // If an overflow ocurred, the byte length exceeds
      // JSArrayBuffer::kMaxByteLength and is invalid.
      if (byteLength >>> this.sizeLog2 != length) goto IfInvalid;
      return byteLength;
    }

    // Calculates the maximum number of elements supported by a specified number
    // of bytes.
    CalculateLength(byteLength: uintptr): PositiveSmi labels IfInvalid {
      return Convert<PositiveSmi>(byteLength >>> this.sizeLog2)
          otherwise IfInvalid;
    }

    // Determines if `bytes` (byte offset or length) cannot be evenly divided by
    // element size.
    IsUnaligned(bytes: uintptr): bool {
      // Exploits the fact the element size is a power of 2. Determining whether
      // there is remainder (not aligned) can be achieved efficiently with bit
      // masking. Shift is safe as sizeLog2 can be 3 at most (see
      // ElementsKindToShiftSize).
      return (bytes & ((1 << this.sizeLog2) - 1)) != 0;
    }

    sizeLog2: uintptr;
    kind: ElementsKind;
  }
  extern runtime TypedArraySortFast(Context, JSAny): JSTypedArray;
  extern macro TypedArrayBuiltinsAssembler::ValidateTypedArray(
      Context, JSAny, constexpr string): JSTypedArray;

  extern macro TypedArrayBuiltinsAssembler::CallCMemcpy(
      RawPtr, RawPtr, uintptr): void;
  extern macro TypedArrayBuiltinsAssembler::CallCMemmove(
      RawPtr, RawPtr, uintptr): void;
  extern macro TypedArrayBuiltinsAssembler::CallCMemset(
      RawPtr, intptr, uintptr): void;
  extern macro TypedArrayBuiltinsAssembler::GetBuffer(
      implicit context: Context)(JSTypedArray): JSArrayBuffer;
  extern macro TypedArrayBuiltinsAssembler::GetTypedArrayElementsInfo(
      JSTypedArray): TypedArrayElementsInfo;
  extern macro TypedArrayBuiltinsAssembler::GetTypedArrayElementsInfo(Map):
      TypedArrayElementsInfo;
  extern macro TypedArrayBuiltinsAssembler::IsBigInt64ElementsKind(
      ElementsKind): bool;
  extern macro LoadFixedTypedArrayElementAsTagged(
      RawPtr, Smi, constexpr ElementsKind): Numeric;
  extern macro StoreJSTypedArrayElementFromTagged(
      Context, JSTypedArray, Smi, JSAny, constexpr ElementsKind);

  type LoadFn = builtin(Context, JSTypedArray, Smi) => JSAny;
  type StoreFn = builtin(Context, JSTypedArray, Smi, JSAny) => JSAny;

  // AttachedJSTypedArray guards that the array's buffer is not detached.
  transient type AttachedJSTypedArray extends JSTypedArray;

  macro EnsureAttached(array: JSTypedArray): AttachedJSTypedArray
      labels Detached {
    if (IsDetachedBuffer(array.buffer)) goto Detached;
    return %RawDownCast<AttachedJSTypedArray>(array);
  }

  struct AttachedJSTypedArrayWitness {
    Get(): AttachedJSTypedArray {
      return this.unstable;
    }

    GetStable(): JSTypedArray {
      return this.stable;
    }

    Recheck() labels Detached {
      if (IsDetachedBuffer(this.stable.buffer)) goto Detached;
      this.unstable = %RawDownCast<AttachedJSTypedArray>(this.stable);
    }

    Load(implicit context: Context)(k: Smi): JSAny {
      const lf: LoadFn = this.loadfn;
      return lf(context, this.unstable, k);
    }

    stable: JSTypedArray;
    unstable: AttachedJSTypedArray;
    loadfn: LoadFn;
  }

  macro NewAttachedJSTypedArrayWitness(array: AttachedJSTypedArray):
      AttachedJSTypedArrayWitness {
    const kind = array.elements_kind;
    return AttachedJSTypedArrayWitness{
      stable: array,
      unstable: array,
      loadfn: GetLoadFnForElementsKind(kind)
    };
  }

  macro GetLoadFnForElementsKind(elementsKind: ElementsKind): LoadFn {
    if (IsElementsKindGreaterThan(elementsKind, UINT32_ELEMENTS)) {
      if (elementsKind == INT32_ELEMENTS) {
        return LoadFixedElement<Int32Elements>;
      } else if (elementsKind == FLOAT32_ELEMENTS) {
        return LoadFixedElement<Float32Elements>;
      } else if (elementsKind == FLOAT64_ELEMENTS) {
        return LoadFixedElement<Float64Elements>;
      } else if (elementsKind == UINT8_CLAMPED_ELEMENTS) {
        return LoadFixedElement<Uint8ClampedElements>;
      } else if (elementsKind == BIGUINT64_ELEMENTS) {
        return LoadFixedElement<BigUint64Elements>;
      } else if (elementsKind == BIGINT64_ELEMENTS) {
        return LoadFixedElement<BigInt64Elements>;
      } else {
        unreachable;
      }
    } else {
      if (elementsKind == UINT8_ELEMENTS) {
        return LoadFixedElement<Uint8Elements>;
      } else if (elementsKind == INT8_ELEMENTS) {
        return LoadFixedElement<Int8Elements>;
      } else if (elementsKind == UINT16_ELEMENTS) {
        return LoadFixedElement<Uint16Elements>;
      } else if (elementsKind == INT16_ELEMENTS) {
        return LoadFixedElement<Int16Elements>;
      } else if (elementsKind == UINT32_ELEMENTS) {
        return LoadFixedElement<Uint32Elements>;
      } else {
        unreachable;
      }
    }
  }

  macro KindForArrayType<T: type>(): constexpr ElementsKind;
  KindForArrayType<Uint8Elements>(): constexpr ElementsKind {
    return UINT8_ELEMENTS;
  }
  KindForArrayType<Int8Elements>(): constexpr ElementsKind {
    return INT8_ELEMENTS;
  }
  KindForArrayType<Uint16Elements>(): constexpr ElementsKind {
    return UINT16_ELEMENTS;
  }
  KindForArrayType<Int16Elements>(): constexpr ElementsKind {
    return INT16_ELEMENTS;
  }
  KindForArrayType<Uint32Elements>(): constexpr ElementsKind {
    return UINT32_ELEMENTS;
  }
  KindForArrayType<Int32Elements>(): constexpr ElementsKind {
    return INT32_ELEMENTS;
  }
  KindForArrayType<Float32Elements>(): constexpr ElementsKind {
    return FLOAT32_ELEMENTS;
  }
  KindForArrayType<Float64Elements>(): constexpr ElementsKind {
    return FLOAT64_ELEMENTS;
  }
  KindForArrayType<Uint8ClampedElements>(): constexpr ElementsKind {
    return UINT8_CLAMPED_ELEMENTS;
  }
  KindForArrayType<BigUint64Elements>(): constexpr ElementsKind {
    return BIGUINT64_ELEMENTS;
  }
  KindForArrayType<BigInt64Elements>(): constexpr ElementsKind {
    return BIGINT64_ELEMENTS;
  }

  builtin LoadFixedElement<T: type>(
      _context: Context, array: JSTypedArray, index: Smi): JSAny {
    return LoadFixedTypedArrayElementAsTagged(
        array.data_ptr, index, KindForArrayType<T>());
  }

  builtin StoreFixedElement<T: type>(
      context: Context, typedArray: JSTypedArray, index: Smi,
      value: JSAny): JSAny {
    StoreJSTypedArrayElementFromTagged(
        context, typedArray, index, value, KindForArrayType<T>());
    return Undefined;
  }

  transitioning macro CallCompare(
      implicit context: Context, array: JSTypedArray,
      comparefn: Callable)(a: JSAny, b: JSAny): Number {
    // a. Let v be ? ToNumber(? Call(comparefn, undefined, x, y)).
    const v: Number =
        ToNumber_Inline(context, Call(context, comparefn, Undefined, a, b));

    // b. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
    if (IsDetachedBuffer(array.buffer)) {
      ThrowTypeError(kDetachedOperation, '%TypedArray%.prototype.sort');
    }

    // c. If v is NaN, return +0.
    if (NumberIsNaN(v)) return 0;

    // d. return v.
    return v;
  }

  // Merges two sorted runs [from, middle) and [middle, to)
  // from "source" into "target".
  transitioning macro
  TypedArrayMerge(
      implicit context: Context, array: JSTypedArray, comparefn: Callable)(
      source: FixedArray, from: Smi, middle: Smi, to: Smi, target: FixedArray) {
    let left: Smi = from;
    let right: Smi = middle;

    for (let targetIndex: Smi = from; targetIndex < to; ++targetIndex) {
      if (left < middle && right >= to) {
        // If the left run has elements, but the right does not, we take
        // from the left.
        target.objects[targetIndex] = source.objects[left++];
      } else if (left < middle) {
        // If both have elements, we need to compare.
        const leftElement = UnsafeCast<JSAny>(source.objects[left]);
        const rightElement = UnsafeCast<JSAny>(source.objects[right]);
        if (CallCompare(leftElement, rightElement) <= 0) {
          target.objects[targetIndex] = leftElement;
          left++;
        } else {
          target.objects[targetIndex] = rightElement;
          right++;
        }
      } else {
        // No elements on the left, but the right does, so we take
        // from the right.
        assert(left == middle);
        target.objects[targetIndex] = source.objects[right++];
      }
    }
  }

  transitioning builtin
  TypedArrayMergeSort(
      implicit context: Context, array: JSTypedArray, comparefn: Callable)(
      source: FixedArray, from: Smi, to: Smi, target: FixedArray): JSAny {
    assert(to - from > 1);
    const middle: Smi = from + ((to - from) >> 1);

    // On the next recursion step source becomes target and vice versa.
    // This saves the copy of the relevant range from the original
    // array into a work array on each recursion step.
    if (middle - from > 1) TypedArrayMergeSort(target, from, middle, source);
    if (to - middle > 1) TypedArrayMergeSort(target, middle, to, source);

    TypedArrayMerge(source, from, middle, to, target);

    return Undefined;
  }

  // https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.sort
  transitioning javascript builtin TypedArrayPrototypeSort(
      js-implicit context: Context,
      receiver: JSAny)(...arguments): JSTypedArray {
    // 1. If comparefn is not undefined and IsCallable(comparefn) is false,
    //    throw a TypeError exception.
    const comparefnObj: JSAny = arguments.length > 0 ? arguments[0] : Undefined;
    if (comparefnObj != Undefined && !TaggedIsCallable(comparefnObj)) {
      ThrowTypeError(kBadSortComparisonFunction, comparefnObj);
    }

    // 2. Let obj be the this value.
    const obj: JSAny = receiver;

    // 3. Let buffer be ? ValidateTypedArray(obj).
    //    ValidateTypedArray currently returns the array, not the ViewBuffer.
    const array: JSTypedArray =
        ValidateTypedArray(context, obj, '%TypedArray%.prototype.sort');

    // Default sorting is done in C++ using std::sort
    if (comparefnObj == Undefined) {
      return TypedArraySortFast(context, obj);
    }

    // 4. Let len be obj.[[ArrayLength]].
    // TODO(v8:4153): Support huge TypedArrays here.
    const len = Cast<Smi>(Convert<Number>(array.length)) otherwise unreachable;

    // Arrays of length 1 or less are considered sorted.
    if (len < 2) return array;

    const comparefn: Callable =
        Cast<Callable>(comparefnObj) otherwise unreachable;
    let loadfn: LoadFn;
    let storefn: StoreFn;

    const elementsKind: ElementsKind = array.elements_kind;

    if (IsElementsKindGreaterThan(elementsKind, UINT32_ELEMENTS)) {
      if (elementsKind == INT32_ELEMENTS) {
        loadfn = LoadFixedElement<Int32Elements>;
        storefn = StoreFixedElement<Int32Elements>;
      } else if (elementsKind == FLOAT32_ELEMENTS) {
        loadfn = LoadFixedElement<Float32Elements>;
        storefn = StoreFixedElement<Float32Elements>;
      } else if (elementsKind == FLOAT64_ELEMENTS) {
        loadfn = LoadFixedElement<Float64Elements>;
        storefn = StoreFixedElement<Float64Elements>;
      } else if (elementsKind == UINT8_CLAMPED_ELEMENTS) {
        loadfn = LoadFixedElement<Uint8ClampedElements>;
        storefn = StoreFixedElement<Uint8ClampedElements>;
      } else if (elementsKind == BIGUINT64_ELEMENTS) {
        loadfn = LoadFixedElement<BigUint64Elements>;
        storefn = StoreFixedElement<BigUint64Elements>;
      } else if (elementsKind == BIGINT64_ELEMENTS) {
        loadfn = LoadFixedElement<BigInt64Elements>;
        storefn = StoreFixedElement<BigInt64Elements>;
      } else {
        unreachable;
      }
    } else {
      if (elementsKind == UINT8_ELEMENTS) {
        loadfn = LoadFixedElement<Uint8Elements>;
        storefn = StoreFixedElement<Uint8Elements>;
      } else if (elementsKind == INT8_ELEMENTS) {
        loadfn = LoadFixedElement<Int8Elements>;
        storefn = StoreFixedElement<Int8Elements>;
      } else if (elementsKind == UINT16_ELEMENTS) {
        loadfn = LoadFixedElement<Uint16Elements>;
        storefn = StoreFixedElement<Uint16Elements>;
      } else if (elementsKind == INT16_ELEMENTS) {
        loadfn = LoadFixedElement<Int16Elements>;
        storefn = StoreFixedElement<Int16Elements>;
      } else if (elementsKind == UINT32_ELEMENTS) {
        loadfn = LoadFixedElement<Uint32Elements>;
        storefn = StoreFixedElement<Uint32Elements>;
      } else {
        unreachable;
      }
    }

    // Prepare the two work arrays. All numbers are converted to tagged
    // objects first, and merge sorted between the two FixedArrays.
    // The result is then written back into the JSTypedArray.
    const work1: FixedArray = AllocateZeroedFixedArray(Convert<intptr>(len));
    const work2: FixedArray = AllocateZeroedFixedArray(Convert<intptr>(len));

    for (let i: Smi = 0; i < len; ++i) {
      const element: JSAny = loadfn(context, array, i);
      work1.objects[i] = element;
      work2.objects[i] = element;
    }

    TypedArrayMergeSort(work2, 0, len, work1);

    // work1 contains the sorted numbers. Write them back.
    for (let i: Smi = 0; i < len; ++i)
      storefn(context, array, i, UnsafeCast<JSAny>(work1.objects[i]));

    return array;
  }
}