18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *	FILE    	bitfield.h
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *	Version 	1.1
58c2ecf20Sopenharmony_ci *	Author  	Copyright (c) Marc A. Viredaz, 1998
68c2ecf20Sopenharmony_ci *	        	DEC Western Research Laboratory, Palo Alto, CA
78c2ecf20Sopenharmony_ci *	Date    	April 1998 (April 1997)
88c2ecf20Sopenharmony_ci *	System  	Advanced RISC Machine (ARM)
98c2ecf20Sopenharmony_ci *	Language	C or ARM Assembly
108c2ecf20Sopenharmony_ci *	Purpose 	Definition of macros to operate on bit fields.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#ifndef __BITFIELD_H
168c2ecf20Sopenharmony_ci#define __BITFIELD_H
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
198c2ecf20Sopenharmony_ci#define UData(Data)	((unsigned long) (Data))
208c2ecf20Sopenharmony_ci#else
218c2ecf20Sopenharmony_ci#define UData(Data)	(Data)
228c2ecf20Sopenharmony_ci#endif
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * MACRO: Fld
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * Purpose
298c2ecf20Sopenharmony_ci *    The macro "Fld" encodes a bit field, given its size and its shift value
308c2ecf20Sopenharmony_ci *    with respect to bit 0.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * Note
338c2ecf20Sopenharmony_ci *    A more intuitive way to encode bit fields would have been to use their
348c2ecf20Sopenharmony_ci *    mask. However, extracting size and shift value information from a bit
358c2ecf20Sopenharmony_ci *    field's mask is cumbersome and might break the assembler (255-character
368c2ecf20Sopenharmony_ci *    line-size limit).
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * Input
398c2ecf20Sopenharmony_ci *    Size      	Size of the bit field, in number of bits.
408c2ecf20Sopenharmony_ci *    Shft      	Shift value of the bit field with respect to bit 0.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * Output
438c2ecf20Sopenharmony_ci *    Fld       	Encoded bit field.
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define Fld(Size, Shft)	(((Size) << 16) + (Shft))
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * MACROS: FSize, FShft, FMsk, FAlnMsk, F1stBit
518c2ecf20Sopenharmony_ci *
528c2ecf20Sopenharmony_ci * Purpose
538c2ecf20Sopenharmony_ci *    The macros "FSize", "FShft", "FMsk", "FAlnMsk", and "F1stBit" return
548c2ecf20Sopenharmony_ci *    the size, shift value, mask, aligned mask, and first bit of a
558c2ecf20Sopenharmony_ci *    bit field.
568c2ecf20Sopenharmony_ci *
578c2ecf20Sopenharmony_ci * Input
588c2ecf20Sopenharmony_ci *    Field     	Encoded bit field (using the macro "Fld").
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * Output
618c2ecf20Sopenharmony_ci *    FSize     	Size of the bit field, in number of bits.
628c2ecf20Sopenharmony_ci *    FShft     	Shift value of the bit field with respect to bit 0.
638c2ecf20Sopenharmony_ci *    FMsk      	Mask for the bit field.
648c2ecf20Sopenharmony_ci *    FAlnMsk   	Mask for the bit field, aligned on bit 0.
658c2ecf20Sopenharmony_ci *    F1stBit   	First bit of the bit field.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define FSize(Field)	((Field) >> 16)
698c2ecf20Sopenharmony_ci#define FShft(Field)	((Field) & 0x0000FFFF)
708c2ecf20Sopenharmony_ci#define FMsk(Field)	(((UData (1) << FSize (Field)) - 1) << FShft (Field))
718c2ecf20Sopenharmony_ci#define FAlnMsk(Field)	((UData (1) << FSize (Field)) - 1)
728c2ecf20Sopenharmony_ci#define F1stBit(Field)	(UData (1) << FShft (Field))
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/*
768c2ecf20Sopenharmony_ci * MACRO: FInsrt
778c2ecf20Sopenharmony_ci *
788c2ecf20Sopenharmony_ci * Purpose
798c2ecf20Sopenharmony_ci *    The macro "FInsrt" inserts a value into a bit field by shifting the
808c2ecf20Sopenharmony_ci *    former appropriately.
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * Input
838c2ecf20Sopenharmony_ci *    Value     	Bit-field value.
848c2ecf20Sopenharmony_ci *    Field     	Encoded bit field (using the macro "Fld").
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci * Output
878c2ecf20Sopenharmony_ci *    FInsrt    	Bit-field value positioned appropriately.
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define FInsrt(Value, Field) \
918c2ecf20Sopenharmony_ci                	(UData (Value) << FShft (Field))
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/*
958c2ecf20Sopenharmony_ci * MACRO: FExtr
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * Purpose
988c2ecf20Sopenharmony_ci *    The macro "FExtr" extracts the value of a bit field by masking and
998c2ecf20Sopenharmony_ci *    shifting it appropriately.
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * Input
1028c2ecf20Sopenharmony_ci *    Data      	Data containing the bit-field to be extracted.
1038c2ecf20Sopenharmony_ci *    Field     	Encoded bit field (using the macro "Fld").
1048c2ecf20Sopenharmony_ci *
1058c2ecf20Sopenharmony_ci * Output
1068c2ecf20Sopenharmony_ci *    FExtr     	Bit-field value.
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define FExtr(Data, Field) \
1108c2ecf20Sopenharmony_ci                	((UData (Data) >> FShft (Field)) & FAlnMsk (Field))
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#endif /* __BITFIELD_H */
114