1
0

PenaltyFloat.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 PenaltyFloat_H
  16. #define PenaltyFloat_H
  17. #include "util/Bits.h"
  18. /**
  19. * 1 2
  20. * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0
  21. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  22. * 0 | Exponent | Mantissa |
  23. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  24. */
  25. #define PenaltyFloat_MAX 0xdbff
  26. static inline uint64_t PenaltyFloat_unpack(uint16_t penalty)
  27. {
  28. uint64_t out = penalty & ((1<<10)-1);
  29. out <<= (penalty >> 10);
  30. return out;
  31. }
  32. /**
  33. * Pack the penalty value for transport on the wire.
  34. */
  35. static inline uint16_t PenaltyFloat_pack(uint64_t penalty)
  36. {
  37. int shift = Bits_log2x64(penalty) - 9;
  38. if (shift <= 0) { return penalty; }
  39. return (shift << 10) | (penalty >> shift);
  40. }
  41. #endif