Skip to contentSkip to conversation

qemu-common.h: optimise muldiv64 if int128 is available

PATCH In review Frediano Ziglio 3 participants 11 messages Jan 9, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 qemu-common.h: optimise muldiv64 if int128 is available

Let compiler do the job to optimise the function.

---
 include/qemu-common.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/qemu-common.h +7 -0
index f862214..f3033ae 100644
@@ -370,6 +370,12 @@ static inline uint8_t from_bcd(uint8_t val)
 }
 
 /* compute with 96 bit intermediate result: (a*b)/c */
+#ifdef CONFIG_INT128
+static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
+{
+    return (__int128)a * b / c;
+}
+#else
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     union {
@@ -392,6 +398,7 @@ 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;
 }
+#endif
 
 /* Round number down to multiple */
 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
Signature · 1 line
1.9.1
#1 lore raw

PATCH 2/2 qemu-common.h: optimise muldiv64 for x86_64 architecture

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

---
 include/qemu-common.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
include/qemu-common.h +13 -1
index f3033ae..880659d 100644
@@ -370,11 +370,23 @@ static inline uint8_t from_bcd(uint8_t val)
 }
 
 /* compute with 96 bit intermediate result: (a*b)/c */
-#ifdef CONFIG_INT128
+#if defined(CONFIG_INT128) && !defined(__x86_64__)
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     return (__int128)a * b / c;
 }
+#elif defined(__x86_64__)
+/* Optimised x64 version. This assume that a*b/c fits in 64 bit */
+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;
+}
 #else
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
Signature · 1 line
1.9.1
#2 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-common.h: optimise muldiv64 for x86_64 architecture

Quoted 9 lines · Frediano Ziglio

/* compute with 96 bit intermediate result: (a*b)/c */

include/qemu-common.h +3 -1
-#ifdef CONFIG_INT128
+#if defined(CONFIG_INT128) && !defined(__x86_64__)
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     return (__int128)a * b / c;
 }
+#elif defined(__x86_64__)
+/* Optimised x64 version. This assume that a*b/c fits in 64 bit */

include/qemu-common.h static inline uint8_t from_bcd(uint8_t val) · line 389

Quoted 10 lines
include/qemu-common.h +10 -0
+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;
+}

Reorder it the other way, and you can simplify the first #if.

I applied patch 1 locally, and will send a pull request once the tree is thawed.

Paolo

#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-common.h: optimise muldiv64 for x86_64 architecture

2015-01-09 11:43 GMT+00:00 Paolo Bonzini <[email protected]>:

Quoted 16 lines · Frediano Ziglio
Quoted 13 lines

/* compute with 96 bit intermediate result: (a*b)/c */

include/qemu-common.h +7 -1
-#ifdef CONFIG_INT128
+#if defined(CONFIG_INT128) && !defined(__x86_64__)
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     return (__int128)a * b / c;
 }
+#elif defined(__x86_64__)
+/* Optimised x64 version. This assume that a*b/c fits in 64 bit */
+static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
+{
+    uint64_t res;
+
Quoted 9 lines

include/qemu-common.h static inline uint8_t from_bcd(uint8_t val) · line 389

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

Reorder it the other way, and you can simplify the first #if.

Yes, I was however thinking about people reading code. The int128 version is much easier to read so I put it first.

Quoted 4 lines

I applied patch 1 locally, and will send a pull request once the tree is thawed.

Paolo

Frediano

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-common.h: optimise muldiv64 for x86_64 architecture

In reply to Frediano Ziglio
Quoted 14 lines · Frediano Ziglio

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

---
 include/qemu-common.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
include/qemu-common.h +0 -0
index f3033ae..880659d 100644
@@ -370,11 +370,23 @@ static inline uint8_t from_bcd(uint8_t val)
 }

include/qemu-common.h static inline uint8_t from_bcd(uint8_t val) · line 379

Quoted 10 lines
include/qemu-common.h +3 -1
 
 /* compute with 96 bit intermediate result: (a*b)/c */
-#ifdef CONFIG_INT128
+#if defined(CONFIG_INT128) && !defined(__x86_64__)
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     return (__int128)a * b / c;
 }
+#elif defined(__x86_64__)
+/* Optimised x64 version. This assume that a*b/c fits in 64 bit */

This assumption isn't necessarily true, and this implementation will dump core on overflow. For instance:

Inputs: a = 8000000000000000 b = 80000000 c = 1
fn muldiv64 result 0
fn muldiv64_with_int128 result 0
fn muldiv64_with_uint128 result 0
Floating point exception (core dumped)

-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-common.h: optimise muldiv64 for x86_64 architecture

2015-01-09 12:22 GMT+00:00 Peter Maydell <[email protected]>:

Quoted 29 lines · Frediano Ziglio
Quoted 14 lines

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

---
 include/qemu-common.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
include/qemu-common.h +0 -0
index f3033ae..880659d 100644
@@ -370,11 +370,23 @@ static inline uint8_t from_bcd(uint8_t val)
 }

include/qemu-common.h static inline uint8_t from_bcd(uint8_t val) · line 379

Quoted 10 lines
include/qemu-common.h +3 -1
 
 /* compute with 96 bit intermediate result: (a*b)/c */
-#ifdef CONFIG_INT128
+#if defined(CONFIG_INT128) && !defined(__x86_64__)
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     return (__int128)a * b / c;
 }
+#elif defined(__x86_64__)
+/* Optimised x64 version. This assume that a*b/c fits in 64 bit */

This assumption isn't necessarily true, and this implementation will dump core on overflow. For instance:

Quoted 7 lines

Inputs: a = 8000000000000000 b = 80000000 c = 1
fn muldiv64 result 0
fn muldiv64_with_int128 result 0
fn muldiv64_with_uint128 result 0
Floating point exception (core dumped)

-- PMM

Yes, I know, it was meant to be a precondition, not a math rule. Doing some grep I'm not really sure this is valid in all cases however I would ask if the truncation is handled correctly. Surely in some cases the call to this function is not needed like in "muldiv64(1, tks, usb_bit_time);" or in "muldiv64((int16_t) s->rtc.comp, 1000, 0x8000);"

Frediano

#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-common.h: optimise muldiv64 for x86_64 architecture

Quoted 11 lines · Frediano Ziglio

2015-01-09 12:22 GMT+00:00 Peter Maydell <[email protected]>:

Quoted 4 lines
Quoted 1 line

+/* Optimised x64 version. This assume that a*b/c fits in 64 bit */

This assumption isn't necessarily true, and this implementation will dump core on overflow.

Yes, I know, it was meant to be a precondition, not a math rule. Doing some grep I'm not really sure this is valid in all cases however I would ask if the truncation is handled correctly. Surely in some cases the call to this function is not needed like in "muldiv64(1, tks, usb_bit_time);" or in "muldiv64((int16_t) s->rtc.comp, 1000, 0x8000);"

Sure, there are some cases where we know the calculation won't overflow, but there are also cases where we can't be so sure, and if the parameters can be controlled by the guest then we absolutely can't allow the guest to cause QEMU to SIGFPE.

Incidentally, in the examples you quote gcc is generally able (because the function is inline) to do a better job because of some of the constants involved: for instance with
"muldiv64((int16_t) s->rtc.comp, 1000, 0x8000);"
it generates a completely inline insn sequence with one mul and no div insns.

So I think at this point I come back to an earlier question:
do you have profiling results showing this function to be a performance problem on a real workload? If not, it just doesn't seem to me to be worth the risk and the maintenance burden to add a native x86 assembly version.

-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-common.h: optimise muldiv64 for x86_64 architecture

2015-01-09 15:52 GMT+00:00 Peter Maydell <[email protected]>:

Quoted 25 lines · Frediano Ziglio
Quoted 11 lines

2015-01-09 12:22 GMT+00:00 Peter Maydell <[email protected]>:

Quoted 4 lines
Quoted 1 line

+/* Optimised x64 version. This assume that a*b/c fits in 64 bit */

This assumption isn't necessarily true, and this implementation will dump core on overflow.

Yes, I know, it was meant to be a precondition, not a math rule. Doing some grep I'm not really sure this is valid in all cases however I would ask if the truncation is handled correctly. Surely in some cases the call to this function is not needed like in "muldiv64(1, tks, usb_bit_time);" or in "muldiv64((int16_t) s->rtc.comp, 1000, 0x8000);"

Sure, there are some cases where we know the calculation won't overflow, but there are also cases where we can't be so sure, and if the parameters can be controlled by the guest then we absolutely can't allow the guest to cause QEMU to SIGFPE.

Incidentally, in the examples you quote gcc is generally able (because the function is inline) to do a better job because of some of the constants involved: for instance with
"muldiv64((int16_t) s->rtc.comp, 1000, 0x8000);"
it generates a completely inline insn sequence with one mul and no div insns.

Quoted 7 lines

So I think at this point I come back to an earlier question:
do you have profiling results showing this function to be a performance problem on a real workload? If not, it just doesn't seem to me to be worth the risk and the maintenance burden to add a native x86 assembly version.

-- PMM

I agree (after some digging) we are not sure we won't get that overflow. Agree to drop the second patch. However I would retain the first. Compiler can use it to optimize much easier. For instance if compiler understand that the multiplication fits into a 64 bit can decide to avoid the 128 bit operation easily, not so easy with all these shift, multiply, division and union structure.

I didn't do any profiling.

Frediano

#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-common.h: optimise muldiv64 for x86_64 architecture

Quoted 6 lines · Frediano Ziglio

I agree (after some digging) we are not sure we won't get that overflow. Agree to drop the second patch. However I would retain the first. Compiler can use it to optimize much easier. For instance if compiler understand that the multiplication fits into a 64 bit can decide to avoid the 128 bit operation easily, not so easy with all these shift, multiply, division and union structure.

Yes, the uint128_t patch is a good idea.

-- PMM

#9 lore raw
In reply to Frediano Ziglio
Quoted 12 lines · Frediano Ziglio

Let compiler do the job to optimise the function.

---
 include/qemu-common.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/qemu-common.h +0 -0
index f862214..f3033ae 100644

include/qemu-common.h static inline uint8_t from_bcd(uint8_t val) · line 376

Quoted 7 lines
include/qemu-common.h +4 -0
@@ -370,6 +370,12 @@ static inline uint8_t from_bcd(uint8_t val)
 }
 
 /* compute with 96 bit intermediate result: (a*b)/c */
+#ifdef CONFIG_INT128
+static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
+{
+    return (__int128)a * b / c;

Wrong type -- this should be __int128_t, which is what we check for in our configure test. (Or __uint128_t I suppose.)

thanks
-- PMM

#10 lore raw

PATCH 1/2 qemu-common.h: optimise muldiv64 if int128 is available

Let compiler do the job to optimise the function.

---
 include/qemu-common.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/qemu-common.h +7 -0
index f862214..f3033ae 100644
@@ -370,6 +370,12 @@ static inline uint8_t from_bcd(uint8_t val)
 }
 
 /* compute with 96 bit intermediate result: (a*b)/c */
+#ifdef CONFIG_INT128
+static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
+{
+    return (__uint128_t)a * b / c;
+}
+#else
 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 {
     union {
@@ -392,6 +398,7 @@ 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;
 }
+#endif
 
 /* Round number down to multiple */
 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
Signature · 1 line
1.9.1