PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/util.cpp

http://github.com/mozy/mordor
C++ | 59 lines | 26 code | 5 blank | 28 comment | 1 complexity | bb1c3c8ebb7ee9339a9aae9d45445bae MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #include "util.h"
  2. #include "endian.h"
  3. namespace Mordor
  4. {
  5. // muldiv64() was adapted from
  6. // http://www.virtualbox.org/browser/trunk/src/VBox/Devices/muldiv64.c?rev=1
  7. //
  8. // the following copyright notice applies:
  9. /*
  10. * QEMU System Emulator
  11. *
  12. * Copyright (c) 2003-2004 Fabrice Bellard
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. * THE SOFTWARE.
  31. */
  32. /* compute with 96 bit intermediate result: (a*b)/c */
  33. uint64_t muldiv64(uint64_t a, uint32_t b, uint64_t c)
  34. {
  35. union {
  36. uint64_t ll;
  37. struct {
  38. #if MORDOR_BYTE_ORDER == MORDOR_BIG_ENDIAN
  39. uint32_t high, low;
  40. #else
  41. uint32_t low, high;
  42. #endif
  43. } l;
  44. } u, res;
  45. uint64_t rl, rh;
  46. u.ll = a;
  47. rl = (uint64_t)u.l.low * (uint64_t)b;
  48. rh = (uint64_t)u.l.high * (uint64_t)b;
  49. rh += (rl >> 32);
  50. res.l.high = (uint32_t)(rh / c);
  51. res.l.low = (uint32_t)((((rh % c) << 32) + (rl & 0xffffffff)) / c);
  52. return res.ll;
  53. }
  54. };