bitfield.h 2.8 KB

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