Skip to content

Commit

Permalink
Properly byte-swap fixed packed fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
haberman committed Nov 5, 2021
1 parent dee1238 commit 5097825
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion benchmarks/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False):
Run("cp -f bazel-bin/tests/conformance_upb {}.bin".format(outbase))


baseline = "master"
baseline = "main"
bench_cpu = True
fasttable = False

Expand Down
27 changes: 25 additions & 2 deletions upb/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,31 @@ static const char *decode_fixed_packed(upb_decstate *d, const char *ptr,
arr->len += count;
// Note: if/when the decoder supports multi-buffer input, we will need to
// handle buffer seams here.
memcpy(mem, ptr, val->size);
return ptr + val->size;
if (_upb_isle()) {
memcpy(mem, ptr, val->size);
ptr += val->size;
} else {
const char *end = ptr + val->size;
char *dst = mem;
while (ptr < end) {
if (lg2 == 2) {
uint32_t val;
memcpy(&val, ptr, sizeof(val));
val = _upb_be_swap32(val);
memcpy(dst, &val, sizeof(val));
} else {
UPB_ASSERT(lg2 == 3);
uint64_t val;
memcpy(&val, ptr, sizeof(val));
val = _upb_be_swap64(val);
memcpy(dst, &val, sizeof(val));
}
ptr += 1 << lg2;
dst += 1 << lg2;
}
}

return ptr;
}

UPB_FORCEINLINE
Expand Down

0 comments on commit 5097825

Please sign in to comment.