diff --git a/libraries/llvm/bytearray.c b/libraries/llvm/bytearray.c index 0597f147e..a8a0d6719 100644 --- a/libraries/llvm/bytearray.c +++ b/libraries/llvm/bytearray.c @@ -152,39 +152,34 @@ struct Pos c_bytearray_equal(const struct Pos left, const struct Pos right) { erasePositive(right); return BooleanFalse; } - for (uint64_t j = 0; j < left_size; ++j) { - if (c_bytearray_data(left)[j] != c_bytearray_data(right)[j]) { - erasePositive(left); - erasePositive(right); - return BooleanFalse; - } - } + + int cmp = memcmp(c_bytearray_data(left), c_bytearray_data(right), left_size); erasePositive(left); erasePositive(right); - return BooleanTrue; + return (cmp == 0 ? BooleanTrue : BooleanFalse); } Int c_bytearray_compare(const struct Pos left, const struct Pos right) { - if (left.obj == right.obj) return 0; - uint64_t left_size = left.tag; uint64_t right_size = right.tag; - for (uint64_t j = 0; j < left_size && j < right_size; ++j) { - uint8_t left_data = c_bytearray_data(left)[j]; - uint8_t right_data = c_bytearray_data(right)[j]; - if (left_data < right_data) { - erasePositive(left); - erasePositive(right); - return -1; - } else if (left_data > right_data) { - erasePositive(left); - erasePositive(right); - return 1; - } - } + uint64_t min_size = (left_size < right_size) ? left_size : right_size; + uint8_t* left_data = c_bytearray_data(left); + uint8_t* right_data = c_bytearray_data(right); + + if (left_data == right_data) return 0; + + int cmp = memcmp(left_data, right_data, min_size); + erasePositive(left); erasePositive(right); + + if (cmp < 0) { + return -1; + } else if(cmp > 0) { + return 1; + } + if (left_size < right_size) { return -1; } else if (left_size > right_size) {