39
39
than N2 and +1 if N1 is greater than N2. If USE_SIGN is false, just
40
40
compare the magnitudes. */
41
41
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
+
42
61
bcmath_compare_result _bc_do_compare (bc_num n1 , bc_num n2 , size_t scale , bool use_sign )
43
62
{
44
63
/* First, compare signs. */
@@ -66,21 +85,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us
66
85
67
86
/* Now compare the magnitude. */
68
87
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 );
84
89
}
85
90
86
91
size_t n1_scale = MIN (n1 -> n_scale , scale );
@@ -92,28 +97,32 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us
92
97
const char * n1ptr = n1 -> n_value ;
93
98
const char * n2ptr = n2 -> n_value ;
94
99
100
+ while (count >= sizeof (BC_VECTOR )) {
101
+ BC_VECTOR n1bytes ;
102
+ BC_VECTOR n2bytes ;
103
+ memcpy (& n1bytes , n1ptr , sizeof (BC_VECTOR ));
104
+ memcpy (& n2bytes , n2ptr , sizeof (BC_VECTOR ));
105
+
106
+ if (n1bytes != n2bytes ) {
107
+ #if BC_LITTLE_ENDIAN
108
+ n1bytes = BC_BSWAP (n1bytes );
109
+ n2bytes = BC_BSWAP (n2bytes );
110
+ #endif
111
+ return bc_compare_get_result_val (n1bytes > n2bytes , use_sign , n1 -> n_sign );
112
+ }
113
+ count -= sizeof (BC_VECTOR );
114
+ n1ptr += sizeof (BC_VECTOR );
115
+ n2ptr += sizeof (BC_VECTOR );
116
+ }
117
+
95
118
while ((count > 0 ) && (* n1ptr == * n2ptr )) {
96
119
n1ptr ++ ;
97
120
n2ptr ++ ;
98
121
count -- ;
99
122
}
100
123
101
124
if (count != 0 ) {
102
- if (* n1ptr > * n2ptr ) {
103
- /* Magnitude of n1 > n2. */
104
- if (!use_sign || n1 -> n_sign == PLUS ) {
105
- return BCMATH_LEFT_GREATER ;
106
- } else {
107
- return BCMATH_RIGHT_GREATER ;
108
- }
109
- } else {
110
- /* Magnitude of n1 < n2. */
111
- if (!use_sign || n1 -> n_sign == PLUS ) {
112
- return BCMATH_RIGHT_GREATER ;
113
- } else {
114
- return BCMATH_LEFT_GREATER ;
115
- }
116
- }
125
+ return bc_compare_get_result_val (* n1ptr > * n2ptr , use_sign , n1 -> n_sign );
117
126
}
118
127
119
128
/* They are equal up to the last part of the equal part of the fraction. */
0 commit comments