1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef ESE_BIT_SPEC_H__
17 #define ESE_BIT_SPEC_H__ 1
18
19 struct bit_spec {
20 uint8_t value;
21 uint8_t shift;
22 };
23
bs_get(const struct bit_spec b,uint8_t var)24 static inline uint8_t bs_get(const struct bit_spec b, uint8_t var) {
25 return (var & (b.value << b.shift)) >> b.shift;
26 }
27
bs_mask(const struct bit_spec b,uint8_t value)28 static inline uint8_t bs_mask(const struct bit_spec b, uint8_t value) {
29 return (value & b.value) << b.shift;
30 }
31
bs_clear(const struct bit_spec b)32 static inline uint8_t bs_clear(const struct bit_spec b) {
33 return (((uint8_t)~0) ^ bs_mask(b, b.value));
34 }
35
bs_set(uint8_t var,const struct bit_spec b,uint8_t value)36 static inline uint8_t bs_set(uint8_t var, const struct bit_spec b, uint8_t value) {
37 return ((var) & bs_clear(b)) | bs_mask(b, value);
38 }
39
bs_assign(uint8_t * dst,const struct bit_spec b,uint8_t value)40 static inline void bs_assign(uint8_t *dst, const struct bit_spec b, uint8_t value) {
41 *dst = bs_set(*dst, b, value);
42 }
43
44
45 #endif /* ESE_BIT_SPEC_H__ */
46
47
48