1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 ///
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8
9 #ifndef UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_
10 #define UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_
11
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #include "upb/base/descriptor_constants.h"
16
17 // Must be last.
18 #include "upb/port/def.inc"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 // Return the log2 of the storage size in bytes for a upb_CType
UPB_PRIVATE(_upb_CType_SizeLg2)25 UPB_INLINE int UPB_PRIVATE(_upb_CType_SizeLg2)(upb_CType c_type) {
26 static const int8_t size[] = {
27 0, // kUpb_CType_Bool
28 2, // kUpb_CType_Float
29 2, // kUpb_CType_Int32
30 2, // kUpb_CType_UInt32
31 2, // kUpb_CType_Enum
32 UPB_SIZE(2, 3), // kUpb_CType_Message
33 3, // kUpb_CType_Double
34 3, // kUpb_CType_Int64
35 3, // kUpb_CType_UInt64
36 UPB_SIZE(3, 4), // kUpb_CType_String
37 UPB_SIZE(3, 4), // kUpb_CType_Bytes
38 };
39
40 // -1 here because the enum is one-based but the table is zero-based.
41 return size[c_type - 1];
42 }
43
44 // Return the log2 of the storage size in bytes for a upb_FieldType
UPB_PRIVATE(_upb_FieldType_SizeLg2)45 UPB_INLINE int UPB_PRIVATE(_upb_FieldType_SizeLg2)(upb_FieldType field_type) {
46 static const int8_t size[] = {
47 3, // kUpb_FieldType_Double
48 2, // kUpb_FieldType_Float
49 3, // kUpb_FieldType_Int64
50 3, // kUpb_FieldType_UInt64
51 2, // kUpb_FieldType_Int32
52 3, // kUpb_FieldType_Fixed64
53 2, // kUpb_FieldType_Fixed32
54 0, // kUpb_FieldType_Bool
55 UPB_SIZE(3, 4), // kUpb_FieldType_String
56 UPB_SIZE(2, 3), // kUpb_FieldType_Group
57 UPB_SIZE(2, 3), // kUpb_FieldType_Message
58 UPB_SIZE(3, 4), // kUpb_FieldType_Bytes
59 2, // kUpb_FieldType_UInt32
60 2, // kUpb_FieldType_Enum
61 2, // kUpb_FieldType_SFixed32
62 3, // kUpb_FieldType_SFixed64
63 2, // kUpb_FieldType_SInt32
64 3, // kUpb_FieldType_SInt64
65 };
66
67 // -1 here because the enum is one-based but the table is zero-based.
68 return size[field_type - 1];
69 }
70
71 #ifdef __cplusplus
72 } /* extern "C" */
73 #endif
74
75 #include "upb/port/undef.inc"
76
77 #endif /* UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_ */
78