Skip to content

Commit

Permalink
Use memcmp for bytearray eq and cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
mattisboeckle committed Jan 14, 2025
1 parent 8ad3e95 commit 469ae81
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions libraries/llvm/bytearray.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 469ae81

Please sign in to comment.