Skip to content

Commit cc8708d

Browse files
author
Zsolt Borbély
committed
Small refactorings
Modifications: * eliminate unnecessary variables, functions * use ECMA_NUMBER macros where it is possible * simplify code * minor style fix (comments, increase-decrease operators) JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
1 parent af715d4 commit cc8708d

14 files changed

+85
-167
lines changed

jerry-core/ecma/base/ecma-globals.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@ typedef double ecma_number_t;
580580
*/
581581
#define ECMA_NUMBER_HALF ((ecma_number_t) 0.5f)
582582

583+
/**
584+
* Value '-1' of ecma_number_t
585+
*/
586+
#define ECMA_NUMBER_MINUS_ONE ((ecma_number_t) -1)
587+
583588
/**
584589
* Minimum positive and maximum value of ecma-number
585590
*/

jerry-core/ecma/base/ecma-helpers-conversion.c

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -404,7 +404,7 @@ ecma_utf8_string_to_number (const lit_utf8_byte_t *str_p, /**< utf-8 string */
404404
/* Hex literal handling */
405405
begin_p += 2;
406406

407-
ecma_number_t num = 0;
407+
ecma_number_t num = ECMA_NUMBER_ZERO;
408408

409409
for (const lit_utf8_byte_t * iter_p = begin_p;
410410
iter_p <= end_p;
@@ -796,7 +796,7 @@ ecma_uint32_to_utf8_string (uint32_t value, /**< value to convert */
796796
*p-- = digits[value % 10];
797797
value /= 10;
798798

799-
bytes_copied ++;
799+
bytes_copied++;
800800
}
801801
while (value != 0);
802802

@@ -821,9 +821,7 @@ ecma_uint32_to_utf8_string (uint32_t value, /**< value to convert */
821821
ecma_number_t
822822
ecma_uint32_to_number (uint32_t value) /**< unsigned 32-bit integer value */
823823
{
824-
ecma_number_t num_value = (ecma_number_t) value;
825-
826-
return num_value;
824+
return (ecma_number_t) value;
827825
} /* ecma_uint32_to_number */
828826

829827
/**
@@ -834,9 +832,7 @@ ecma_uint32_to_number (uint32_t value) /**< unsigned 32-bit integer value */
834832
ecma_number_t
835833
ecma_int32_to_number (int32_t value) /**< signed 32-bit integer value */
836834
{
837-
ecma_number_t num_value = (ecma_number_t) value;
838-
839-
return num_value;
835+
return (ecma_number_t) value;
840836
} /* ecma_int32_to_number */
841837

842838
/**
@@ -857,17 +853,8 @@ ecma_number_to_uint32 (ecma_number_t num) /**< ecma-number */
857853
return 0;
858854
}
859855

860-
bool sign = ecma_number_is_negative (num);
861-
ecma_number_t abs_num;
862-
863-
if (sign)
864-
{
865-
abs_num = ecma_number_negate (num);
866-
}
867-
else
868-
{
869-
abs_num = num;
870-
}
856+
const bool sign = ecma_number_is_negative (num);
857+
const ecma_number_t abs_num = sign ? ecma_number_negate (num) : num;
871858

872859
// 2 ^ 32
873860
const uint64_t uint64_2_pow_32 = (1ull << 32);
@@ -890,16 +877,7 @@ ecma_number_to_uint32 (ecma_number_t num) /**< ecma-number */
890877
JERRY_ASSERT (num_in_uint32_range < uint64_2_pow_32);
891878
uint32_t uint32_num = (uint32_t) num_in_uint32_range;
892879

893-
uint32_t ret;
894-
895-
if (sign)
896-
{
897-
ret = -uint32_num;
898-
}
899-
else
900-
{
901-
ret = uint32_num;
902-
}
880+
const uint32_t ret = sign ? -uint32_num : uint32_num;
903881

904882
#ifndef JERRY_NDEBUG
905883
if (sign

jerry-core/ecma/base/ecma-helpers-number.c

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* Copyright 2014 Samsung Electronics Co., Ltd.
2-
* Copyright 2015 University of Szeged.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2015-2016 University of Szeged.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,9 @@
2424
#include "ecma-globals.h"
2525
#include "ecma-helpers.h"
2626

27+
#define ECMA_NUMBER_SIGN_POS (ECMA_NUMBER_FRACTION_WIDTH + \
28+
ECMA_NUMBER_BIASED_EXP_WIDTH)
29+
2730
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT32
2831
JERRY_STATIC_ASSERT (sizeof (ecma_number_t) == sizeof (uint32_t),
2932
size_of_ecma_number_t_must_be_equal_to_4_bytes);
@@ -38,16 +41,12 @@ ecma_number_pack (bool sign, /**< sign */
3841
uint32_t biased_exp, /**< biased exponent */
3942
uint64_t fraction) /**< fraction */
4043
{
41-
const uint32_t fraction_pos = 0;
42-
const uint32_t biased_exp_pos = fraction_pos + ECMA_NUMBER_FRACTION_WIDTH;
43-
const uint32_t sign_pos = biased_exp_pos + ECMA_NUMBER_BIASED_EXP_WIDTH;
44-
4544
JERRY_ASSERT ((biased_exp & ~((1u << ECMA_NUMBER_BIASED_EXP_WIDTH) - 1)) == 0);
4645
JERRY_ASSERT ((fraction & ~((1ull << ECMA_NUMBER_FRACTION_WIDTH) - 1)) == 0);
4746

48-
uint32_t packed_value = (((sign ? 1u : 0u) << sign_pos) |
49-
(biased_exp << biased_exp_pos) |
50-
(((uint32_t) fraction) << fraction_pos));
47+
uint32_t packed_value = (((sign ? 1u : 0u) << ECMA_NUMBER_SIGN_POS) |
48+
(biased_exp << ECMA_NUMBER_FRACTION_WIDTH) |
49+
((uint32_t) fraction));
5150

5251
union
5352
{
@@ -69,10 +68,6 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
6968
uint32_t *biased_exp_p, /**< optional out: biased exponent */
7069
uint64_t *fraction_p) /**< optional out: fraction */
7170
{
72-
const uint32_t fraction_pos = 0;
73-
const uint32_t biased_exp_pos = fraction_pos + ECMA_NUMBER_FRACTION_WIDTH;
74-
const uint32_t sign_pos = biased_exp_pos + ECMA_NUMBER_BIASED_EXP_WIDTH;
75-
7671
union
7772
{
7873
uint32_t u32_value;
@@ -85,12 +80,12 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
8580

8681
if (sign_p != NULL)
8782
{
88-
*sign_p = ((packed_value >> sign_pos) != 0);
83+
*sign_p = ((packed_value >> ECMA_NUMBER_SIGN_POS) != 0);
8984
}
9085

9186
if (biased_exp_p != NULL)
9287
{
93-
*biased_exp_p = (((packed_value) & ~(1u << sign_pos)) >> biased_exp_pos);
88+
*biased_exp_p = (((packed_value) & ~(1u << ECMA_NUMBER_SIGN_POS)) >> ECMA_NUMBER_FRACTION_WIDTH);
9489
}
9590

9691
if (fraction_p != NULL)
@@ -107,10 +102,6 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
107102
*/
108103
const int32_t ecma_number_exponent_bias = 127;
109104

110-
/**
111-
* Relative precision used in calculation with ecma-numbers
112-
*/
113-
const ecma_number_t ecma_number_relative_eps = 1.0e-10f;
114105
#elif CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
115106
JERRY_STATIC_ASSERT (sizeof (ecma_number_t) == sizeof (uint64_t),
116107
size_of_ecma_number_t_must_be_equal_to_8_bytes);
@@ -125,13 +116,9 @@ ecma_number_pack (bool sign, /**< sign */
125116
uint32_t biased_exp, /**< biased exponent */
126117
uint64_t fraction) /**< fraction */
127118
{
128-
const uint32_t fraction_pos = 0;
129-
const uint32_t biased_exp_pos = fraction_pos + ECMA_NUMBER_FRACTION_WIDTH;
130-
const uint32_t sign_pos = biased_exp_pos + ECMA_NUMBER_BIASED_EXP_WIDTH;
131-
132-
uint64_t packed_value = (((sign ? 1ull : 0ull) << sign_pos) |
133-
(((uint64_t) biased_exp) << biased_exp_pos) |
134-
(fraction << fraction_pos));
119+
uint64_t packed_value = (((sign ? 1ull : 0ull) << ECMA_NUMBER_SIGN_POS) |
120+
(((uint64_t) biased_exp) << ECMA_NUMBER_FRACTION_WIDTH) |
121+
fraction);
135122

136123
JERRY_ASSERT ((biased_exp & ~((1u << ECMA_NUMBER_BIASED_EXP_WIDTH) - 1)) == 0);
137124
JERRY_ASSERT ((fraction & ~((1ull << ECMA_NUMBER_FRACTION_WIDTH) - 1)) == 0);
@@ -156,10 +143,6 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
156143
uint32_t *biased_exp_p, /**< optional out: biased exponent */
157144
uint64_t *fraction_p) /**< optional out: fraction */
158145
{
159-
const uint32_t fraction_pos = 0;
160-
const uint32_t biased_exp_pos = fraction_pos + ECMA_NUMBER_FRACTION_WIDTH;
161-
const uint32_t sign_pos = biased_exp_pos + ECMA_NUMBER_BIASED_EXP_WIDTH;
162-
163146
union
164147
{
165148
uint64_t u64_value;
@@ -171,12 +154,12 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
171154

172155
if (sign_p != NULL)
173156
{
174-
*sign_p = ((packed_value >> sign_pos) != 0);
157+
*sign_p = ((packed_value >> ECMA_NUMBER_SIGN_POS) != 0);
175158
}
176159

177160
if (biased_exp_p != NULL)
178161
{
179-
*biased_exp_p = (uint32_t) (((packed_value) & ~(1ull << sign_pos)) >> biased_exp_pos);
162+
*biased_exp_p = (uint32_t) (((packed_value) & ~(1ull << ECMA_NUMBER_SIGN_POS)) >> ECMA_NUMBER_FRACTION_WIDTH);
180163
}
181164

182165
if (fraction_p != NULL)
@@ -193,10 +176,6 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
193176
*/
194177
const int32_t ecma_number_exponent_bias = 1023;
195178

196-
/**
197-
* Relative precision used in calculation with ecma-numbers
198-
*/
199-
const ecma_number_t ecma_number_relative_eps = 1.0e-16;
200179
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
201180

202181
/**
@@ -638,7 +617,7 @@ ecma_number_trunc (ecma_number_t num) /**< ecma-number */
638617

639618
if (exponent < 0)
640619
{
641-
return 0;
620+
return ECMA_NUMBER_ZERO;
642621
}
643622
else if (exponent < dot_shift)
644623
{
@@ -674,21 +653,18 @@ ecma_number_t
674653
ecma_number_calc_remainder (ecma_number_t left_num, /**< left operand */
675654
ecma_number_t right_num) /**< right operand */
676655
{
677-
ecma_number_t n = left_num, d = right_num;
678-
679-
JERRY_ASSERT (!ecma_number_is_nan (n)
680-
&& !ecma_number_is_zero (n)
681-
&& !ecma_number_is_infinity (n));
682-
JERRY_ASSERT (!ecma_number_is_nan (d)
683-
&& !ecma_number_is_zero (d)
684-
&& !ecma_number_is_infinity (d));
685-
686-
ecma_number_t q = ecma_number_trunc (ecma_number_divide (n, d));
656+
JERRY_ASSERT (!ecma_number_is_nan (left_num)
657+
&& !ecma_number_is_zero (left_num)
658+
&& !ecma_number_is_infinity (left_num));
659+
JERRY_ASSERT (!ecma_number_is_nan (right_num)
660+
&& !ecma_number_is_zero (right_num)
661+
&& !ecma_number_is_infinity (right_num));
687662

688-
ecma_number_t r = ecma_number_substract (n, ecma_number_multiply (d, q));
663+
const ecma_number_t q = ecma_number_trunc (ecma_number_divide (left_num, right_num));
664+
ecma_number_t r = ecma_number_substract (left_num, ecma_number_multiply (right_num, q));
689665

690666
if (ecma_number_is_zero (r)
691-
&& ecma_number_is_negative (n))
667+
&& ecma_number_is_negative (left_num))
692668
{
693669
r = ecma_number_negate (r);
694670
}

jerry-core/ecma/base/ecma-helpers-string.c

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,11 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
585585

586586
lit_utf8_byte_t *str_p = (lit_utf8_byte_t *) mem_heap_alloc_block (buffer_size, MEM_HEAP_ALLOC_SHORT_TERM);
587587

588-
ssize_t bytes_copied1, bytes_copied2;
588+
ssize_t bytes_copied = ecma_string_to_utf8_string (string1_p, str_p, (ssize_t) str1_size);
589+
JERRY_ASSERT (bytes_copied > 0);
589590

590-
bytes_copied1 = ecma_string_to_utf8_string (string1_p, str_p, (ssize_t) str1_size);
591-
JERRY_ASSERT (bytes_copied1 > 0);
592-
593-
bytes_copied2 = ecma_string_to_utf8_string (string2_p, str_p + str1_size, (ssize_t) str2_size);
594-
JERRY_ASSERT (bytes_copied2 > 0);
591+
bytes_copied = ecma_string_to_utf8_string (string2_p, str_p + str1_size, (ssize_t) str2_size);
592+
JERRY_ASSERT (bytes_copied > 0);
595593

596594
ecma_string_t *str_concat_p = ecma_new_ecma_string_from_utf8 (str_p, buffer_size);
597595

@@ -705,8 +703,8 @@ ecma_copy_or_ref_ecma_string (ecma_string_t *string_desc_p) /**< string descript
705703
} /* ecma_copy_or_ref_ecma_string */
706704

707705
/**
708-
* Decrease reference counter and deallocate ecma-string if
709-
* after that the counter the counter becomes zero.
706+
* Decrease reference counter and deallocate ecma-string
707+
* if the counter becomes zero.
710708
*/
711709
void
712710
ecma_deref_ecma_string (ecma_string_t *string_p) /**< ecma-string */
@@ -754,32 +752,6 @@ ecma_deref_ecma_string (ecma_string_t *string_p) /**< ecma-string */
754752
ecma_dealloc_string (string_p);
755753
} /* ecma_deref_ecma_string */
756754

757-
/**
758-
* Assertion that specified ecma-string need not be freed
759-
*/
760-
void
761-
ecma_check_that_ecma_string_need_not_be_freed (const ecma_string_t *string_p) /**< ecma-string descriptor */
762-
{
763-
#ifdef JERRY_NDEBUG
764-
(void) string_p;
765-
#else /* JERRY_NDEBUG */
766-
767-
/*
768-
* No reference counter increment or decrement
769-
* should be performed with ecma-string placed
770-
* on stack
771-
*/
772-
JERRY_ASSERT (string_p->refs == 1);
773-
774-
ecma_string_container_t container_type = (ecma_string_container_t) string_p->container;
775-
776-
JERRY_ASSERT (container_type == ECMA_STRING_CONTAINER_LIT_TABLE ||
777-
container_type == ECMA_STRING_CONTAINER_MAGIC_STRING ||
778-
container_type == ECMA_STRING_CONTAINER_MAGIC_STRING_EX ||
779-
container_type == ECMA_STRING_CONTAINER_UINT32_IN_DESC);
780-
#endif /* !JERRY_NDEBUG */
781-
} /* ecma_check_that_ecma_string_need_not_be_freed */
782-
783755
/**
784756
* Convert ecma-string to number
785757
*/
@@ -1062,9 +1034,7 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri
10621034
MEM_DEFINE_LOCAL_ARRAY (string1_buf, strings_size, lit_utf8_byte_t);
10631035
MEM_DEFINE_LOCAL_ARRAY (string2_buf, strings_size, lit_utf8_byte_t);
10641036

1065-
ssize_t req_size;
1066-
1067-
req_size = (ssize_t) ecma_string_to_utf8_string (string1_p, string1_buf, (ssize_t) strings_size);
1037+
ssize_t req_size = (ssize_t) ecma_string_to_utf8_string (string1_p, string1_buf, (ssize_t) strings_size);
10681038
JERRY_ASSERT (req_size > 0);
10691039
req_size = (ssize_t) ecma_string_to_utf8_string (string2_p, string2_buf, (ssize_t) strings_size);
10701040
JERRY_ASSERT (req_size > 0);
@@ -1112,16 +1082,12 @@ ecma_compare_ecma_strings (const ecma_string_t *string1_p, /* ecma-string */
11121082
{
11131083
JERRY_ASSERT (string1_p != NULL && string2_p != NULL);
11141084

1115-
const bool is_equal_hashes = (string1_p->hash == string2_p->hash);
1116-
1117-
if (!is_equal_hashes)
1085+
if (string1_p->hash != string2_p->hash)
11181086
{
11191087
return false;
11201088
}
1121-
const bool is_equal_containers = (string1_p->container == string2_p->container);
1122-
const bool is_equal_fields = (string1_p->u.common_field == string2_p->u.common_field);
11231089

1124-
if (is_equal_containers && is_equal_fields)
1090+
if (ecma_compare_ecma_strings_equal_hashes (string1_p, string2_p))
11251091
{
11261092
return true;
11271093
}

jerry-core/ecma/base/ecma-helpers.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ extern ecma_string_t *ecma_new_ecma_string_from_magic_string_ex_id (lit_magic_st
9494
extern ecma_string_t *ecma_concat_ecma_strings (ecma_string_t *, ecma_string_t *);
9595
extern ecma_string_t *ecma_copy_or_ref_ecma_string (ecma_string_t *);
9696
extern void ecma_deref_ecma_string (ecma_string_t *);
97-
extern void ecma_check_that_ecma_string_need_not_be_freed (const ecma_string_t *);
9897
extern ecma_number_t ecma_string_to_number (const ecma_string_t *);
9998
extern bool ecma_string_get_array_index (const ecma_string_t *, uint32_t *);
10099

@@ -119,8 +118,6 @@ extern ecma_string_t *ecma_string_substr (const ecma_string_t *, ecma_length_t,
119118
extern ecma_string_t *ecma_string_trim (const ecma_string_t *);
120119

121120
/* ecma-helpers-number.c */
122-
extern const ecma_number_t ecma_number_relative_eps;
123-
124121
extern ecma_number_t ecma_number_make_nan (void);
125122
extern ecma_number_t ecma_number_make_infinity (bool);
126123
extern bool ecma_number_is_nan (ecma_number_t);

0 commit comments

Comments
 (0)