math.h 5.9 KB

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