2
0

host-utils.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <stdlib.h>
  26. #include <stdint.h>
  27. #include "host-utils.h"
  28. //#define DEBUG_MULDIV
  29. /* Long integer helpers */
  30. #if !defined(__x86_64__)
  31. static void add128 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
  32. {
  33. *plow += a;
  34. /* carry test */
  35. if (*plow < a)
  36. (*phigh)++;
  37. *phigh += b;
  38. }
  39. static void neg128 (uint64_t *plow, uint64_t *phigh)
  40. {
  41. *plow = ~*plow;
  42. *phigh = ~*phigh;
  43. add128(plow, phigh, 1, 0);
  44. }
  45. static void mul64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
  46. {
  47. uint32_t a0, a1, b0, b1;
  48. uint64_t v;
  49. a0 = a;
  50. a1 = a >> 32;
  51. b0 = b;
  52. b1 = b >> 32;
  53. v = (uint64_t)a0 * (uint64_t)b0;
  54. *plow = v;
  55. *phigh = 0;
  56. v = (uint64_t)a0 * (uint64_t)b1;
  57. add128(plow, phigh, v << 32, v >> 32);
  58. v = (uint64_t)a1 * (uint64_t)b0;
  59. add128(plow, phigh, v << 32, v >> 32);
  60. v = (uint64_t)a1 * (uint64_t)b1;
  61. *phigh += v;
  62. }
  63. /* Unsigned 64x64 -> 128 multiplication */
  64. void mulu64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
  65. {
  66. mul64(plow, phigh, a, b);
  67. #if defined(DEBUG_MULDIV)
  68. printf("mulu64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
  69. a, b, *phigh, *plow);
  70. #endif
  71. }
  72. /* Signed 64x64 -> 128 multiplication */
  73. void muls64 (uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
  74. {
  75. int sa, sb;
  76. sa = (a < 0);
  77. if (sa)
  78. a = -a;
  79. sb = (b < 0);
  80. if (sb)
  81. b = -b;
  82. mul64(plow, phigh, a, b);
  83. if (sa ^ sb) {
  84. neg128(plow, phigh);
  85. }
  86. #if defined(DEBUG_MULDIV)
  87. printf("muls64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
  88. a, b, *phigh, *plow);
  89. #endif
  90. }
  91. #endif /* !defined(__x86_64__) */