blob: 104a21c2e47d17f58c6eeedc687dd4ae2367dfe3 [file] [log] [blame]
wdenk1cb8e982003-03-06 21:55:29 +00001/*
Wolfgang Denk53677ef2008-05-20 16:00:29 +02002 * FILE bitfield.h
wdenk1cb8e982003-03-06 21:55:29 +00003 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +02004 * Version 1.1
5 * Author Copyright (c) Marc A. Viredaz, 1998
6 * DEC Western Research Laboratory, Palo Alto, CA
7 * Date April 1998 (April 1997)
8 * System Advanced RISC Machine (ARM)
wdenk1cb8e982003-03-06 21:55:29 +00009 * Language C or ARM Assembly
Wolfgang Denk53677ef2008-05-20 16:00:29 +020010 * Purpose Definition of macros to operate on bit fields.
wdenk1cb8e982003-03-06 21:55:29 +000011 */
12
13
wdenk1cb8e982003-03-06 21:55:29 +000014#ifndef __BITFIELD_H
15#define __BITFIELD_H
16
17#ifndef __ASSEMBLY__
18#define UData(Data) ((unsigned long) (Data))
19#else
20#define UData(Data) (Data)
21#endif
22
23
24/*
25 * MACRO: Fld
26 *
27 * Purpose
28 * The macro "Fld" encodes a bit field, given its size and its shift value
29 * with respect to bit 0.
30 *
31 * Note
32 * A more intuitive way to encode bit fields would have been to use their
33 * mask. However, extracting size and shift value information from a bit
34 * field's mask is cumbersome and might break the assembler (255-character
35 * line-size limit).
36 *
37 * Input
Wolfgang Denk53677ef2008-05-20 16:00:29 +020038 * Size Size of the bit field, in number of bits.
39 * Shft Shift value of the bit field with respect to bit 0.
wdenk1cb8e982003-03-06 21:55:29 +000040 *
41 * Output
Wolfgang Denk53677ef2008-05-20 16:00:29 +020042 * Fld Encoded bit field.
wdenk1cb8e982003-03-06 21:55:29 +000043 */
44
45#define Fld(Size, Shft) (((Size) << 16) + (Shft))
46
47
48/*
49 * MACROS: FSize, FShft, FMsk, FAlnMsk, F1stBit
50 *
51 * Purpose
52 * The macros "FSize", "FShft", "FMsk", "FAlnMsk", and "F1stBit" return
53 * the size, shift value, mask, aligned mask, and first bit of a
54 * bit field.
55 *
56 * Input
Wolfgang Denk53677ef2008-05-20 16:00:29 +020057 * Field Encoded bit field (using the macro "Fld").
wdenk1cb8e982003-03-06 21:55:29 +000058 *
59 * Output
Wolfgang Denk53677ef2008-05-20 16:00:29 +020060 * FSize Size of the bit field, in number of bits.
61 * FShft Shift value of the bit field with respect to bit 0.
62 * FMsk Mask for the bit field.
63 * FAlnMsk Mask for the bit field, aligned on bit 0.
64 * F1stBit First bit of the bit field.
wdenk1cb8e982003-03-06 21:55:29 +000065 */
66
67#define FSize(Field) ((Field) >> 16)
68#define FShft(Field) ((Field) & 0x0000FFFF)
69#define FMsk(Field) (((UData (1) << FSize (Field)) - 1) << FShft (Field))
70#define FAlnMsk(Field) ((UData (1) << FSize (Field)) - 1)
71#define F1stBit(Field) (UData (1) << FShft (Field))
72
73
74/*
75 * MACRO: FInsrt
76 *
77 * Purpose
78 * The macro "FInsrt" inserts a value into a bit field by shifting the
79 * former appropriately.
80 *
81 * Input
Wolfgang Denk53677ef2008-05-20 16:00:29 +020082 * Value Bit-field value.
83 * Field Encoded bit field (using the macro "Fld").
wdenk1cb8e982003-03-06 21:55:29 +000084 *
85 * Output
Wolfgang Denk53677ef2008-05-20 16:00:29 +020086 * FInsrt Bit-field value positioned appropriately.
wdenk1cb8e982003-03-06 21:55:29 +000087 */
88
89#define FInsrt(Value, Field) \
wdenk8bde7f72003-06-27 21:31:46 +000090 (UData (Value) << FShft (Field))
wdenk1cb8e982003-03-06 21:55:29 +000091
92
93/*
94 * MACRO: FExtr
95 *
96 * Purpose
97 * The macro "FExtr" extracts the value of a bit field by masking and
98 * shifting it appropriately.
99 *
100 * Input
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200101 * Data Data containing the bit-field to be extracted.
102 * Field Encoded bit field (using the macro "Fld").
wdenk1cb8e982003-03-06 21:55:29 +0000103 *
104 * Output
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200105 * FExtr Bit-field value.
wdenk1cb8e982003-03-06 21:55:29 +0000106 */
107
108#define FExtr(Data, Field) \
wdenk8bde7f72003-06-27 21:31:46 +0000109 ((UData (Data) >> FShft (Field)) & FAlnMsk (Field))
wdenk1cb8e982003-03-06 21:55:29 +0000110
111
112#endif /* __BITFIELD_H */