1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef UPB_MINI_TABLE_ENUM_INTERNAL_H_
29 #define UPB_MINI_TABLE_ENUM_INTERNAL_H_
30 
31 #include "upb/mini_table/types.h"
32 
33 // Must be last.
34 #include "upb/port/def.inc"
35 
36 struct upb_MiniTableEnum {
37   uint32_t mask_limit;   // Limit enum value that can be tested with mask.
38   uint32_t value_count;  // Number of values after the bitfield.
39   uint32_t data[];       // Bitmask + enumerated values follow.
40 };
41 
42 typedef enum {
43   _kUpb_FastEnumCheck_ValueIsInEnum = 0,
44   _kUpb_FastEnumCheck_ValueIsNotInEnum = 1,
45   _kUpb_FastEnumCheck_CannotCheckFast = 2,
46 } _kUpb_FastEnumCheck_Status;
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 UPB_INLINE _kUpb_FastEnumCheck_Status
_upb_MiniTable_CheckEnumValueFast(const upb_MiniTableEnum * e,uint32_t val)53 _upb_MiniTable_CheckEnumValueFast(const upb_MiniTableEnum* e, uint32_t val) {
54   if (UPB_UNLIKELY(val >= 64)) return _kUpb_FastEnumCheck_CannotCheckFast;
55   uint64_t mask = e->data[0] | ((uint64_t)e->data[1] << 32);
56   return (mask & (1ULL << val)) ? _kUpb_FastEnumCheck_ValueIsInEnum
57                                 : _kUpb_FastEnumCheck_ValueIsNotInEnum;
58 }
59 
_upb_MiniTable_CheckEnumValueSlow(const upb_MiniTableEnum * e,uint32_t val)60 UPB_INLINE bool _upb_MiniTable_CheckEnumValueSlow(const upb_MiniTableEnum* e,
61                                                   uint32_t val) {
62   if (val < e->mask_limit) return e->data[val / 32] & (1ULL << (val % 32));
63   // OPT: binary search long lists?
64   const uint32_t* start = &e->data[e->mask_limit / 32];
65   const uint32_t* limit = &e->data[(e->mask_limit / 32) + e->value_count];
66   for (const uint32_t* p = start; p < limit; p++) {
67     if (*p == val) return true;
68   }
69   return false;
70 }
71 
72 // Validates enum value against range defined by enum mini table.
upb_MiniTableEnum_CheckValue(const upb_MiniTableEnum * e,uint32_t val)73 UPB_INLINE bool upb_MiniTableEnum_CheckValue(const upb_MiniTableEnum* e,
74                                              uint32_t val) {
75   _kUpb_FastEnumCheck_Status status = _upb_MiniTable_CheckEnumValueFast(e, val);
76   if (UPB_UNLIKELY(status == _kUpb_FastEnumCheck_CannotCheckFast)) {
77     return _upb_MiniTable_CheckEnumValueSlow(e, val);
78   }
79   return status == _kUpb_FastEnumCheck_ValueIsInEnum ? true : false;
80 }
81 
82 #ifdef __cplusplus
83 } /* extern "C" */
84 #endif
85 
86 #include "upb/port/undef.inc"
87 
88 #endif /* UPB_MINI_TABLE_ENUM_INTERNAL_H_ */
89