NumberCompress.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #ifndef NumberCompress_TYPE
  21. #define NumberCompress_TYPE v3x5x8
  22. #endif
  23. #include "switch/EncodingScheme.h"
  24. #include "util/Bits.h"
  25. #include <stdint.h>
  26. /* right now 4 implementations:
  27. * - fixed 4 bits: 14 peers + self
  28. * - fixed 8 bits: 254 peers + self
  29. * - dynamically 4-8 bits: 256 peers
  30. * - dynamically 3-5-8 bits: 256 peers
  31. */
  32. /* implementations
  33. *
  34. * they are all accesible with their internal names for unit testing
  35. */
  36. /**********************
  37. * Fixed 4 bit scheme: 15 peers + 1 router
  38. **********************/
  39. # define NumberCompress_f4_INTERFACES 16
  40. static inline struct EncodingScheme* NumberCompress_f4_defineScheme(struct Allocator* alloc)
  41. {
  42. return EncodingScheme_defineFixedWidthScheme(4, alloc);
  43. }
  44. static inline uint32_t NumberCompress_f4_bitsUsedForLabel(const uint64_t label)
  45. {
  46. return 4;
  47. }
  48. static inline uint32_t NumberCompress_f4_bitsUsedForNumber(const uint32_t number)
  49. {
  50. Assert_true(number < 16);
  51. return 4;
  52. }
  53. static inline uint64_t NumberCompress_f4_getCompressed(const uint32_t number,
  54. const uint32_t bitsUsed)
  55. {
  56. Assert_true(number < 16);
  57. return number;
  58. }
  59. static inline uint32_t NumberCompress_f4_getDecompressed(const uint64_t label,
  60. const uint32_t bitsUsed)
  61. {
  62. return label & 0xf;
  63. }
  64. # define NumberCompress_f8_INTERFACES 256
  65. static inline struct EncodingScheme* NumberCompress_f8_defineScheme(struct Allocator* alloc)
  66. {
  67. return EncodingScheme_defineFixedWidthScheme(8, alloc);
  68. }
  69. static inline uint32_t NumberCompress_f8_bitsUsedForLabel(const uint64_t label)
  70. {
  71. return 8;
  72. }
  73. static inline uint32_t NumberCompress_f8_bitsUsedForNumber(const uint32_t number)
  74. {
  75. Assert_true(number < 256);
  76. return 8;
  77. }
  78. static inline uint64_t NumberCompress_f8_getCompressed(const uint32_t number,
  79. const uint32_t bitsUsed)
  80. {
  81. Assert_true(number < 256);
  82. return number;
  83. }
  84. static inline uint32_t NumberCompress_f8_getDecompressed(const uint64_t label,
  85. const uint32_t bitsUsed)
  86. {
  87. return label & 0xff;
  88. }
  89. /*
  90. * 3/5/8 bit dynamic number compression scheme:
  91. *
  92. * scheme data suffix range bits used
  93. * route 0001 1 4 (number 1 always encoded as 0001)
  94. * 0 000-111 1 0-7 4 (swapped number 0 and 1)
  95. * 1 00000-11111 10 0-32 7 (skip the number 1)
  96. * 2 00000000-11111111 00 0-256 10 (skip the number 1)
  97. */
  98. # define NumberCompress_v3x5x8_INTERFACES 257
  99. static inline struct EncodingScheme* NumberCompress_v3x5x8_defineScheme(struct Allocator* alloc)
  100. {
  101. return EncodingScheme_defineDynWidthScheme(
  102. ((struct EncodingScheme_Form[3]) {
  103. { .bitCount = 3, .prefixLen = 1, .prefix = 1, },
  104. { .bitCount = 5, .prefixLen = 2, .prefix = 1<<1, },
  105. { .bitCount = 8, .prefixLen = 2, .prefix = 0, }
  106. }),
  107. 3,
  108. alloc);
  109. }
  110. static inline uint32_t NumberCompress_v3x5x8_bitsUsedForLabel(const uint64_t label)
  111. {
  112. if (0 != (label & 0x1)) {
  113. return 4;
  114. }
  115. if (0 != (label & 0x2)) {
  116. return 7;
  117. }
  118. return 10;
  119. }
  120. static inline uint32_t NumberCompress_v3x5x8_bitsUsedForNumber(const uint32_t number)
  121. {
  122. if (number < 8) {
  123. return 4;
  124. } else if (number < 33) {
  125. return 7;
  126. } else {
  127. return 10;
  128. }
  129. }
  130. static inline uint64_t NumberCompress_v3x5x8_getCompressed(const uint32_t number,
  131. const uint32_t bitsUsed)
  132. {
  133. if (1 == number) {
  134. return 1;
  135. }
  136. switch (bitsUsed) {
  137. case 4:
  138. if (0 == number) {
  139. return 3;
  140. }
  141. return (number << 1) | 1;
  142. case 7:
  143. if (0 == number) {
  144. return 2;
  145. }
  146. // skip the number 1
  147. return ((number-1) << 2) | 2;
  148. case 10:
  149. if (0 == number) {
  150. return 0;
  151. }
  152. // skip the number 1
  153. return ((number-1) << 2);
  154. default: return 0;
  155. }
  156. }
  157. static inline uint32_t NumberCompress_v3x5x8_getDecompressed(const uint64_t label,
  158. const uint32_t bitsUsed)
  159. {
  160. uint32_t number;
  161. switch (bitsUsed) {
  162. case 4:
  163. number = (label >> 1) & 0x7u;
  164. if (0 == number) {
  165. return 1;
  166. }
  167. if (1 == number) {
  168. return 0;
  169. }
  170. return number;
  171. case 7:
  172. number = (label >> 2) & 0x1fu;
  173. if (0 != number) {
  174. ++number; // skip the number 1
  175. }
  176. return number;
  177. case 10:
  178. number = (label >> 2) & 0xffu;
  179. if (0 != number) {
  180. ++number; // skip the number 1
  181. }
  182. return number;
  183. default: return 0;
  184. }
  185. }
  186. /*
  187. * 4/8 Dynamic number compression scheme:
  188. *
  189. * scheme data suffix range bits used
  190. * 0 0000-1111 1 0-15 5 (00001 indicates loopback route)
  191. * 1 00000000-11111111 0 0-255 9
  192. */
  193. # define NumberCompress_v4x8_INTERFACES 256
  194. static inline struct EncodingScheme* NumberCompress_v4x8_defineScheme(struct Allocator* alloc)
  195. {
  196. return EncodingScheme_defineDynWidthScheme(
  197. ((struct EncodingScheme_Form[]) {
  198. { .bitCount = 4, .prefixLen = 1, .prefix = 1, },
  199. { .bitCount = 8, .prefixLen = 1, .prefix = 0, }
  200. }),
  201. 2,
  202. alloc);
  203. }
  204. static inline uint32_t NumberCompress_v4x8_getDecompressed(const uint64_t label,
  205. const uint32_t bitsUsed)
  206. {
  207. if ((label & 0x1f) == 1) { return 1; }
  208. switch (bitsUsed) {
  209. case 5: {
  210. return ((label >> 1) & 0xfu) ^ 1;
  211. }
  212. case 9: {
  213. return ((label >> 1) & 0xffu) ^ 1;
  214. }
  215. default: Assert_ifTesting(0);
  216. }
  217. return 0;
  218. }
  219. static inline uint64_t NumberCompress_v4x8_getCompressed(uint32_t number,
  220. const uint32_t bitsUsed)
  221. {
  222. if (1 == number) { return 1; }
  223. switch (bitsUsed) {
  224. case 5: {
  225. Assert_ifTesting(number < 16);
  226. return (number << 1) ^ 3;
  227. }
  228. case 9: {
  229. Assert_ifTesting(number < 256);
  230. return (number << 1) ^ 2;
  231. }
  232. default: Assert_ifTesting(0);
  233. }
  234. return 0;
  235. }
  236. static inline uint32_t NumberCompress_v4x8_bitsUsedForLabel(const uint64_t label)
  237. {
  238. return (label & 1) ? 5 : 9;
  239. }
  240. static inline uint32_t NumberCompress_v4x8_bitsUsedForNumber(const uint32_t number)
  241. {
  242. Assert_ifTesting(number < 256);
  243. return (number < 16) ? 5 : 9;
  244. }
  245. #define NumberCompress_MKNAME(x) NumberCompress__MKNAME(NumberCompress_TYPE, x)
  246. #define NumberCompress__MKNAME(y, x) NumberCompress___MKNAME(y, x)
  247. #define NumberCompress___MKNAME(y, x) NumberCompress_ ## y ## _ ## x
  248. #define NumberCompress_INTERFACES NumberCompress_MKNAME(INTERFACES)
  249. #define NumberCompress_defineScheme(a) NumberCompress_MKNAME(defineScheme)(a)
  250. #define NumberCompress_bitsUsedForLabel(label) NumberCompress_MKNAME(bitsUsedForLabel)(label)
  251. #define NumberCompress_bitsUsedForNumber(number) NumberCompress_MKNAME(bitsUsedForNumber)(number)
  252. #define NumberCompress_getCompressed(a, b) NumberCompress_MKNAME(getCompressed)(a, b)
  253. #define NumberCompress_getDecompressed(a, b) NumberCompress_MKNAME(getDecompressed)(a, b)
  254. // conveinence macro
  255. #define NumberCompress_decompress(label) \
  256. NumberCompress_getDecompressed(label, NumberCompress_bitsUsedForLabel(label))
  257. /**
  258. * Get the label for a particular destination from a given source.
  259. * This needs to be called before handing out a label because if a source interface is
  260. * represented using more bits than the destination interface, the destination interface
  261. * must be padded out so that the switch will find the source and destination labels compatable.
  262. *
  263. * @param target the label for the location to send to in host byte order.
  264. * @param whoIsAsking the label for the node which we are sending the target to in host byte order.
  265. * @return the modified target for that node in host byte order.
  266. */
  267. static inline uint64_t NumberCompress_getLabelFor(uint64_t target, uint64_t whoIsAsking)
  268. {
  269. uint32_t targetBits = NumberCompress_bitsUsedForLabel(target);
  270. uint32_t whoIsAskingBits = NumberCompress_bitsUsedForLabel(whoIsAsking);
  271. if (targetBits >= whoIsAskingBits) {
  272. return target;
  273. }
  274. uint32_t targetIfaceNum = NumberCompress_getDecompressed(target, targetBits);
  275. return ((target & (UINT64_MAX << targetBits)) << (whoIsAskingBits - targetBits))
  276. | NumberCompress_getCompressed(targetIfaceNum, whoIsAskingBits);
  277. }
  278. #endif