File indexing completed on 2026-07-13 08:34:21
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef UPB_WIRE_INTERNAL_DECODE_FAST_COMBINATIONS_H_
0009 #define UPB_WIRE_INTERNAL_DECODE_FAST_COMBINATIONS_H_
0010
0011 #include <stdint.h>
0012
0013 #include "upb/base/internal/log2.h"
0014 #include "upb/wire/types.h"
0015
0016
0017 #include "upb/port/def.inc"
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #define UPB_DECODEFAST_CARDINALITIES(F, ...) \
0028 F(__VA_ARGS__, Scalar) \
0029 F(__VA_ARGS__, Oneof) \
0030 F(__VA_ARGS__, Repeated) \
0031 F(__VA_ARGS__, Packed)
0032
0033 #define UPB_DECODEFAST_TYPES(F, ...) \
0034 F(__VA_ARGS__, Bool) \
0035 F(__VA_ARGS__, Varint32) \
0036 F(__VA_ARGS__, Varint64) \
0037 F(__VA_ARGS__, ZigZag32) \
0038 F(__VA_ARGS__, ZigZag64) \
0039 F(__VA_ARGS__, Fixed32) \
0040 F(__VA_ARGS__, Fixed64) \
0041 F(__VA_ARGS__, String) \
0042 F(__VA_ARGS__, Bytes) \
0043 F(__VA_ARGS__, Message)
0044
0045 #define UPB_DECODEFAST_TAGSIZES(F, ...) \
0046 F(__VA_ARGS__, Tag1Byte) \
0047 F(__VA_ARGS__, Tag2Byte)
0048
0049 #define ENUM_VAL(_, x) kUpb_DecodeFast_##x,
0050
0051 typedef enum {
0052 UPB_DECODEFAST_CARDINALITIES(ENUM_VAL)
0053 } upb_DecodeFast_Cardinality;
0054
0055 typedef enum {
0056 UPB_DECODEFAST_TYPES(ENUM_VAL)
0057 } upb_DecodeFast_Type;
0058
0059 typedef enum {
0060 UPB_DECODEFAST_TAGSIZES(ENUM_VAL)
0061 } upb_DecodeFast_TagSize;
0062
0063 #undef ENUM_VAL
0064
0065 #define ADD(_, x) +1
0066
0067 enum {
0068
0069 kUpb_DecodeFast_CardinalityCount = UPB_DECODEFAST_CARDINALITIES(ADD),
0070 kUpb_DecodeFast_TypeCount = UPB_DECODEFAST_TYPES(ADD),
0071 kUpb_DecodeFast_TagSizeCount = UPB_DECODEFAST_TAGSIZES(ADD),
0072 };
0073
0074 #undef ADD
0075
0076
0077
0078 UPB_INLINE int upb_DecodeFast_TagSizeBytes(upb_DecodeFast_TagSize size) {
0079 switch (size) {
0080 case kUpb_DecodeFast_Tag1Byte:
0081 return 1;
0082 case kUpb_DecodeFast_Tag2Byte:
0083 return 2;
0084 default:
0085 UPB_UNREACHABLE();
0086 }
0087 }
0088
0089 UPB_INLINE int upb_DecodeFast_ValueBytes(upb_DecodeFast_Type type) {
0090 switch (type) {
0091 case kUpb_DecodeFast_Bool:
0092 return 1;
0093 case kUpb_DecodeFast_Varint32:
0094 case kUpb_DecodeFast_ZigZag32:
0095 case kUpb_DecodeFast_Fixed32:
0096 return 4;
0097 case kUpb_DecodeFast_Varint64:
0098 case kUpb_DecodeFast_ZigZag64:
0099 case kUpb_DecodeFast_Fixed64:
0100 case kUpb_DecodeFast_Message:
0101 return 8;
0102 case kUpb_DecodeFast_String:
0103 case kUpb_DecodeFast_Bytes:
0104 return 16;
0105 default:
0106 UPB_UNREACHABLE();
0107 }
0108 }
0109
0110 UPB_INLINE upb_WireType upb_DecodeFast_WireType(upb_DecodeFast_Type type) {
0111 switch (type) {
0112 case kUpb_DecodeFast_Bool:
0113 case kUpb_DecodeFast_Varint32:
0114 case kUpb_DecodeFast_Varint64:
0115 case kUpb_DecodeFast_ZigZag32:
0116 case kUpb_DecodeFast_ZigZag64:
0117 return kUpb_WireType_Varint;
0118 case kUpb_DecodeFast_Fixed32:
0119 return kUpb_WireType_32Bit;
0120 case kUpb_DecodeFast_Fixed64:
0121 return kUpb_WireType_64Bit;
0122 case kUpb_DecodeFast_Message:
0123 case kUpb_DecodeFast_String:
0124 case kUpb_DecodeFast_Bytes:
0125 return kUpb_WireType_Delimited;
0126 default:
0127 UPB_UNREACHABLE();
0128 }
0129 }
0130
0131 UPB_INLINE int upb_DecodeFast_ValueBytesLg2(upb_DecodeFast_Type type) {
0132 return upb_Log2Ceiling(upb_DecodeFast_ValueBytes(type));
0133 }
0134
0135 UPB_INLINE bool upb_DecodeFast_IsRepeated(upb_DecodeFast_Cardinality card) {
0136 return card == kUpb_DecodeFast_Repeated || card == kUpb_DecodeFast_Packed;
0137 }
0138
0139 UPB_INLINE bool upb_DecodeFast_IsZigZag(upb_DecodeFast_Type type) {
0140 switch (type) {
0141 case kUpb_DecodeFast_ZigZag32:
0142 case kUpb_DecodeFast_ZigZag64:
0143 return true;
0144 default:
0145 return false;
0146 }
0147 }
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157 #define UPB_DECODEFAST_FUNCTIONS(F) \
0158 UPB_DECODEFAST_TYPES(UPB_DECODEFAST_CARDINALITIES, UPB_DECODEFAST_TAGSIZES, F)
0159
0160
0161
0162
0163 #define UPB_DECODEFAST_FUNCTION_IDX(type, card, size) \
0164 (((uint32_t)type * kUpb_DecodeFast_CardinalityCount * \
0165 kUpb_DecodeFast_TagSizeCount) + \
0166 ((uint32_t)card * kUpb_DecodeFast_TagSizeCount) + (uint32_t)size)
0167
0168
0169 UPB_INLINE upb_DecodeFast_TagSize
0170 upb_DecodeFast_GetTagSize(uint32_t function_idx) {
0171 return (upb_DecodeFast_TagSize)(function_idx % kUpb_DecodeFast_TagSizeCount);
0172 }
0173
0174 UPB_INLINE upb_DecodeFast_Cardinality
0175 upb_DecodeFast_GetCardinality(uint32_t function_idx) {
0176 return (upb_DecodeFast_Cardinality)((function_idx /
0177 kUpb_DecodeFast_TagSizeCount) %
0178 kUpb_DecodeFast_CardinalityCount);
0179 }
0180
0181 UPB_INLINE upb_DecodeFast_Type upb_DecodeFast_GetType(uint32_t function_idx) {
0182 return (upb_DecodeFast_Type)(function_idx /
0183 (kUpb_DecodeFast_TagSizeCount *
0184 kUpb_DecodeFast_CardinalityCount));
0185 }
0186
0187
0188 #define UPB_DECODEFAST_FUNCNAME(type, card, size) \
0189 upb_DecodeFast_##type##_##card##_##size
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 #define UPB_DECODEFAST_COMBINATION_IS_ENABLED(type, card, size) \
0203 (type == kUpb_DecodeFast_Fixed32 || type == kUpb_DecodeFast_Fixed64)
0204
0205 #ifdef UPB_DECODEFAST_DISABLE_FUNCTIONS_ABOVE
0206 #define UPB_DECODEFAST_ISENABLED(type, card, size) \
0207 (UPB_DECODEFAST_COMBINATION_IS_ENABLED(type, card, size) && \
0208 (UPB_DECODEFAST_FUNCION_IDX(type, card, size) <= \
0209 UPB_DECODEFAST_DISABLE_FUNCTIONS_ABOVE))
0210 #else
0211 #define UPB_DECODEFAST_ISENABLED(type, card, size) \
0212 UPB_DECODEFAST_COMBINATION_IS_ENABLED(type, card, size)
0213 #endif
0214
0215 #include "upb/port/undef.inc"
0216
0217 #endif