Skip to content

Commit

Permalink
upb: add more functions to bits/mini_table_field.h
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 592847506
  • Loading branch information
ericsalo authored and copybara-github committed Dec 21, 2023
1 parent df7e74b commit cdb0782
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
18 changes: 11 additions & 7 deletions upb/message/internal/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,34 @@ extern "C" {

UPB_INLINE bool UPB_PRIVATE(_upb_Message_GetHasbit)(
const upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = _upb_MiniTableField_HasbitOffset(f);
const char mask = _upb_MiniTableField_HasbitMask(f);
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);

return (*UPB_PTR_AT(msg, offset, const char) & mask) != 0;
}

UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbit)(
const upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = _upb_MiniTableField_HasbitOffset(f);
const char mask = _upb_MiniTableField_HasbitMask(f);
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);

(*UPB_PTR_AT(msg, offset, char)) |= mask;
}

UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearHasbit)(
const upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = _upb_MiniTableField_HasbitOffset(f);
const char mask = _upb_MiniTableField_HasbitMask(f);
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);

(*UPB_PTR_AT(msg, offset, char)) &= ~mask;
}

// Oneof case access ///////////////////////////////////////////////////////////

UPB_INLINE uint32_t* UPB_PRIVATE(_upb_Message_OneofCasePtr)(
upb_Message* msg, const upb_MiniTableField* f) {
return UPB_PTR_AT(msg, _upb_MiniTableField_OneofOffset(f), uint32_t);
return UPB_PTR_AT(msg, UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(f),
uint32_t);
}

UPB_INLINE uint32_t UPB_PRIVATE(_upb_Message_GetOneofCase)(
Expand Down
2 changes: 1 addition & 1 deletion upb/mini_descriptor/internal/encode_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ TEST_P(MiniTableTest, AllScalarTypesOneof) {
EXPECT_EQ(table->UPB_PRIVATE(fields)[0].UPB_PRIVATE(offset),
f->UPB_PRIVATE(offset));
// All presence fields should point to the same oneof case offset.
size_t case_ofs = _upb_MiniTableField_OneofOffset(f);
size_t case_ofs = UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(f);
EXPECT_EQ(table->UPB_PRIVATE(fields)[0].presence, f->presence);
EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size));
EXPECT_TRUE(case_ofs < table->UPB_PRIVATE(size));
Expand Down
10 changes: 5 additions & 5 deletions upb/mini_table/internal/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ UPB_PRIVATE(_upb_MiniTableField_CType)(const upb_MiniTableField* f) {
return upb_FieldType_CType(UPB_PRIVATE(_upb_MiniTableField_Type)(f));
}

UPB_INLINE char _upb_MiniTableField_HasbitMask(
UPB_INLINE char UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(f->presence > 0);
const size_t index = f->presence;
return 1 << (index % 8);
}

UPB_INLINE size_t
_upb_MiniTableField_HasbitOffset(const struct upb_MiniTableField* f) {
UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(f->presence > 0);
const size_t index = f->presence;
return index / 8;
Expand Down Expand Up @@ -180,8 +180,8 @@ UPB_PRIVATE(_upb_MiniTableField_Offset)(const struct upb_MiniTableField* f) {
return f->UPB_ONLYBITS(offset);
}

UPB_INLINE size_t
_upb_MiniTableField_OneofOffset(const struct upb_MiniTableField* f) {
UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_IsInOneof)(f));
return ~(ptrdiff_t)f->presence;
}
Expand Down

0 comments on commit cdb0782

Please sign in to comment.