math.h 5.9 KB

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