Skip to content

Commit bc31662

Browse files
committed
The duplicated code was refactored into an inline function.
1 parent 9bb806f commit bc31662

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

ext/bcmath/libbcmath/src/compare.c

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@
3939
than N2 and +1 if N1 is greater than N2. If USE_SIGN is false, just
4040
compare the magnitudes. */
4141

42+
static inline bcmath_compare_result bc_compare_get_result_val(bool left_abs_greater, bool use_sign, sign left_sign)
43+
{
44+
if (left_abs_greater) {
45+
/* Magnitude of left > right. */
46+
if (!use_sign || left_sign == PLUS) {
47+
return BCMATH_LEFT_GREATER;
48+
} else {
49+
return BCMATH_RIGHT_GREATER;
50+
}
51+
} else {
52+
/* Magnitude of left < right. */
53+
if (!use_sign || left_sign == PLUS) {
54+
return BCMATH_RIGHT_GREATER;
55+
} else {
56+
return BCMATH_LEFT_GREATER;
57+
}
58+
}
59+
}
60+
4261
bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool use_sign)
4362
{
4463
/* First, compare signs. */
@@ -66,21 +85,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us
6685

6786
/* Now compare the magnitude. */
6887
if (n1->n_len != n2->n_len) {
69-
if (n1->n_len > n2->n_len) {
70-
/* Magnitude of n1 > n2. */
71-
if (!use_sign || n1->n_sign == PLUS) {
72-
return BCMATH_LEFT_GREATER;
73-
} else {
74-
return BCMATH_RIGHT_GREATER;
75-
}
76-
} else {
77-
/* Magnitude of n1 < n2. */
78-
if (!use_sign || n1->n_sign == PLUS) {
79-
return BCMATH_RIGHT_GREATER;
80-
} else {
81-
return BCMATH_LEFT_GREATER;
82-
}
83-
}
88+
return bc_compare_get_result_val(n1->n_len > n2->n_len, use_sign, n1->n_sign);
8489
}
8590

8691
size_t n1_scale = MIN(n1->n_scale, scale);
@@ -103,21 +108,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us
103108
n1bytes = BC_BSWAP(n1bytes);
104109
n2bytes = BC_BSWAP(n2bytes);
105110
#endif
106-
if (n1bytes > n2bytes) {
107-
/* Magnitude of n1 > n2. */
108-
if (!use_sign || n1->n_sign == PLUS) {
109-
return BCMATH_LEFT_GREATER;
110-
} else {
111-
return BCMATH_RIGHT_GREATER;
112-
}
113-
} else {
114-
/* Magnitude of n1 < n2. */
115-
if (!use_sign || n1->n_sign == PLUS) {
116-
return BCMATH_RIGHT_GREATER;
117-
} else {
118-
return BCMATH_LEFT_GREATER;
119-
}
120-
}
111+
return bc_compare_get_result_val(n1bytes > n2bytes, use_sign, n1->n_sign);
121112
}
122113
count -= sizeof(BC_VECTOR);
123114
n1ptr += sizeof(BC_VECTOR);
@@ -131,21 +122,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us
131122
}
132123

133124
if (count != 0) {
134-
if (*n1ptr > *n2ptr) {
135-
/* Magnitude of n1 > n2. */
136-
if (!use_sign || n1->n_sign == PLUS) {
137-
return BCMATH_LEFT_GREATER;
138-
} else {
139-
return BCMATH_RIGHT_GREATER;
140-
}
141-
} else {
142-
/* Magnitude of n1 < n2. */
143-
if (!use_sign || n1->n_sign == PLUS) {
144-
return BCMATH_RIGHT_GREATER;
145-
} else {
146-
return BCMATH_LEFT_GREATER;
147-
}
148-
}
125+
return bc_compare_get_result_val(*n1ptr > *n2ptr, use_sign, n1->n_sign);
149126
}
150127

151128
/* They are equal up to the last part of the equal part of the fraction. */

0 commit comments

Comments
 (0)