host-utils.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Utility compute operations used by translated code.
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. * Copyright (c) 2007 Aurelien Jarno
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "exec.h"
  26. #include "host-utils.h"
  27. //#define DEBUG_MULDIV
  28. /* Long integer helpers */
  29. #if !defined(__x86_64__)
  30. static void add128 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
  31. {
  32. *plow += a;
  33. /* carry test */
  34. if (*plow < a)
  35. (*phigh)++;
  36. *phigh += b;
  37. }
  38. static void neg128 (uint64_t *plow, uint64_t *phigh)
  39. {
  40. *plow = ~*plow;
  41. *phigh = ~*phigh;
  42. add128(plow, phigh, 1, 0);
  43. }
  44. static void mul64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
  45. {
  46. uint32_t a0, a1, b0, b1;
  47. uint64_t v;
  48. a0 = a;
  49. a1 = a >> 32;
  50. b0 = b;
  51. b1 = b >> 32;
  52. v = (uint64_t)a0 * (uint64_t)b0;
  53. *plow = v;
  54. *phigh = 0;
  55. v = (uint64_t)a0 * (uint64_t)b1;
  56. add128(plow, phigh, v << 32, v >> 32);
  57. v = (uint64_t)a1 * (uint64_t)b0;
  58. add128(plow, phigh, v << 32, v >> 32);
  59. v = (uint64_t)a1 * (uint64_t)b1;
  60. *phigh += v;
  61. }
  62. /* Unsigned 64x64 -> 128 multiplication */
  63. void mulu64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
  64. {
  65. mul64(plow, phigh, a, b);
  66. #if defined(DEBUG_MULDIV)
  67. printf("mulu64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
  68. a, b, *phigh, *plow);
  69. #endif
  70. }
  71. /* Signed 64x64 -> 128 multiplication */
  72. void muls64 (uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
  73. {
  74. int sa, sb;
  75. sa = (a < 0);
  76. if (sa)
  77. a = -a;
  78. sb = (b < 0);
  79. if (sb)
  80. b = -b;
  81. mul64(plow, phigh, a, b);
  82. if (sa ^ sb) {
  83. neg128(plow, phigh);
  84. }
  85. #if defined(DEBUG_MULDIV)
  86. printf("muls64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
  87. a, b, *phigh, *plow);
  88. #endif
  89. }
  90. #endif /* !defined(__x86_64__) */