Skip to content

Commit 7f7df8f

Browse files
committed
Reduced the number of times it is judged whether to compare as absolute values.
1 parent 0ce6de5 commit 7f7df8f

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

ext/bcmath/libbcmath/src/compare.c

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,32 @@
4242
int _bc_do_compare(bc_num n1, bc_num n2, bool use_sign, bool ignore_last)
4343
{
4444
char *n1ptr, *n2ptr;
45+
int result_n1_is_large = -1;
4546

4647
/* First, compare signs. */
47-
if (use_sign && n1->n_sign != n2->n_sign) {
48+
if (use_sign) {
4849
if (n1->n_sign == PLUS) {
49-
/* Positive N1 > Negative N2 */
50-
return (1);
50+
if (n1->n_sign != n2->n_sign) {
51+
return 1;
52+
}
53+
result_n1_is_large = 1;
5154
} else {
52-
/* Negative N1 < Positive N1 */
53-
return (-1);
55+
if (n1->n_sign != n2->n_sign) {
56+
return -1;
57+
}
5458
}
59+
} else {
60+
result_n1_is_large = 1;
5561
}
5662

5763
/* Now compare the magnitude. */
5864
if (n1->n_len != n2->n_len) {
5965
if (n1->n_len > n2->n_len) {
6066
/* Magnitude of n1 > n2. */
61-
if (!use_sign || n1->n_sign == PLUS) {
62-
return (1);
63-
} else {
64-
return (-1);
65-
}
67+
return result_n1_is_large;
6668
} else {
6769
/* Magnitude of n1 < n2. */
68-
if (!use_sign || n1->n_sign == PLUS) {
69-
return (-1);
70-
} else {
71-
return (1);
72-
}
70+
return -result_n1_is_large;
7371
}
7472
}
7573

@@ -91,18 +89,10 @@ int _bc_do_compare(bc_num n1, bc_num n2, bool use_sign, bool ignore_last)
9189
if (count != 0) {
9290
if (*n1ptr > *n2ptr) {
9391
/* Magnitude of n1 > n2. */
94-
if (!use_sign || n1->n_sign == PLUS) {
95-
return (1);
96-
} else {
97-
return (-1);
98-
}
92+
return result_n1_is_large;
9993
} else {
10094
/* Magnitude of n1 < n2. */
101-
if (!use_sign || n1->n_sign == PLUS) {
102-
return (-1);
103-
} else {
104-
return (1);
105-
}
95+
return -result_n1_is_large;
10696
}
10797
}
10898

@@ -112,22 +102,14 @@ int _bc_do_compare(bc_num n1, bc_num n2, bool use_sign, bool ignore_last)
112102
for (count = n1->n_scale - n2->n_scale; count > 0; count--) {
113103
if (*n1ptr++ != 0) {
114104
/* Magnitude of n1 > n2. */
115-
if (!use_sign || n1->n_sign == PLUS) {
116-
return (1);
117-
} else {
118-
return (-1);
119-
}
105+
return result_n1_is_large;
120106
}
121107
}
122108
} else {
123109
for (count = n2->n_scale - n1->n_scale; count > 0; count--) {
124110
if (*n2ptr++ != 0) {
125111
/* Magnitude of n1 < n2. */
126-
if (!use_sign || n1->n_sign == PLUS) {
127-
return (-1);
128-
} else {
129-
return (1);
130-
}
112+
return -result_n1_is_large;
131113
}
132114
}
133115
}

0 commit comments

Comments
 (0)