1// Copyright 2017 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#include "src/builtins/builtins-utils-gen.h"
6#include "src/builtins/builtins.h"
7#include "src/code-stub-assembler.h"
8
9namespace v8 {
10namespace internal {
11
12// https://tc39.github.io/proposal-bigint/#sec-to-big-int64
13TF_BUILTIN(BigIntToI64, CodeStubAssembler) {
14 if (!Is64()) {
15 Unreachable();
16 return;
17 }
18
19 TNode<Object> value = CAST(Parameter(Descriptor::kArgument));
20 TNode<Context> context = CAST(Parameter(Descriptor::kContext));
21 TNode<BigInt> bigint = ToBigInt(context, value);
22
23 TVARIABLE(UintPtrT, var_low);
24 TVARIABLE(UintPtrT, var_high);
25
26 // 2. Let int64bit be n modulo 2^64.
27 // 3. If int64bit ≥ 2^63, return int64bit - 2^64;
28 BigIntToRawBytes(bigint, &var_low, &var_high);
29 ReturnRaw(var_low.value());
30}
31
32// https://tc39.github.io/proposal-bigint/#sec-bigint-constructor-number-value
33TF_BUILTIN(I64ToBigInt, CodeStubAssembler) {
34 if (!Is64()) {
35 Unreachable();
36 return;
37 }
38
39 TNode<IntPtrT> argument =
40 UncheckedCast<IntPtrT>(Parameter(Descriptor::kArgument));
41
42 Return(BigIntFromInt64(argument));
43}
44
45} // namespace internal
46} // namespace v8
47