math.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #define bit_read(value, bit) (((value) >> (bit)) & 0x01)
  6. #define bit_set(value, bit) \
  7. ({ \
  8. __typeof__(value) _one = (1); \
  9. (value) |= (_one << (bit)); \
  10. })
  11. #define bit_clear(value, bit) \
  12. ({ \
  13. __typeof__(value) _one = (1); \
  14. (value) &= ~(_one << (bit)); \
  15. })
  16. #define bit_write(value, bit, bitvalue) (bitvalue ? bit_set(value, bit) : bit_clear(value, bit))
  17. #define DURATION_DIFF(x, y) (((x) < (y)) ? ((y) - (x)) : ((x) - (y)))
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /** Flip the data bitwise
  22. *
  23. * @param key In data
  24. * @param bit_count number of data bits
  25. *
  26. * @return Reverse data
  27. */
  28. uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t bit_count);
  29. /** Get parity the data bitwise
  30. *
  31. * @param key In data
  32. * @param bit_count number of data bits
  33. *
  34. * @return parity
  35. */
  36. uint8_t subghz_protocol_blocks_get_parity(uint64_t key, uint8_t bit_count);
  37. /** CRC-4
  38. *
  39. * @param message array of bytes to check
  40. * @param size number of bytes in message
  41. * @param polynomial CRC polynomial
  42. * @param init starting crc value
  43. *
  44. * @return CRC value
  45. */
  46. uint8_t subghz_protocol_blocks_crc4(
  47. uint8_t const message[],
  48. size_t size,
  49. uint8_t polynomial,
  50. uint8_t init);
  51. /** CRC-7
  52. *
  53. * @param message array of bytes to check
  54. * @param size number of bytes in message
  55. * @param polynomial CRC polynomial
  56. * @param init starting crc value
  57. *
  58. * @return CRC value
  59. */
  60. uint8_t subghz_protocol_blocks_crc7(
  61. uint8_t const message[],
  62. size_t size,
  63. uint8_t polynomial,
  64. uint8_t init);
  65. /** Generic Cyclic Redundancy Check CRC-8. Example polynomial: 0x31 = x8 + x5 +
  66. * x4 + 1 (x8 is implicit) Example polynomial: 0x80 = x8 + x7 (a normal
  67. * bit-by-bit parity XOR)
  68. *
  69. * @param message array of bytes to check
  70. * @param size number of bytes in message
  71. * @param polynomial byte is from x^7 to x^0 (x^8 is implicitly one)
  72. * @param init starting crc value
  73. *
  74. * @return CRC value
  75. */
  76. uint8_t subghz_protocol_blocks_crc8(
  77. uint8_t const message[],
  78. size_t size,
  79. uint8_t polynomial,
  80. uint8_t init);
  81. /** "Little-endian" Cyclic Redundancy Check CRC-8 LE Input and output are
  82. * reflected, i.e. least significant bit is shifted in first
  83. *
  84. * @param message array of bytes to check
  85. * @param size number of bytes in message
  86. * @param polynomial CRC polynomial
  87. * @param init starting crc value
  88. *
  89. * @return CRC value
  90. */
  91. uint8_t subghz_protocol_blocks_crc8le(
  92. uint8_t const message[],
  93. size_t size,
  94. uint8_t polynomial,
  95. uint8_t init);
  96. /** CRC-16 LSB. Input and output are reflected, i.e. least significant bit is
  97. * shifted in first. Note that poly and init already need to be reflected
  98. *
  99. * @param message array of bytes to check
  100. * @param size number of bytes in message
  101. * @param polynomial CRC polynomial
  102. * @param init starting crc value
  103. *
  104. * @return CRC value
  105. */
  106. uint16_t subghz_protocol_blocks_crc16lsb(
  107. uint8_t const message[],
  108. size_t size,
  109. uint16_t polynomial,
  110. uint16_t init);
  111. /** CRC-16
  112. *
  113. * @param message array of bytes to check
  114. * @param size number of bytes in message
  115. * @param polynomial CRC polynomial
  116. * @param init starting crc value
  117. *
  118. * @return CRC value
  119. */
  120. uint16_t subghz_protocol_blocks_crc16(
  121. uint8_t const message[],
  122. size_t size,
  123. uint16_t polynomial,
  124. uint16_t init);
  125. /** Digest-8 by "LFSR-based Toeplitz hash"
  126. *
  127. * @param message bytes of message data
  128. * @param size number of bytes to digest
  129. * @param gen key stream generator, needs to includes the MSB if the
  130. * LFSR is rolling
  131. * @param key initial key
  132. *
  133. * @return digest value
  134. */
  135. uint8_t subghz_protocol_blocks_lfsr_digest8(
  136. uint8_t const message[],
  137. size_t size,
  138. uint8_t gen,
  139. uint8_t key);
  140. /** Digest-8 by "LFSR-based Toeplitz hash", byte reflect, bit reflect
  141. *
  142. * @param message bytes of message data
  143. * @param size number of bytes to digest
  144. * @param gen key stream generator, needs to includes the MSB if the
  145. * LFSR is rolling
  146. * @param key initial key
  147. *
  148. * @return digest value
  149. */
  150. uint8_t subghz_protocol_blocks_lfsr_digest8_reflect(
  151. uint8_t const message[],
  152. size_t size,
  153. uint8_t gen,
  154. uint8_t key);
  155. /** Digest-16 by "LFSR-based Toeplitz hash"
  156. *
  157. * @param message bytes of message data
  158. * @param size number of bytes to digest
  159. * @param gen key stream generator, needs to includes the MSB if the
  160. * LFSR is rolling
  161. * @param key initial key
  162. *
  163. * @return digest value
  164. */
  165. uint16_t subghz_protocol_blocks_lfsr_digest16(
  166. uint8_t const message[],
  167. size_t size,
  168. uint16_t gen,
  169. uint16_t key);
  170. /** Compute Addition of a number of bytes
  171. *
  172. * @param message bytes of message data
  173. * @param size number of bytes to sum
  174. *
  175. * @return summation value
  176. */
  177. uint8_t subghz_protocol_blocks_add_bytes(uint8_t const message[], size_t size);
  178. /** Compute bit parity of a single byte (8 bits)
  179. *
  180. * @param byte single byte to check
  181. *
  182. * @return 1 odd parity, 0 even parity
  183. */
  184. uint8_t subghz_protocol_blocks_parity8(uint8_t byte);
  185. /** Compute bit parity of a number of bytes
  186. *
  187. * @param message bytes of message data
  188. * @param size number of bytes to sum
  189. *
  190. * @return 1 odd parity, 0 even parity
  191. */
  192. uint8_t subghz_protocol_blocks_parity_bytes(uint8_t const message[], size_t size);
  193. /** Compute XOR (byte-wide parity) of a number of bytes
  194. *
  195. * @param message bytes of message data
  196. * @param size number of bytes to sum
  197. *
  198. * @return summation value, per bit-position 1 odd parity, 0 even parity
  199. */
  200. uint8_t subghz_protocol_blocks_xor_bytes(uint8_t const message[], size_t size);
  201. #ifdef __cplusplus
  202. }
  203. #endif