zend_multiply.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Sascha Schumann <sascha@schumann.cx> |
  16. | Ard Biesheuvel <ard.biesheuvel@linaro.org> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #if defined(__i386__) && defined(__GNUC__)
  21. #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \
  22. long __tmpvar; \
  23. __asm__ ("imul %3,%0\n" \
  24. "adc $0,%1" \
  25. : "=r"(__tmpvar),"=r"(usedval) \
  26. : "0"(a), "r"(b), "1"(0)); \
  27. if (usedval) (dval) = (double) (a) * (double) (b); \
  28. else (lval) = __tmpvar; \
  29. } while (0)
  30. #elif defined(__x86_64__) && defined(__GNUC__)
  31. #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \
  32. long __tmpvar; \
  33. __asm__ ("imul %3,%0\n" \
  34. "adc $0,%1" \
  35. : "=r"(__tmpvar),"=r"(usedval) \
  36. : "0"(a), "r"(b), "1"(0)); \
  37. if (usedval) (dval) = (double) (a) * (double) (b); \
  38. else (lval) = __tmpvar; \
  39. } while (0)
  40. #elif defined(__arm__) && defined(__GNUC__)
  41. #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \
  42. long __tmpvar; \
  43. __asm__("smull %0, %1, %2, %3\n" \
  44. "sub %1, %1, %0, asr #31" \
  45. : "=r"(__tmpvar), "=r"(usedval) \
  46. : "r"(a), "r"(b)); \
  47. if (usedval) (dval) = (double) (a) * (double) (b); \
  48. else (lval) = __tmpvar; \
  49. } while (0)
  50. #elif defined(__aarch64__) && defined(__GNUC__)
  51. #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \
  52. long __tmpvar; \
  53. __asm__("mul %0, %2, %3\n" \
  54. "smulh %1, %2, %3\n" \
  55. "sub %1, %1, %0, asr #63\n" \
  56. : "=X"(__tmpvar), "=X"(usedval) \
  57. : "X"(a), "X"(b)); \
  58. if (usedval) (dval) = (double) (a) * (double) (b); \
  59. else (lval) = __tmpvar; \
  60. } while (0)
  61. #elif SIZEOF_LONG == 4 && defined(HAVE_ZEND_LONG64)
  62. #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \
  63. zend_long64 __result = (zend_long64) (a) * (zend_long64) (b); \
  64. if (__result > LONG_MAX || __result < LONG_MIN) { \
  65. (dval) = (double) __result; \
  66. (usedval) = 1; \
  67. } else { \
  68. (lval) = (long) __result; \
  69. (usedval) = 0; \
  70. } \
  71. } while (0)
  72. #else
  73. #define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \
  74. long __lres = (a) * (b); \
  75. long double __dres = (long double)(a) * (long double)(b); \
  76. long double __delta = (long double) __lres - __dres; \
  77. if ( ((usedval) = (( __dres + __delta ) != __dres))) { \
  78. (dval) = __dres; \
  79. } else { \
  80. (lval) = __lres; \
  81. } \
  82. } while (0)
  83. #endif