bitfield.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * FILE bitfield.h
  3. *
  4. * 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)
  9. * Language C or ARM Assembly
  10. * Purpose Definition of macros to operate on bit fields.
  11. */
  12. #ifndef __BITFIELD_H
  13. #define __BITFIELD_H
  14. #ifndef __ASSEMBLY__
  15. #define UData(Data) ((unsigned long) (Data))
  16. #else
  17. #define UData(Data) (Data)
  18. #endif
  19. /*
  20. * MACRO: Fld
  21. *
  22. * Purpose
  23. * The macro "Fld" encodes a bit field, given its size and its shift value
  24. * with respect to bit 0.
  25. *
  26. * Note
  27. * A more intuitive way to encode bit fields would have been to use their
  28. * mask. However, extracting size and shift value information from a bit
  29. * field's mask is cumbersome and might break the assembler (255-character
  30. * line-size limit).
  31. *
  32. * Input
  33. * Size Size of the bit field, in number of bits.
  34. * Shft Shift value of the bit field with respect to bit 0.
  35. *
  36. * Output
  37. * Fld Encoded bit field.
  38. */
  39. #define Fld(Size, Shft) (((Size) << 16) + (Shft))
  40. /*
  41. * MACROS: FSize, FShft, FMsk, FAlnMsk, F1stBit
  42. *
  43. * Purpose
  44. * The macros "FSize", "FShft", "FMsk", "FAlnMsk", and "F1stBit" return
  45. * the size, shift value, mask, aligned mask, and first bit of a
  46. * bit field.
  47. *
  48. * Input
  49. * Field Encoded bit field (using the macro "Fld").
  50. *
  51. * Output
  52. * FSize Size of the bit field, in number of bits.
  53. * FShft Shift value of the bit field with respect to bit 0.
  54. * FMsk Mask for the bit field.
  55. * FAlnMsk Mask for the bit field, aligned on bit 0.
  56. * F1stBit First bit of the bit field.
  57. */
  58. #define FSize(Field) ((Field) >> 16)
  59. #define FShft(Field) ((Field) & 0x0000FFFF)
  60. #define FMsk(Field) (((UData (1) << FSize (Field)) - 1) << FShft (Field))
  61. #define FAlnMsk(Field) ((UData (1) << FSize (Field)) - 1)
  62. #define F1stBit(Field) (UData (1) << FShft (Field))
  63. /*
  64. * MACRO: FInsrt
  65. *
  66. * Purpose
  67. * The macro "FInsrt" inserts a value into a bit field by shifting the
  68. * former appropriately.
  69. *
  70. * Input
  71. * Value Bit-field value.
  72. * Field Encoded bit field (using the macro "Fld").
  73. *
  74. * Output
  75. * FInsrt Bit-field value positioned appropriately.
  76. */
  77. #define FInsrt(Value, Field) \
  78. (UData (Value) << FShft (Field))
  79. /*
  80. * MACRO: FExtr
  81. *
  82. * Purpose
  83. * The macro "FExtr" extracts the value of a bit field by masking and
  84. * shifting it appropriately.
  85. *
  86. * Input
  87. * Data Data containing the bit-field to be extracted.
  88. * Field Encoded bit field (using the macro "Fld").
  89. *
  90. * Output
  91. * FExtr Bit-field value.
  92. */
  93. #define FExtr(Data, Field) \
  94. ((UData (Data) >> FShft (Field)) & FAlnMsk (Field))
  95. #endif /* __BITFIELD_H */