1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef JRT_BIT_FIELDS_H
17#define JRT_BIT_FIELDS_H
18
19/**
20 * Extract a bit-field.
21 *
22 * @param type type of container
23 * @param container container to extract bit-field from
24 * @param lsb least significant bit of the value to be extracted
25 * @param width width of the bit-field to be extracted
26 * @return bit-field's value
27 */
28#define JRT_EXTRACT_BIT_FIELD(type, container, lsb, width) \
29 (((container) >> lsb) & ((((type) 1) << (width)) - 1))
30
31/**
32 * Set a bit-field.
33 *
34 * @param type type of container
35 * @param container container to insert bit-field to
36 * @param new_bit_field_value value of bit-field to insert
37 * @param lsb least significant bit of the value to be inserted
38 * @param width width of the bit-field to be inserted
39 * @return bit-field's value
40 */
41#define JRT_SET_BIT_FIELD_VALUE(type, container, new_bit_field_value, lsb, width) \
42 (((container) & ~(((((type) 1) << (width)) - 1) << (lsb))) | (((type) new_bit_field_value) << (lsb)))
43
44#endif /* !JRT_BIT_FIELDS_H */
45