Skip to contentSkip to conversation

x86_64: optimise muldiv64 for x86_64 architecture

PATCH In review Frediano Ziglio 2 participants 2 messages Jan 9, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86_64: optimise muldiv64 for x86_64 architecture

As this platform can do multiply/divide using 128 bit precision use these instruction to implement it.

---
 include/qemu-common.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/qemu-common.h +13 -0
index f862214..5366220 100644
@@ -370,6 +370,7 @@ static inline uint8_t from_bcd(uint8_t val)
 }
 
 /* compute with 96 bit intermediate result: (a*b)/c */
+#ifndef __x86_64__
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     union {
@@ -392,6 +393,18 @@ static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
     res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
     return res.ll;
 }
+#else
+static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
+{
+    uint64_t res;
+
+    asm ("mulq %2\n\tdivq %3"
+         : "=a"(res)
+         : "a"(a), "qm"((uint64_t) b), "qm"((uint64_t)c)
+         : "rdx", "cc");
+    return res;
+}
+#endif
 
 /* Round number down to multiple */
 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
Signature · 1 line
1.9.1
#1 lore raw
Quoted 26 lines · Frediano Ziglio

As this platform can do multiply/divide using 128 bit precision use these instruction to implement it.

---
 include/qemu-common.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/qemu-common.h +3 -0
index f862214..5366220 100644
@@ -370,6 +370,7 @@ static inline uint8_t from_bcd(uint8_t val)
 }
 
 /* compute with 96 bit intermediate result: (a*b)/c */
+#ifndef __x86_64__
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     union {
@@ -392,6 +393,18 @@ static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
     res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
     return res.ll;
 }
+#else
+static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)

include/qemu-common.h static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) · line 407

Quoted 10 lines
include/qemu-common.h +10 -0
+{
+    uint64_t res;
+
+    asm ("mulq %2\n\tdivq %3"
+         : "=a"(res)
+         : "a"(a), "qm"((uint64_t) b), "qm"((uint64_t)c)
+         : "rdx", "cc");
+    return res;
+}
+#endif

Honestly, this ought to move into qemu/host-utils.h, and it should use __int128 for targets that support it. Which includes x86_64, but also other 64-bit hosts.

r~