NumberCompress.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef NumberCompress_H
  16. #define NumberCompress_H
  17. #include "switch/EncodingScheme.h"
  18. #include "util/Bits.h"
  19. #include <stdint.h>
  20. /* right now 4 implementations:
  21. * - fixed 4 bits: 15 peers
  22. * - fixed 8 bits: 240 peers
  23. * - dynamically 4-10 bits: 256 peers
  24. * - dynamically 5-9 bits: 256 peers
  25. */
  26. /* implementations
  27. *
  28. * they are all accesible with their internal names for unit testing
  29. */
  30. /**********************
  31. * Fixed 4 bit scheme: 15 peers + 1 router
  32. **********************/
  33. # define NumberCompress_f4_INTERFACES 16
  34. static inline struct EncodingScheme* NumberCompress_f4_defineScheme(struct Allocator* alloc)
  35. {
  36. return EncodingScheme_defineFixedWidthScheme(4, alloc);
  37. }
  38. static inline uint32_t NumberCompress_f4_bitsUsedForLabel(const uint64_t label)
  39. {
  40. return 4;
  41. }
  42. static inline uint32_t NumberCompress_f4_bitsUsedForNumber(const uint32_t number)
  43. {
  44. return 4;
  45. }
  46. static inline uint64_t NumberCompress_f4_getCompressed(const uint32_t number,
  47. const uint32_t bitsUsed)
  48. {
  49. return number;
  50. }
  51. static inline uint32_t NumberCompress_f4_getDecompressed(const uint64_t label,
  52. const uint32_t bitsUsed)
  53. {
  54. return label & 0xf;
  55. }
  56. /**********************
  57. * (Fixed) 8 bit scheme: 240 peers + 1 router
  58. **********************/
  59. // Basic idea: encode number XXXXYYYY as YYYY(XXXX)'; (XXXX)' is XXXX+1 if XXXX != 0, otherwise XXXX
  60. // that way XXXX is never 1
  61. // so map numbers 16-239 -> 32-255, and 240 <-> 1, then swap nibbles
  62. # define NumberCompress_f8_INTERFACES 241
  63. static inline struct EncodingScheme* NumberCompress_f8_defineScheme(struct Allocator* alloc)
  64. {
  65. return EncodingScheme_defineFixedWidthScheme(8, alloc);
  66. }
  67. static inline uint32_t NumberCompress_f8_bitsUsedForLabel(const uint64_t label)
  68. {
  69. if (1 == (label & 0xf)) {
  70. return 4;
  71. }
  72. return 8;
  73. }
  74. static inline uint32_t NumberCompress_f8_bitsUsedForNumber(const uint32_t number)
  75. {
  76. if (1 == number) {
  77. return 4;
  78. }
  79. return 8;
  80. }
  81. static inline uint64_t NumberCompress_f8_getCompressed(const uint32_t number,
  82. const uint32_t bitsUsed)
  83. {
  84. if (1 == number) {
  85. return 1;
  86. }
  87. if (240 == number) {
  88. return 0x10;
  89. }
  90. uint32_t low = number & 0xf;
  91. uint32_t high = (number >> 4) & 0xf;
  92. if (high > 0) {
  93. ++high;
  94. }
  95. return (low << 4) + high;
  96. }
  97. static inline uint32_t NumberCompress_f8_getDecompressed(const uint64_t label,
  98. const uint32_t bitsUsed)
  99. {
  100. uint32_t low = label & 0xf;
  101. if (1 == low) {
  102. return 1;
  103. }
  104. if (0x10 == (label & 0xff)) {
  105. return 240;
  106. }
  107. uint32_t high = (label >> 4) & 0xf;
  108. if (low > 0) {
  109. --low; // low != 1
  110. }
  111. return (low << 4) + high;
  112. }
  113. /*
  114. * 3/5/8 bit dynamic number compression scheme:
  115. *
  116. * scheme data suffix range bits used
  117. * route 0001 1 4 (number 1 always encoded as 0001)
  118. * 0 000-111 1 0-7 4 (swapped number 0 and 1)
  119. * 1 00000-11111 10 0-32 7 (skip the number 1)
  120. * 2 00000000-11111111 00 0-256 10 (skip the number 1)
  121. */
  122. # define NumberCompress_v3x5x8_INTERFACES 257
  123. static inline struct EncodingScheme* NumberCompress_v3x5x8_defineScheme(struct Allocator* alloc)
  124. {
  125. return EncodingScheme_defineDynWidthScheme(
  126. ((struct EncodingScheme_Form[3]) {
  127. { .bitCount = 3, .prefixLen = 1, .prefix = 1, },
  128. { .bitCount = 5, .prefixLen = 2, .prefix = 1<<1, },
  129. { .bitCount = 8, .prefixLen = 2, .prefix = 0, }
  130. }),
  131. 3,
  132. alloc);
  133. }
  134. static inline uint32_t NumberCompress_v3x5x8_bitsUsedForLabel(const uint64_t label)
  135. {
  136. if (0 != (label & 0x1)) {
  137. return 4;
  138. }
  139. if (0 != (label & 0x2)) {
  140. return 7;
  141. }
  142. return 10;
  143. }
  144. static inline uint32_t NumberCompress_v3x5x8_bitsUsedForNumber(const uint32_t number)
  145. {
  146. if (number < 8) {
  147. return 4;
  148. } else if (number < 33) {
  149. return 7;
  150. } else {
  151. return 10;
  152. }
  153. }
  154. static inline uint64_t NumberCompress_v3x5x8_getCompressed(const uint32_t number,
  155. const uint32_t bitsUsed)
  156. {
  157. if (1 == number) {
  158. return 1;
  159. }
  160. switch (bitsUsed) {
  161. case 4:
  162. if (0 == number) {
  163. return 3;
  164. }
  165. return (number << 1) | 1;
  166. case 7:
  167. if (0 == number) {
  168. return 2;
  169. }
  170. // skip the number 1
  171. return ((number-1) << 2) | 2;
  172. case 10:
  173. if (0 == number) {
  174. return 0;
  175. }
  176. // skip the number 1
  177. return ((number-1) << 2);
  178. default: return 0;
  179. }
  180. }
  181. static inline uint32_t NumberCompress_v3x5x8_getDecompressed(const uint64_t label,
  182. const uint32_t bitsUsed)
  183. {
  184. uint32_t number;
  185. switch (bitsUsed) {
  186. case 4:
  187. number = (label >> 1) & 0x7u;
  188. if (0 == number) {
  189. return 1;
  190. }
  191. if (1 == number) {
  192. return 0;
  193. }
  194. return number;
  195. case 7:
  196. number = (label >> 2) & 0x1fu;
  197. if (0 != number) {
  198. ++number; // skip the number 1
  199. }
  200. return number;
  201. case 10:
  202. number = (label >> 2) & 0xffu;
  203. if (0 != number) {
  204. ++number; // skip the number 1
  205. }
  206. return number;
  207. default: return 0;
  208. }
  209. }
  210. /*
  211. * 4/8 Dynamic number compression scheme:
  212. *
  213. * scheme data suffix range bits used
  214. * 0 0000-1111 1 0-15 5 (00001 indicates loopback route)
  215. * 1 00000000-11111111 0 0-255 9
  216. */
  217. # define NumberCompress_v4x8_INTERFACES 257
  218. static inline struct EncodingScheme* NumberCompress_v4x8_defineScheme(struct Allocator* alloc)
  219. {
  220. return EncodingScheme_defineDynWidthScheme(
  221. ((struct EncodingScheme_Form[]) {
  222. { .bitCount = 4, .prefixLen = 1, .prefix = 1, },
  223. { .bitCount = 8, .prefixLen = 1, .prefix = 0, }
  224. }),
  225. 2,
  226. alloc);
  227. }
  228. static inline uint32_t NumberCompress_v4x8_getDecompressed(const uint64_t label,
  229. const uint32_t bitsUsed)
  230. {
  231. if ((label & 0x1f) == 1) { return 1; }
  232. switch (bitsUsed) {
  233. case 5: {
  234. uint32_t number = (label >> 1) & 0xfu;
  235. if (1 == number) { return 0; }
  236. return number;
  237. }
  238. case 9: {
  239. uint32_t number = (label >> 1) & 0xffu;
  240. if (number) { number++; } // skip the number 1
  241. return number;
  242. }
  243. default: Assert_ifTesting(0);
  244. }
  245. return 0;
  246. }
  247. static inline uint64_t NumberCompress_v4x8_getCompressed(uint32_t number,
  248. const uint32_t bitsUsed)
  249. {
  250. if (1 == number) { return 1; }
  251. switch (bitsUsed) {
  252. case 5:
  253. // 10001 is reserved
  254. Assert_ifTesting(number < 16);
  255. // 0 is encoded as 0011, 1 is handled seperately
  256. if (number == 0) { number = 1; }
  257. return (number << 1) | 1;
  258. case 9:
  259. Assert_ifTesting(number < 256);
  260. // 2 is encoded as 1, 1 and 0 are both encoded as 0 because 1 never happens.
  261. if (number) { number--; }
  262. return number << 1;
  263. default: Assert_ifTesting(0);
  264. }
  265. return 0;
  266. }
  267. static inline uint32_t NumberCompress_v4x8_bitsUsedForLabel(const uint64_t label)
  268. {
  269. return (label & 1) ? 5 : 9;
  270. }
  271. static inline uint32_t NumberCompress_v4x8_bitsUsedForNumber(const uint32_t number)
  272. {
  273. Assert_ifTesting(number < 257);
  274. return (number < 15) ? 5 : 9;
  275. }
  276. #define NumberCompress_MKNAME(x) NumberCompress__MKNAME(NumberCompress_TYPE, x)
  277. #define NumberCompress__MKNAME(y, x) NumberCompress___MKNAME(y, x)
  278. #define NumberCompress___MKNAME(y, x) NumberCompress_ ## y ## _ ## x
  279. #define NumberCompress_INTERFACES NumberCompress_MKNAME(INTERFACES)
  280. #define NumberCompress_defineScheme(a) NumberCompress_MKNAME(defineScheme)(a)
  281. #define NumberCompress_bitsUsedForLabel(label) NumberCompress_MKNAME(bitsUsedForLabel)(label)
  282. #define NumberCompress_bitsUsedForNumber(number) NumberCompress_MKNAME(bitsUsedForNumber)(number)
  283. #define NumberCompress_getCompressed(a, b) NumberCompress_MKNAME(getCompressed)(a, b)
  284. #define NumberCompress_getDecompressed(a, b) NumberCompress_MKNAME(getDecompressed)(a, b)
  285. // conveinence macro
  286. #define NumberCompress_decompress(label) \
  287. NumberCompress_getDecompressed(label, NumberCompress_bitsUsedForLabel(label))
  288. #endif