1
0

LabelSplicer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 LabelSplicer_H
  16. #define LabelSplicer_H
  17. #include "switch/NumberCompress.h"
  18. #include "util/Bits.h"
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. /**
  22. * Splice a label and a label fragment together.
  23. *
  24. */
  25. static inline uint64_t LabelSplicer_splice(uint64_t goHere, uint64_t viaHere)
  26. {
  27. uint64_t log2ViaHere = Bits_log2x64(viaHere);
  28. if (Bits_log2x64(goHere) + log2ViaHere > 59) {
  29. // Too big, can't splice.
  30. return UINT64_MAX;
  31. }
  32. return ((goHere ^ 1) << log2ViaHere) ^ viaHere;
  33. }
  34. /**
  35. * Get the label for a particular destination from a given source.
  36. * This needs to be called before handing out a label because if a source interface is
  37. * represented using more bits than the destination interface, the destination interface
  38. * must be padded out so that the switch will find the source and destination labels compatable.
  39. *
  40. * @param target the label for the location to send to in host byte order.
  41. * @param whoIsAsking the label for the node which we are sending the target to in host byte order.
  42. * @return the modified target for that node in host byte order.
  43. */
  44. static inline uint64_t LabelSplicer_getLabelFor(uint64_t target, uint64_t whoIsAsking)
  45. {
  46. uint32_t targetBits = NumberCompress_bitsUsedForLabel(target);
  47. uint32_t whoIsAskingBits = NumberCompress_bitsUsedForLabel(whoIsAsking);
  48. if (targetBits >= whoIsAskingBits) {
  49. return target;
  50. }
  51. uint32_t targetIfaceNum = NumberCompress_getDecompressed(target, targetBits);
  52. return ((target & (UINT64_MAX << targetBits)) << (whoIsAskingBits - targetBits))
  53. | NumberCompress_getCompressed(targetIfaceNum, whoIsAskingBits);
  54. }
  55. /**
  56. * Determine if the node at the end of the given label is one hop away.
  57. *
  58. * @param label the label to test in host byte order.
  59. * @return true if the node is 1 hop away, false otherwise.
  60. */
  61. static inline bool LabelSplicer_isOneHop(uint64_t label)
  62. {
  63. return (int)NumberCompress_bitsUsedForLabel(label) == Bits_log2x64(label);
  64. }
  65. /**
  66. * Determine if the route to one node passes through another node.
  67. * Given:
  68. * 1. alice->bob->charlie->fred->bob
  69. * 2. alice->bob
  70. * 1 routes through 2.
  71. *
  72. * @param destination the node to route to.
  73. * @param midPath the node which might be in the middle of the route.
  74. * @return true if midPath is in the middle of the route to destination.
  75. */
  76. static inline bool LabelSplicer_routesThrough(uint64_t destination, uint64_t midPath)
  77. {
  78. if (midPath > destination) {
  79. return false;
  80. } if (midPath < 2) {
  81. return true;
  82. }
  83. uint64_t mask = UINT64_MAX >> (64 - Bits_log2x64(midPath));
  84. return (destination & mask) == (midPath & mask);
  85. }
  86. /**
  87. * Convert a full path (represention we can use) to a representaiton which a node
  88. * along that path can use.
  89. *
  90. * @param fullPath the full route to the destination
  91. * @param midPath a path to a node which falls somewhere within fullPath
  92. * @return a version of fullPath which is sutible for use by node at midPath
  93. */
  94. static inline uint64_t LabelSplicer_unsplice(uint64_t fullPath, uint64_t midPath)
  95. {
  96. Assert_true(LabelSplicer_routesThrough(fullPath, midPath));
  97. return fullPath >> Bits_log2x64(midPath);
  98. }
  99. #endif