Skip to content

Commit 9bb806f

Browse files
committed
use vector in compare
1 parent 2751064 commit 9bb806f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

ext/bcmath/libbcmath/src/compare.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us
9292
const char *n1ptr = n1->n_value;
9393
const char *n2ptr = n2->n_value;
9494

95+
while (count >= sizeof(BC_VECTOR)) {
96+
BC_VECTOR n1bytes;
97+
BC_VECTOR n2bytes;
98+
memcpy(&n1bytes, n1ptr, sizeof(BC_VECTOR));
99+
memcpy(&n2bytes, n2ptr, sizeof(BC_VECTOR));
100+
101+
if (n1bytes != n2bytes) {
102+
#if BC_LITTLE_ENDIAN
103+
n1bytes = BC_BSWAP(n1bytes);
104+
n2bytes = BC_BSWAP(n2bytes);
105+
#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+
}
121+
}
122+
count -= sizeof(BC_VECTOR);
123+
n1ptr += sizeof(BC_VECTOR);
124+
n2ptr += sizeof(BC_VECTOR);
125+
}
126+
95127
while ((count > 0) && (*n1ptr == *n2ptr)) {
96128
n1ptr++;
97129
n2ptr++;

0 commit comments

Comments
 (0)