NumberCompress.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef NumberCompress_H
  16. #define NumberCompress_H
  17. #if defined(SUBNODE) && !defined(NumberCompress_OLD_CODE)
  18. #error "new code should be trying to use EncodingScheme instead of NumberCompress"
  19. #endif
  20. #include "switch/EncodingScheme.h"
  21. #include "util/Bits.h"
  22. #include "util/Constant.h"
  23. #include <stdint.h>
  24. /* right now 4 implementations:
  25. * - fixed 4 bits: 14 peers + self
  26. * - fixed 8 bits: 254 peers + self
  27. * - dynamically 4-8 bits: 256 peers
  28. * - dynamically 3-5-8 bits: 256 peers
  29. */
  30. /* implementations
  31. *
  32. * they are all accesible with their internal names for unit testing
  33. */
  34. /**********************
  35. * Fixed 4 bit scheme: 15 peers + 1 router
  36. **********************/
  37. # define NumberCompress_f4_INTERFACES 16
  38. static inline struct EncodingScheme* NumberCompress_f4_defineScheme(struct Allocator* alloc)
  39. {
  40. return EncodingScheme_defineFixedWidthScheme(4, alloc);
  41. }
  42. static inline uint32_t NumberCompress_f4_bitsUsedForLabel(const uint64_t label)
  43. {
  44. return 4;
  45. }
  46. static inline uint32_t NumberCompress_f4_bitsUsedForNumber(const uint32_t number)
  47. {
  48. Assert_true(number < 16);
  49. return 4;
  50. }
  51. static inline uint64_t NumberCompress_f4_getCompressed(const uint32_t number,
  52. const uint32_t bitsUsed)
  53. {
  54. Assert_true(number < 16);
  55. return number;
  56. }
  57. static inline uint32_t NumberCompress_f4_getDecompressed(const uint64_t label,
  58. const uint32_t bitsUsed)
  59. {
  60. return label & 0xf;
  61. }
  62. # define NumberCompress_f8_INTERFACES 256
  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. return 8;
  70. }
  71. static inline uint32_t NumberCompress_f8_bitsUsedForNumber(const uint32_t number)
  72. {
  73. Assert_true(number < 256);
  74. return 8;
  75. }
  76. static inline uint64_t NumberCompress_f8_getCompressed(const uint32_t number,
  77. const uint32_t bitsUsed)
  78. {
  79. Assert_true(number < 256);
  80. return number;
  81. }
  82. static inline uint32_t NumberCompress_f8_getDecompressed(const uint64_t label,
  83. const uint32_t bitsUsed)
  84. {
  85. return label & 0xff;
  86. }
  87. /*
  88. * 3/5/8 bit dynamic number compression scheme:
  89. *
  90. * scheme data suffix range bits used
  91. * route 0001 1 4 (number 1 always encoded as 0001)
  92. * 0 000-111 1 0-7 4 (swapped number 0 and 1)
  93. * 1 00000-11111 10 0-32 7 (skip the number 1)
  94. * 2 00000000-11111111 00 0-256 10 (skip the number 1)
  95. */
  96. # define NumberCompress_v3x5x8_INTERFACES 257
  97. static inline struct EncodingScheme* NumberCompress_v3x5x8_defineScheme(struct Allocator* alloc)
  98. {
  99. return EncodingScheme_defineDynWidthScheme(
  100. ((struct EncodingScheme_Form[3]) {
  101. { .bitCount = 3, .prefixLen = 1, .prefix = 1, },
  102. { .bitCount = 5, .prefixLen = 2, .prefix = 1<<1, },
  103. { .bitCount = 8, .prefixLen = 2, .prefix = 0, }
  104. }),
  105. 3,
  106. alloc);
  107. }
  108. static inline uint32_t NumberCompress_v3x5x8_bitsUsedForLabel(const uint64_t label)
  109. {
  110. if (0 != (label & 0x1)) {
  111. return 4;
  112. }
  113. if (0 != (label & 0x2)) {
  114. return 7;
  115. }
  116. return 10;
  117. }
  118. static inline uint32_t NumberCompress_v3x5x8_bitsUsedForNumber(const uint32_t number)
  119. {
  120. if (number < 8) {
  121. return 4;
  122. } else if (number < 33) {
  123. return 7;
  124. } else {
  125. return 10;
  126. }
  127. }
  128. static inline uint64_t NumberCompress_v3x5x8_getCompressed(const uint32_t number,
  129. const uint32_t bitsUsed)
  130. {
  131. if (1 == number) {
  132. return 1;
  133. }
  134. switch (bitsUsed) {
  135. case 4:
  136. if (0 == number) {
  137. return 3;
  138. }
  139. return (number << 1) | 1;
  140. case 7:
  141. if (0 == number) {
  142. return 2;
  143. }
  144. // skip the number 1
  145. return ((number-1) << 2) | 2;
  146. case 10:
  147. if (0 == number) {
  148. return 0;
  149. }
  150. // skip the number 1
  151. return ((number-1) << 2);
  152. default: return 0;
  153. }
  154. }
  155. static inline uint32_t NumberCompress_v3x5x8_getDecompressed(const uint64_t label,
  156. const uint32_t bitsUsed)
  157. {
  158. uint32_t number;
  159. switch (bitsUsed) {
  160. case 4:
  161. number = (label >> 1) & 0x7u;
  162. if (0 == number) {
  163. return 1;
  164. }
  165. if (1 == number) {
  166. return 0;
  167. }
  168. return number;
  169. case 7:
  170. number = (label >> 2) & 0x1fu;
  171. if (0 != number) {
  172. ++number; // skip the number 1
  173. }
  174. return number;
  175. case 10:
  176. number = (label >> 2) & 0xffu;
  177. if (0 != number) {
  178. ++number; // skip the number 1
  179. }
  180. return number;
  181. default: return 0;
  182. }
  183. }
  184. /*
  185. * 4/8 Dynamic number compression scheme:
  186. *
  187. * scheme data suffix range bits used
  188. * 0 0000-1111 1 0-15 5 (00001 indicates loopback route)
  189. * 1 00000000-11111111 0 0-255 9
  190. */
  191. # define NumberCompress_v4x8_INTERFACES 256
  192. static inline struct EncodingScheme* NumberCompress_v4x8_defineScheme(struct Allocator* alloc)
  193. {
  194. return EncodingScheme_defineDynWidthScheme(
  195. ((struct EncodingScheme_Form[]) {
  196. { .bitCount = 4, .prefixLen = 1, .prefix = 1, },
  197. { .bitCount = 8, .prefixLen = 1, .prefix = 0, }
  198. }),
  199. 2,
  200. alloc);
  201. }
  202. static inline uint32_t NumberCompress_v4x8_getDecompressed(const uint64_t label,
  203. const uint32_t bitsUsed)
  204. {
  205. switch (bitsUsed) {
  206. case 5: {
  207. return ((label >> 1) & 0xfu);
  208. }
  209. case 9: {
  210. return ((label >> 1) & 0xffu);
  211. }
  212. default: Assert_ifTesting(0);
  213. }
  214. return 0;
  215. }
  216. static inline uint64_t NumberCompress_v4x8_getCompressed(const uint32_t number,
  217. const uint32_t bitsUsed)
  218. {
  219. switch (bitsUsed) {
  220. case 5: {
  221. Assert_ifTesting(number < 16);
  222. return (number << 1) | 1;
  223. }
  224. case 9: {
  225. Assert_ifTesting(number < 256);
  226. return (number << 1);
  227. }
  228. default: Assert_ifTesting(0);
  229. }
  230. return 0;
  231. }
  232. static inline uint32_t NumberCompress_v4x8_bitsUsedForLabel(const uint64_t label)
  233. {
  234. return (label & 1) ? 5 : 9;
  235. }
  236. static inline uint32_t NumberCompress_v4x8_bitsUsedForNumber(const uint32_t number)
  237. {
  238. Assert_ifTesting(number < 256);
  239. return (number < 16) ? 5 : 9;
  240. }
  241. /*
  242. * 3/6/10 Dynamic number compression scheme:
  243. * This is a v21 new-world scheme, it has no slot for the router.
  244. *
  245. * scheme data suffix range bits used
  246. * 0 000-111 1 0-8 4
  247. * 1 000000-111111 10 0-64 8
  248. * 2 0000000000-1111111111 00 0-1024 12
  249. */
  250. # define NumberCompress_v3x6x10_INTERFACES 1024
  251. static inline struct EncodingScheme* NumberCompress_v3x6x10_defineScheme(struct Allocator* alloc)
  252. {
  253. return EncodingScheme_defineDynWidthScheme(
  254. ((struct EncodingScheme_Form[]) {
  255. { .bitCount = 3, .prefixLen = 1, .prefix = 1, },
  256. { .bitCount = 6, .prefixLen = 2, .prefix = 1<<1, },
  257. { .bitCount = 10, .prefixLen = 2, .prefix = 0, },
  258. }),
  259. 3,
  260. alloc);
  261. }
  262. static inline uint32_t NumberCompress_v3x6x10_getDecompressed(const uint64_t label,
  263. const uint32_t bitsUsed)
  264. {
  265. switch (bitsUsed) {
  266. case 4: return ( (label >> 1) & Constant_base2(111) );
  267. case 8: return ( (label >> 2) & Constant_base2(111111) );
  268. case 12: return ( (label >> 2) & Constant_base2(1111111111) );
  269. default: Assert_failure("bad bitsUsed");
  270. }
  271. }
  272. static inline uint64_t NumberCompress_v3x6x10_getCompressed(const uint32_t number,
  273. const uint32_t bitsUsed)
  274. {
  275. switch (bitsUsed) {
  276. case 4: {
  277. Assert_true(number < 8);
  278. return (number << 1) | 1;
  279. }
  280. case 8: {
  281. Assert_true(number < 64);
  282. return (number << 2) | (1<<1);
  283. }
  284. case 12: {
  285. Assert_true(number < 1024);
  286. return (number << 2);
  287. }
  288. default: Assert_failure("bad bitsUsed");
  289. }
  290. }
  291. static inline uint32_t NumberCompress_v3x6x10_bitsUsedForLabel(const uint64_t label)
  292. {
  293. if (label & 1) { return 4; }
  294. if (label & 3) { return 8; }
  295. return 12;
  296. }
  297. static inline uint32_t NumberCompress_v3x6x10_bitsUsedForNumber(const uint32_t number)
  298. {
  299. if (number < 8) { return 4; }
  300. if (number < 64) { return 8; }
  301. Assert_true(number < 1024);
  302. return 12;
  303. }
  304. #if !defined(NumberCompress_TYPE)
  305. #define NumberCompress_TYPE v3x6x10
  306. #elif NumberCompress_TYPE == v3x5x8 && Version_CURRENT_PROTOCOL >= 21
  307. #error v358 is nolonger allowed in protocol versions over 21
  308. #endif
  309. #define NumberCompress_MKNAME(x) NumberCompress__MKNAME(NumberCompress_TYPE, x)
  310. #define NumberCompress__MKNAME(y, x) NumberCompress___MKNAME(y, x)
  311. #define NumberCompress___MKNAME(y, x) NumberCompress_ ## y ## _ ## x
  312. #define NumberCompress_INTERFACES NumberCompress_MKNAME(INTERFACES)
  313. #define NumberCompress_defineScheme(a) NumberCompress_MKNAME(defineScheme)(a)
  314. #define NumberCompress_bitsUsedForLabel(label) NumberCompress_MKNAME(bitsUsedForLabel)(label)
  315. #define NumberCompress_bitsUsedForNumber(number) NumberCompress_MKNAME(bitsUsedForNumber)(number)
  316. #define NumberCompress_getCompressed(a, b) NumberCompress_MKNAME(getCompressed)(a, b)
  317. #define NumberCompress_getDecompressed(a, b) NumberCompress_MKNAME(getDecompressed)(a, b)
  318. // conveinence macro
  319. #define NumberCompress_decompress(label) \
  320. NumberCompress_getDecompressed(label, NumberCompress_bitsUsedForLabel(label))
  321. /**
  322. * Get the label for a particular destination from a given source.
  323. * This needs to be called before handing out a label because if a source interface is
  324. * represented using more bits than the destination interface, the destination interface
  325. * must be padded out so that the switch will find the source and destination labels compatable.
  326. *
  327. * @param target the label for the location to send to in host byte order.
  328. * @param whoIsAsking the label for the node which we are sending the target to in host byte order.
  329. * @return the modified target for that node in host byte order.
  330. */
  331. static inline uint64_t NumberCompress_getLabelFor(uint64_t target, uint64_t whoIsAsking)
  332. {
  333. uint32_t targetBits = NumberCompress_bitsUsedForLabel(target);
  334. uint32_t whoIsAskingBits = NumberCompress_bitsUsedForLabel(whoIsAsking);
  335. if (targetBits >= whoIsAskingBits) {
  336. return target;
  337. }
  338. uint32_t targetIfaceNum = NumberCompress_getDecompressed(target, targetBits);
  339. return ((target & (UINT64_MAX << targetBits)) << (whoIsAskingBits - targetBits))
  340. | NumberCompress_getCompressed(targetIfaceNum, whoIsAskingBits);
  341. }
  342. #endif