Skip to content

Commit 23c771c

Browse files
committed
Implement BigInt asIntN and asUintN methods
The following methods were implemented: - BigInt.asIntN - BigInt.asUintN Custom dispatcher also added to builtin_bigint. JerryScript-DCO-1.0-Signed-off-by: Daniel Batiz [email protected]
1 parent 499cdea commit 23c771c

File tree

7 files changed

+587
-26
lines changed

7 files changed

+587
-26
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-bigint.c

Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,31 @@
1414
*/
1515

1616
#include "ecma-bigint.h"
17+
#include "ecma-big-uint.h"
1718
#include "ecma-builtins.h"
1819
#include "ecma-exceptions.h"
20+
#include <math.h>
1921

2022
#if JERRY_BUILTIN_BIGINT
2123

2224
#define ECMA_BUILTINS_INTERNAL
2325
#include "ecma-builtins-internal.h"
2426

27+
/**
28+
* This object has a custom dispatch function.
29+
*/
30+
#define BUILTIN_CUSTOM_DISPATCH
31+
32+
/**
33+
* List of built-in routine identifiers.
34+
*/
35+
enum
36+
{
37+
ECMA_BUILTIN_BIGINT_START = 0,
38+
ECMA_BUILTIN_BIGINT_AS_INT_N,
39+
ECMA_BUILTIN_BIGINT_AS_U_INT_N,
40+
};
41+
2542
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-bigint.inc.h"
2643
#define BUILTIN_UNDERSCORED_ID bigint
2744
#include "ecma-builtin-internal-routines-template.inc.h"
@@ -36,6 +53,311 @@
3653
* @{
3754
*/
3855

56+
/**
57+
* The BigInt object's 'asIntN' and 'asUintN' routines
58+
*
59+
* See also:
60+
* ECMA-262 v5, 11.0
61+
*
62+
* @return ecma value
63+
* Returned value must be freed with ecma_free_value.
64+
*/
65+
static ecma_value_t
66+
ecma_builtin_bigint_object_as_int_n (ecma_value_t bits, /**< number of bits */
67+
ecma_value_t bigint, /**< bigint number */
68+
bool is_signed) /**< The operation is signed */
69+
{
70+
ecma_number_t input_bits;
71+
ecma_value_t bit_value = ecma_op_to_index (bits, &input_bits);
72+
73+
if (ECMA_IS_VALUE_ERROR (bit_value))
74+
{
75+
return bit_value;
76+
}
77+
78+
ecma_value_t bigint_value = ecma_bigint_to_bigint (bigint, false);
79+
80+
if (ECMA_IS_VALUE_ERROR (bigint_value))
81+
{
82+
return bigint_value;
83+
}
84+
85+
if (input_bits == 0 || bigint_value == ECMA_BIGINT_ZERO)
86+
{
87+
ecma_free_value (bigint_value);
88+
return ECMA_BIGINT_ZERO;
89+
}
90+
91+
const uint32_t size_of_divisor_in_bits = sizeof (uint32_t) * JERRY_BITSINBYTE;
92+
93+
uint32_t whole_part = (uint32_t) floor (input_bits / size_of_divisor_in_bits);
94+
uint32_t remainder = (uint32_t) fmod (input_bits, size_of_divisor_in_bits);
95+
96+
uint32_t input_bit_length = 1;
97+
98+
if (whole_part == 0)
99+
{
100+
input_bit_length = (uint32_t) input_bits;
101+
}
102+
else
103+
{
104+
input_bit_length = (remainder > 0) ? (whole_part + 1) * size_of_divisor_in_bits : (uint32_t) input_bits;
105+
}
106+
107+
uint32_t input_byte_size = (uint32_t) floor (input_bits / JERRY_BITSINBYTE);
108+
109+
if (fmod (input_bits, JERRY_BITSINBYTE) != 0)
110+
{
111+
input_byte_size += 1;
112+
}
113+
114+
ecma_extended_primitive_t *input_bigint_p = ecma_get_extended_primitive_from_value (bigint_value);
115+
116+
uint32_t bigint_size = ECMA_BIGINT_GET_SIZE (input_bigint_p);
117+
uint8_t input_bigint_sign = input_bigint_p->u.bigint_sign_and_size & ECMA_BIGINT_SIGN;
118+
119+
const uint32_t input_bits_in_byte = (uint32_t) input_bit_length / JERRY_BITSINBYTE;
120+
121+
uint32_t min_size = (input_bits_in_byte < bigint_size) ? input_bits_in_byte : bigint_size;
122+
123+
if (input_bigint_sign && (input_byte_size > bigint_size))
124+
{
125+
min_size = (input_bits_in_byte > bigint_size) ? input_bits_in_byte : bigint_size;
126+
}
127+
128+
if (min_size < sizeof (uint32_t))
129+
{
130+
min_size = sizeof (uint32_t);
131+
}
132+
133+
ecma_extended_primitive_t * result_p = ecma_bigint_create ((uint32_t) min_size);
134+
135+
if (JERRY_UNLIKELY (result_p == NULL))
136+
{
137+
ecma_deref_bigint (input_bigint_p);
138+
return ECMA_VALUE_ERROR;
139+
}
140+
141+
ecma_bigint_digit_t *last_digit_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, bigint_size);
142+
143+
/* Calculate the leading zeros of the input_bigint */
144+
145+
ecma_bigint_digit_t zeros = ecma_big_uint_count_leading_zero (last_digit_p[-1]);
146+
147+
uint32_t bits_of_bigint = (uint32_t) (bigint_size * JERRY_BITSINBYTE) - zeros;
148+
149+
uint32_t exact_size = 1;
150+
151+
if ((bits_of_bigint > (size_of_divisor_in_bits - 1)) || input_bigint_sign)
152+
{
153+
exact_size = (input_byte_size < bigint_size) ? (uint32_t) input_byte_size : bigint_size;
154+
155+
if (input_bigint_sign && (input_byte_size > bigint_size))
156+
{
157+
exact_size = (input_byte_size > bigint_size) ? (uint32_t) input_byte_size : bigint_size;
158+
}
159+
}
160+
else
161+
{
162+
exact_size = bigint_size;
163+
}
164+
165+
if (input_bigint_sign)
166+
{
167+
bits_of_bigint += 1;
168+
}
169+
170+
if (bits_of_bigint > (input_bits - 1) || input_bigint_sign)
171+
{
172+
ecma_bigint_digit_t *digits_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, 0);
173+
ecma_bigint_digit_t *digits_end_p = ECMA_BIGINT_GET_DIGITS (input_bigint_p, exact_size);
174+
ecma_bigint_digit_t *result_number_p = ECMA_BIGINT_GET_DIGITS (result_p, 0);
175+
int32_t first_cell = 0;
176+
uint32_t surplus_bits = (whole_part > 0) ? (uint32_t) (whole_part * size_of_divisor_in_bits)
177+
: (uint32_t) size_of_divisor_in_bits;
178+
uint32_t mask_bit = (whole_part == 0) ? (uint32_t) input_bits : (uint32_t) input_bits - surplus_bits;
179+
180+
if (mask_bit == 0)
181+
{
182+
mask_bit = size_of_divisor_in_bits - 1;
183+
}
184+
185+
uint32_t check_sign_mask = (uint32_t) 1 << (mask_bit - 1);
186+
uint32_t mask = ((uint32_t) 1 << mask_bit) - 1;
187+
uint32_t last_cell = (exact_size >= sizeof (uint32_t)) ? (uint32_t) (min_size / sizeof (uint32_t)) - 1 : 0;
188+
bool is_positive = false;
189+
bool is_representation_positive = false;
190+
191+
if (is_signed)
192+
{
193+
if (input_bigint_sign && ((~digits_p[last_cell] + 1) & check_sign_mask) == 0)
194+
{
195+
is_positive = true;
196+
}
197+
198+
if ((digits_p[last_cell] & check_sign_mask) == 0)
199+
{
200+
is_representation_positive = true;
201+
}
202+
}
203+
204+
do
205+
{
206+
*result_number_p++ = (is_representation_positive
207+
|| (!is_signed && !input_bigint_sign)) ? *digits_p++: ~(*digits_p++);
208+
first_cell--;
209+
}
210+
while (digits_p < digits_end_p);
211+
212+
int16_t equal_bit_s = 0;
213+
214+
if (remainder != 0)
215+
{
216+
equal_bit_s = -1;
217+
}
218+
219+
int32_t last_cell_negative = (last_cell != 0) ? ((int32_t) last_cell * (-1)) : -1;
220+
bool is_zero_values = false;
221+
222+
if (!is_signed)
223+
{
224+
if (input_bigint_sign)
225+
{
226+
is_zero_values = true;
227+
}
228+
}
229+
else
230+
{
231+
if (((digits_p[-1] & check_sign_mask) > 0) || (result_number_p[-1] & check_sign_mask) > 0)
232+
{
233+
is_zero_values = true;
234+
}
235+
}
236+
if (is_zero_values)
237+
{
238+
result_number_p[first_cell] += 1;
239+
240+
if (result_number_p[first_cell] == 0)
241+
{
242+
do
243+
{
244+
result_number_p[++first_cell] += 1;
245+
}
246+
while (first_cell != equal_bit_s);
247+
248+
first_cell = last_cell_negative;
249+
}
250+
}
251+
252+
result_number_p[-1] &= mask;
253+
uint32_t surplus = (uint32_t) (min_size - exact_size) / sizeof (ecma_char_t);
254+
uint32_t new_size = result_p->u.bigint_sign_and_size;
255+
256+
if ((min_size - exact_size) % (sizeof (ecma_char_t)) > 0 && surplus == 0)
257+
{
258+
surplus += (uint32_t) sizeof (ecma_char_t);
259+
}
260+
else
261+
{
262+
surplus = (uint32_t) (surplus * sizeof (ecma_char_t));
263+
}
264+
265+
if (min_size / JERRY_BITSINBYTE < 1)
266+
{
267+
surplus = 0;
268+
}
269+
270+
if (is_signed)
271+
{
272+
if (result_p->u.bigint_sign_and_size > exact_size
273+
&& min_size > sizeof (uint32_t)
274+
&& result_number_p[last_cell_negative] < 1)
275+
{
276+
new_size -= surplus;
277+
}
278+
279+
new_size += 1;
280+
281+
if (is_positive || ((digits_p[-1] & check_sign_mask) == 0 && !input_bigint_sign))
282+
{
283+
new_size -= 1;
284+
}
285+
}
286+
287+
while (first_cell != 0)
288+
{
289+
if (result_number_p[first_cell] != 0)
290+
{
291+
break;
292+
}
293+
294+
first_cell++;
295+
}
296+
297+
if (first_cell == 0)
298+
{
299+
ecma_deref_bigint (result_p);
300+
ecma_deref_bigint (input_bigint_p);
301+
302+
return ECMA_BIGINT_ZERO;
303+
}
304+
305+
last_cell_negative = first_cell + (int32_t) last_cell;
306+
int16_t zero_section_cnt = 0;
307+
308+
while (last_cell_negative > first_cell)
309+
{
310+
if (result_number_p[last_cell_negative] == 0)
311+
{
312+
zero_section_cnt++;
313+
}
314+
315+
last_cell_negative--;
316+
}
317+
318+
uint32_t size_limit = sizeof (uint32_t);
319+
320+
if (zero_section_cnt >= 1)
321+
{
322+
size_limit = new_size - (uint32_t) zero_section_cnt * size_limit;
323+
new_size = (size_limit < sizeof (uint32_t)) ? (uint32_t) (JERRY_BITSINBYTE - size_limit) : size_limit;
324+
}
325+
326+
if (new_size < result_p->u.bigint_sign_and_size)
327+
{
328+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
329+
uint32_t new_size_remainder = new_size % sizeof (uint32_t);
330+
ecma_extended_primitive_t * new_result_p = ecma_bigint_create (new_size - new_size_remainder);
331+
332+
new_result_p->u.bigint_sign_and_size += new_size_remainder;
333+
memcpy (new_result_p + 1, result_p + 1, new_size - new_size_remainder);
334+
335+
ecma_deref_bigint (result_p);
336+
ecma_deref_bigint (input_bigint_p);
337+
338+
return ecma_make_extended_primitive_value (new_result_p, ECMA_TYPE_BIGINT);
339+
}
340+
341+
result_p->u.bigint_sign_and_size = new_size;
342+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
343+
344+
ecma_deref_bigint (input_bigint_p);
345+
return ecma_make_extended_primitive_value (result_p, ECMA_TYPE_BIGINT);
346+
}
347+
348+
memcpy (result_p + 1, input_bigint_p + 1, exact_size);
349+
result_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | ECMA_TYPE_BIGINT;
350+
351+
if (input_bigint_p->u.bigint_sign_and_size & ECMA_BIGINT_SIGN)
352+
{
353+
ecma_deref_bigint (input_bigint_p);
354+
return ecma_bigint_negate (result_p);
355+
}
356+
357+
ecma_deref_bigint (input_bigint_p);
358+
return ecma_make_extended_primitive_value (result_p, ECMA_TYPE_BIGINT);
359+
} /* ecma_builtin_bigint_object_as_int_n */
360+
39361
/**
40362
* Handle calling [[Call]] of built-in BigInt object
41363
*
@@ -71,6 +393,42 @@ ecma_builtin_bigint_dispatch_construct (const ecma_value_t *arguments_list_p, /*
71393
return ecma_raise_type_error (ECMA_ERR_MSG ("BigInt function is not a constructor"));
72394
} /* ecma_builtin_bigint_dispatch_construct */
73395

396+
/**
397+
* Dispatcher of the built-in's routines
398+
*
399+
* @return ecma value
400+
* Returned value must be freed with ecma_free_value.
401+
*/
402+
ecma_value_t
403+
ecma_builtin_bigint_dispatch_routine (uint8_t builtin_routine_id, /**< built-in wide routine identifier */
404+
ecma_value_t this_arg, /**< 'this' argument value */
405+
const ecma_value_t arguments_list_p[], /**< list of arguments
406+
* passed to routine */
407+
uint32_t arguments_number) /**< length of arguments' list */
408+
{
409+
JERRY_UNUSED_2 (this_arg, arguments_number);
410+
411+
switch (builtin_routine_id)
412+
{
413+
case ECMA_BUILTIN_BIGINT_AS_INT_N:
414+
{
415+
return ecma_builtin_bigint_object_as_int_n (arguments_list_p[0],
416+
arguments_list_p[1],
417+
true);
418+
}
419+
case ECMA_BUILTIN_BIGINT_AS_U_INT_N:
420+
{
421+
return ecma_builtin_bigint_object_as_int_n (arguments_list_p[0],
422+
arguments_list_p[1],
423+
false);
424+
}
425+
default:
426+
{
427+
JERRY_UNREACHABLE ();
428+
}
429+
}
430+
} /* ecma_builtin_bigint_dispatch_routine */
431+
74432
/**
75433
* @}
76434
* @}

jerry-core/ecma/builtin-objects/ecma-builtin-bigint.inc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
4242
ECMA_BUILTIN_ID_BIGINT_PROTOTYPE,
4343
ECMA_PROPERTY_FIXED)
4444

45+
/* Routine properties:
46+
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
47+
48+
ROUTINE (LIT_MAGIC_STRING_AS_INT_N, ECMA_BUILTIN_BIGINT_AS_INT_N, 2, 2)
49+
ROUTINE (LIT_MAGIC_STRING_AS_U_INT_N, ECMA_BUILTIN_BIGINT_AS_U_INT_N, 2, 2)
4550
#endif /* JERRY_BUILTIN_BIGINT */
4651

4752
#include "ecma-builtin-helpers-macro-undefs.inc.h"

0 commit comments

Comments
 (0)