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
This commit is contained in:
Zsolt Borbély
2016-02-22 08:50:11 +01:00
parent e14d0b8942
commit 3f377692d9
14 changed files with 88 additions and 167 deletions
+5
View File
@@ -579,6 +579,11 @@ typedef double ecma_number_t;
*/
#define ECMA_NUMBER_HALF ((ecma_number_t) 0.5f)
/**
* Value '-1' of ecma_number_t
*/
#define ECMA_NUMBER_MINUS_ONE ((ecma_number_t) -1)
/**
* Minimum positive and maximum value of ecma-number
*/
+8 -30
View File
@@ -1,4 +1,4 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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 */
/* Hex literal handling */
begin_p += 2;
ecma_number_t num = 0;
ecma_number_t num = ECMA_NUMBER_ZERO;
for (const lit_utf8_byte_t * iter_p = begin_p;
iter_p <= end_p;
@@ -796,7 +796,7 @@ ecma_uint32_to_utf8_string (uint32_t value, /**< value to convert */
*p-- = digits[value % 10];
value /= 10;
bytes_copied ++;
bytes_copied++;
}
while (value != 0);
@@ -821,9 +821,7 @@ ecma_uint32_to_utf8_string (uint32_t value, /**< value to convert */
ecma_number_t
ecma_uint32_to_number (uint32_t value) /**< unsigned 32-bit integer value */
{
ecma_number_t num_value = (ecma_number_t) value;
return num_value;
return (ecma_number_t) value;
} /* ecma_uint32_to_number */
/**
@@ -834,9 +832,7 @@ ecma_uint32_to_number (uint32_t value) /**< unsigned 32-bit integer value */
ecma_number_t
ecma_int32_to_number (int32_t value) /**< signed 32-bit integer value */
{
ecma_number_t num_value = (ecma_number_t) value;
return num_value;
return (ecma_number_t) value;
} /* ecma_int32_to_number */
/**
@@ -857,17 +853,8 @@ ecma_number_to_uint32 (ecma_number_t num) /**< ecma-number */
return 0;
}
bool sign = ecma_number_is_negative (num);
ecma_number_t abs_num;
if (sign)
{
abs_num = ecma_number_negate (num);
}
else
{
abs_num = num;
}
const bool sign = ecma_number_is_negative (num);
const ecma_number_t abs_num = sign ? ecma_number_negate (num) : num;
// 2 ^ 32
const uint64_t uint64_2_pow_32 = (1ull << 32);
@@ -890,16 +877,7 @@ ecma_number_to_uint32 (ecma_number_t num) /**< ecma-number */
JERRY_ASSERT (num_in_uint32_range < uint64_2_pow_32);
uint32_t uint32_num = (uint32_t) num_in_uint32_range;
uint32_t ret;
if (sign)
{
ret = -uint32_num;
}
else
{
ret = uint32_num;
}
const uint32_t ret = sign ? -uint32_num : uint32_num;
#ifndef JERRY_NDEBUG
if (sign
+25 -49
View File
@@ -1,5 +1,5 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
* Copyright 2015 University of Szeged.
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,9 @@
#include "ecma-globals.h"
#include "ecma-helpers.h"
#define ECMA_NUMBER_SIGN_POS (ECMA_NUMBER_FRACTION_WIDTH + \
ECMA_NUMBER_BIASED_EXP_WIDTH)
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT32
JERRY_STATIC_ASSERT (sizeof (ecma_number_t) == sizeof (uint32_t),
size_of_ecma_number_t_must_be_equal_to_4_bytes);
@@ -38,16 +41,12 @@ ecma_number_pack (bool sign, /**< sign */
uint32_t biased_exp, /**< biased exponent */
uint64_t fraction) /**< fraction */
{
const uint32_t fraction_pos = 0;
const uint32_t biased_exp_pos = fraction_pos + ECMA_NUMBER_FRACTION_WIDTH;
const uint32_t sign_pos = biased_exp_pos + ECMA_NUMBER_BIASED_EXP_WIDTH;
JERRY_ASSERT ((biased_exp & ~((1u << ECMA_NUMBER_BIASED_EXP_WIDTH) - 1)) == 0);
JERRY_ASSERT ((fraction & ~((1ull << ECMA_NUMBER_FRACTION_WIDTH) - 1)) == 0);
uint32_t packed_value = (((sign ? 1u : 0u) << sign_pos) |
(biased_exp << biased_exp_pos) |
(((uint32_t) fraction) << fraction_pos));
uint32_t packed_value = (((sign ? 1u : 0u) << ECMA_NUMBER_SIGN_POS) |
(biased_exp << ECMA_NUMBER_FRACTION_WIDTH) |
((uint32_t) fraction));
union
{
@@ -69,10 +68,6 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
uint32_t *biased_exp_p, /**< [out] biased exponent (optional) */
uint64_t *fraction_p) /**< [out] fraction (optional) */
{
const uint32_t fraction_pos = 0;
const uint32_t biased_exp_pos = fraction_pos + ECMA_NUMBER_FRACTION_WIDTH;
const uint32_t sign_pos = biased_exp_pos + ECMA_NUMBER_BIASED_EXP_WIDTH;
union
{
uint32_t u32_value;
@@ -85,12 +80,12 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
if (sign_p != NULL)
{
*sign_p = ((packed_value >> sign_pos) != 0);
*sign_p = ((packed_value >> ECMA_NUMBER_SIGN_POS) != 0);
}
if (biased_exp_p != NULL)
{
*biased_exp_p = (((packed_value) & ~(1u << sign_pos)) >> biased_exp_pos);
*biased_exp_p = (((packed_value) & ~(1u << ECMA_NUMBER_SIGN_POS)) >> ECMA_NUMBER_FRACTION_WIDTH);
}
if (fraction_p != NULL)
@@ -107,10 +102,6 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
*/
const int32_t ecma_number_exponent_bias = 127;
/**
* Relative precision used in calculation with ecma-numbers
*/
const ecma_number_t ecma_number_relative_eps = 1.0e-10f;
#elif CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
JERRY_STATIC_ASSERT (sizeof (ecma_number_t) == sizeof (uint64_t),
size_of_ecma_number_t_must_be_equal_to_8_bytes);
@@ -125,13 +116,9 @@ ecma_number_pack (bool sign, /**< sign */
uint32_t biased_exp, /**< biased exponent */
uint64_t fraction) /**< fraction */
{
const uint32_t fraction_pos = 0;
const uint32_t biased_exp_pos = fraction_pos + ECMA_NUMBER_FRACTION_WIDTH;
const uint32_t sign_pos = biased_exp_pos + ECMA_NUMBER_BIASED_EXP_WIDTH;
uint64_t packed_value = (((sign ? 1ull : 0ull) << sign_pos) |
(((uint64_t) biased_exp) << biased_exp_pos) |
(fraction << fraction_pos));
uint64_t packed_value = (((sign ? 1ull : 0ull) << ECMA_NUMBER_SIGN_POS) |
(((uint64_t) biased_exp) << ECMA_NUMBER_FRACTION_WIDTH) |
fraction);
JERRY_ASSERT ((biased_exp & ~((1u << ECMA_NUMBER_BIASED_EXP_WIDTH) - 1)) == 0);
JERRY_ASSERT ((fraction & ~((1ull << ECMA_NUMBER_FRACTION_WIDTH) - 1)) == 0);
@@ -156,10 +143,6 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
uint32_t *biased_exp_p, /**< [out] biased exponent (optional) */
uint64_t *fraction_p) /**< [out] fraction (optional) */
{
const uint32_t fraction_pos = 0;
const uint32_t biased_exp_pos = fraction_pos + ECMA_NUMBER_FRACTION_WIDTH;
const uint32_t sign_pos = biased_exp_pos + ECMA_NUMBER_BIASED_EXP_WIDTH;
union
{
uint64_t u64_value;
@@ -171,12 +154,12 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
if (sign_p != NULL)
{
*sign_p = ((packed_value >> sign_pos) != 0);
*sign_p = ((packed_value >> ECMA_NUMBER_SIGN_POS) != 0);
}
if (biased_exp_p != NULL)
{
*biased_exp_p = (uint32_t) (((packed_value) & ~(1ull << sign_pos)) >> biased_exp_pos);
*biased_exp_p = (uint32_t) (((packed_value) & ~(1ull << ECMA_NUMBER_SIGN_POS)) >> ECMA_NUMBER_FRACTION_WIDTH);
}
if (fraction_p != NULL)
@@ -193,10 +176,6 @@ ecma_number_unpack (ecma_number_t num, /**< ecma-number */
*/
const int32_t ecma_number_exponent_bias = 1023;
/**
* Relative precision used in calculation with ecma-numbers
*/
const ecma_number_t ecma_number_relative_eps = 1.0e-16;
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
/**
@@ -638,7 +617,7 @@ ecma_number_trunc (ecma_number_t num) /**< ecma-number */
if (exponent < 0)
{
return 0;
return ECMA_NUMBER_ZERO;
}
else if (exponent < dot_shift)
{
@@ -674,21 +653,18 @@ ecma_number_t
ecma_number_calc_remainder (ecma_number_t left_num, /**< left operand */
ecma_number_t right_num) /**< right operand */
{
ecma_number_t n = left_num, d = right_num;
JERRY_ASSERT (!ecma_number_is_nan (left_num)
&& !ecma_number_is_zero (left_num)
&& !ecma_number_is_infinity (left_num));
JERRY_ASSERT (!ecma_number_is_nan (right_num)
&& !ecma_number_is_zero (right_num)
&& !ecma_number_is_infinity (right_num));
JERRY_ASSERT (!ecma_number_is_nan (n)
&& !ecma_number_is_zero (n)
&& !ecma_number_is_infinity (n));
JERRY_ASSERT (!ecma_number_is_nan (d)
&& !ecma_number_is_zero (d)
&& !ecma_number_is_infinity (d));
ecma_number_t q = ecma_number_trunc (ecma_number_divide (n, d));
ecma_number_t r = ecma_number_substract (n, ecma_number_multiply (d, q));
const ecma_number_t q = ecma_number_trunc (ecma_number_divide (left_num, right_num));
ecma_number_t r = ecma_number_substract (left_num, ecma_number_multiply (right_num, q));
if (ecma_number_is_zero (r)
&& ecma_number_is_negative (n))
&& ecma_number_is_negative (left_num))
{
r = ecma_number_negate (r);
}
+12 -43
View File
@@ -339,17 +339,16 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */
const size_t data_size = new_size + sizeof (ecma_string_heap_header_t);
ecma_string_heap_header_t *data_p = (ecma_string_heap_header_t *) mem_heap_alloc_block (data_size);
ssize_t bytes_copied1, bytes_copied2;
bytes_copied1 = ecma_string_to_utf8_string (string1_p,
(lit_utf8_byte_t *) (data_p + 1),
(ssize_t) str1_size);
JERRY_ASSERT (bytes_copied1 > 0);
ssize_t bytes_copied = ecma_string_to_utf8_string (string1_p,
(lit_utf8_byte_t *) (data_p + 1),
(ssize_t) str1_size);
JERRY_ASSERT (bytes_copied > 0);
bytes_copied2 = ecma_string_to_utf8_string (string2_p,
(lit_utf8_byte_t *) (data_p + 1) + str1_size,
(ssize_t) str2_size);
JERRY_ASSERT (bytes_copied2 > 0);
bytes_copied = ecma_string_to_utf8_string (string2_p,
(lit_utf8_byte_t *) (data_p + 1) + str1_size,
(ssize_t) str2_size);
JERRY_ASSERT (bytes_copied > 0);
data_p->size = (uint16_t) new_size;
data_p->length = (uint16_t) (ecma_string_get_length (string1_p) + ecma_string_get_length (string2_p));
@@ -469,8 +468,8 @@ ecma_copy_or_ref_ecma_string (ecma_string_t *string_desc_p) /**< string descript
} /* ecma_copy_or_ref_ecma_string */
/**
* Decrease reference counter and deallocate ecma-string if
* after that the counter the counter becomes zero.
* Decrease reference counter and deallocate ecma-string
* if the counter becomes zero.
*/
void
ecma_deref_ecma_string (ecma_string_t *string_p) /**< ecma-string */
@@ -518,32 +517,6 @@ ecma_deref_ecma_string (ecma_string_t *string_p) /**< ecma-string */
ecma_dealloc_string (string_p);
} /* ecma_deref_ecma_string */
/**
* Assertion that specified ecma-string need not be freed
*/
void
ecma_check_that_ecma_string_need_not_be_freed (const ecma_string_t *string_p) /**< ecma-string descriptor */
{
#ifdef JERRY_NDEBUG
(void) string_p;
#else /* JERRY_NDEBUG */
/*
* No reference counter increment or decrement
* should be performed with ecma-string placed
* on stack
*/
JERRY_ASSERT (string_p->refs == 1);
ecma_string_container_t container_type = (ecma_string_container_t) string_p->container;
JERRY_ASSERT (container_type == ECMA_STRING_CONTAINER_LIT_TABLE ||
container_type == ECMA_STRING_CONTAINER_MAGIC_STRING ||
container_type == ECMA_STRING_CONTAINER_MAGIC_STRING_EX ||
container_type == ECMA_STRING_CONTAINER_UINT32_IN_DESC);
#endif /* !JERRY_NDEBUG */
} /* ecma_check_that_ecma_string_need_not_be_freed */
/**
* Convert ecma-string to number
*/
@@ -926,16 +899,12 @@ ecma_compare_ecma_strings (const ecma_string_t *string1_p, /* ecma-string */
{
JERRY_ASSERT (string1_p != NULL && string2_p != NULL);
const bool is_equal_hashes = (string1_p->hash == string2_p->hash);
if (!is_equal_hashes)
if (string1_p->hash != string2_p->hash)
{
return false;
}
const bool is_equal_containers = (string1_p->container == string2_p->container);
const bool is_equal_fields = (string1_p->u.common_field == string2_p->u.common_field);
if (is_equal_containers && is_equal_fields)
if (ecma_compare_ecma_strings_equal_hashes (string1_p, string2_p))
{
return true;
}
-3
View File
@@ -95,7 +95,6 @@ extern ecma_string_t *ecma_new_ecma_string_from_magic_string_ex_id (lit_magic_st
extern ecma_string_t *ecma_concat_ecma_strings (ecma_string_t *, ecma_string_t *);
extern ecma_string_t *ecma_copy_or_ref_ecma_string (ecma_string_t *);
extern void ecma_deref_ecma_string (ecma_string_t *);
extern void ecma_check_that_ecma_string_need_not_be_freed (const ecma_string_t *);
extern ecma_number_t ecma_string_to_number (const ecma_string_t *);
extern bool ecma_string_get_array_index (const ecma_string_t *, uint32_t *);
@@ -120,8 +119,6 @@ extern ecma_string_t *ecma_string_substr (const ecma_string_t *, ecma_length_t,
extern ecma_string_t *ecma_string_trim (const ecma_string_t *);
/* ecma-helpers-number.c */
extern const ecma_number_t ecma_number_relative_eps;
extern ecma_number_t ecma_number_make_nan (void);
extern ecma_number_t ecma_number_make_infinity (bool);
extern bool ecma_number_is_nan (ecma_number_t);