NumberCompress.h 9.6 KB

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