Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-13 08:34:21

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2025 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
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 // Must be last.
0017 #include "upb/port/def.inc"
0018 
0019 // Each fast function is specialized for a cardinality, type, and tag size.
0020 // These macros enumerate all possibilities for each of these three dimensions.
0021 //
0022 // These can be generated in any combination, except that non-primitive types
0023 // cannot be packed.  So we have:
0024 //   {1bt,2bt} x {s,o,r,p} x {b,v32,v64,z32,z64,f32,f64,ce}  // Primitive
0025 //   {1bt,2bt} x {s,o,r}   x {s,b,m}                         // Non-primitive
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)  // kUpb_DecodeFast_Scalar = 0, etc.
0053 } upb_DecodeFast_Cardinality;
0054 
0055 typedef enum {
0056   UPB_DECODEFAST_TYPES(ENUM_VAL)  // kUpb_DecodeFast_Bool = 0, etc.
0057 } upb_DecodeFast_Type;
0058 
0059 typedef enum {
0060   UPB_DECODEFAST_TAGSIZES(ENUM_VAL)  // kUpb_DecodeFast_Tag1Byte = 0, etc.
0061 } upb_DecodeFast_TagSize;
0062 
0063 #undef ENUM_VAL
0064 
0065 #define ADD(_, x) +1
0066 
0067 enum {
0068   // Counts of the number of enum values for each dimension.
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 // Attributes of the various dimensions.
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 // The canonical ordering of functions, used by the arrays of function pointers
0150 // and function names.  This macro generates the full cross product of the
0151 // cardinality, type, and tag size enums.
0152 //
0153 // This ordering generates some combinations that are not actually used (like
0154 // packed strings or messages), but it's simpler than trying to avoid them.
0155 // There are only 14 impossible combinations out of 120 total, so it's not
0156 // worth optimizing for.
0157 #define UPB_DECODEFAST_FUNCTIONS(F) \
0158   UPB_DECODEFAST_TYPES(UPB_DECODEFAST_CARDINALITIES, UPB_DECODEFAST_TAGSIZES, F)
0159 
0160 // The canonical index of a given function.  This must be kept in sync with the
0161 // ordering above, such that this index selects the same function as the
0162 // corresponding UPB_DECODEFAST_FUNCNAME() macro.
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 // Functions to decompose a function index into its constituent parts.
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 // The canonical function name for a given cardinality, type, and tag size.
0188 #define UPB_DECODEFAST_FUNCNAME(type, card, size) \
0189   upb_DecodeFast_##type##_##card##_##size
0190 
0191 // Returns true if we should disable fast decode for this function_idx.  This is
0192 // useful for field types that are known not to work yet.  It is also useful for
0193 // bisecting a test failure to find which function(s) are broken.
0194 //
0195 // This must be a macro because it must evaluate to a compile-time constant,
0196 // since we use it when initializing the fastdecode function array.
0197 //
0198 // This function only applies to field types that have been assigned a function
0199 // index.  Some field types (eg. groups) do not even have a function index at
0200 // the moment, and so will be rejected by upb_DecodeFast_TryFillEntry() before
0201 // we even get here.
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  // UPB_WIRE_INTERNAL_DECODE_FAST_COMBINATIONS_H_