Skip to contentSkip to conversation

include/fpu/softfloat: Fix compilation with Clang on s390x

PATCH In review Thomas Huth 8 participants 23 messages Jan 14, 2019 – Jan 17, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 include/fpu/softfloat: Fix compilation with Clang on s390x

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/fpu/softfloat-macros.h +1 -1
index b1d772e..bd5b641 100644
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)
     /* Need to use a TImode type to get an even register pair for DLGR.  */
     unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
     asm("dlgr %0, %1" : "+r"(n) : "r"(d));
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 22 lines · Thomas Huth

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/fpu/softfloat-macros.h +0 -0
index b1d772e..bd5b641 100644

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 644

Quoted 5 lines
include/fpu/softfloat-macros.h +1 -1
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)

Can we rather check if __int128 is natively supported? So this part get compiled once Clang do support it, else we'll never use it...

Quoted 4 lines
include/fpu/softfloat-macros.h +0 -0
     /* Need to use a TImode type to get an even register pair for DLGR.  */
     unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
     asm("dlgr %0, %1" : "+r"(n) : "r"(d));
#2 lore raw
Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Thomas Huth

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 644

Quoted 10 lines
include/fpu/softfloat-macros.h +1 -1
index b1d772e..bd5b641 100644
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)

Can we rather check if __int128 is natively supported? So this part get compiled once Clang do support it, else we'll never use it...

We already define CONFIG_INT128 so you could just use that.

Thomas does the s390 clang leave CONFIG_INT128=y in config-host.mak?

Quoted 5 lines

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 647

Quoted 5 lines
include/fpu/softfloat-macros.h +0 -0
     /* Need to use a TImode type to get an even register pair for DLGR.  */
     unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
     asm("dlgr %0, %1" : "+r"(n) : "r"(d));

--
Alex Bennée

#3 lore raw
Quoted 35 lines · Alex Bennée
Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Thomas Huth

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 644

Quoted 10 lines
include/fpu/softfloat-macros.h +1 -1
index b1d772e..bd5b641 100644
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)

Can we rather check if __int128 is natively supported? So this part get compiled once Clang do support it, else we'll never use it...

Quoted 3 lines

We already define CONFIG_INT128 so you could just use that.

Thomas does the s390 clang leave CONFIG_INT128=y in config-host.mak?

Yes, CONFIG_INT128=y is also set with Clang on s390x. It's really just that it does not like __int128 to be passed as parameters for inline assembly...

Thomas

#4 lore raw
Quoted 36 lines · Thomas Huth
Quoted 35 lines · Alex Bennée
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 22 lines · Thomas Huth

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/fpu/softfloat-macros.h +0 -0
index b1d772e..bd5b641 100644

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 644

Quoted 5 lines
include/fpu/softfloat-macros.h +1 -1
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)

Can we rather check if __int128 is natively supported? So this part get compiled once Clang do support it, else we'll never use it...

Quoted 7 lines
Quoted 3 lines

We already define CONFIG_INT128 so you could just use that.

Thomas does the s390 clang leave CONFIG_INT128=y in config-host.mak?

Yes, CONFIG_INT128=y is also set with Clang on s390x. It's really just that it does not like __int128 to be passed as parameters for inline assembly...

What about something like this:

modified include/fpu/softfloat-macros.h
@@ -641,12 +641,6 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,

     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
-    /* Need to use a TImode type to get an even register pair for DLGR.  */
-    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
-    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
-    *r = n >> 64;
-    return n;
 #elif defined(_ARCH_PPC64) && defined(_ARCH_PWR7)
     /* From Power ISA 2.06, programming note for divdeu.  */
     uint64_t q1, q2, Q, r1, r2, R;
@@ -663,6 +657,11 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     }
     *r = R;
     return Q;
+#elif defined(CONFIG_INT128)
+    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
+    unsigned __int128 q = n / d;
+    *r = q >> 64;
+    return q;
 #else
     uint64_t d0, d1, q0, q1, r1, r0, m;

--
Alex Bennée

#5 lore raw
Quoted 64 lines · Alex Bennée
Quoted 36 lines · Thomas Huth
Quoted 35 lines · Alex Bennée
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 22 lines · Thomas Huth

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/fpu/softfloat-macros.h +0 -0
index b1d772e..bd5b641 100644

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 644

Quoted 5 lines
include/fpu/softfloat-macros.h +1 -1
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)

Can we rather check if __int128 is natively supported? So this part get compiled once Clang do support it, else we'll never use it...

Quoted 7 lines
Quoted 3 lines

We already define CONFIG_INT128 so you could just use that.

Thomas does the s390 clang leave CONFIG_INT128=y in config-host.mak?

Yes, CONFIG_INT128=y is also set with Clang on s390x. It's really just that it does not like __int128 to be passed as parameters for inline assembly...

What about something like this:

modified include/fpu/softfloat-macros.h

(quoted diff) +0 -6
@@ -641,12 +641,6 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
-    /* Need to use a TImode type to get an even register pair for DLGR.  */
-    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
-    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
-    *r = n >> 64;
-    return n;
 #elif defined(_ARCH_PPC64) && defined(_ARCH_PWR7)
     /* From Power ISA 2.06, programming note for divdeu.  */
     uint64_t q1, q2, Q, r1, r2, R;

Review static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 664

Quoted 8 lines
(quoted diff) +5 -0
@@ -663,6 +657,11 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     }
     *r = R;
     return Q;
+#elif defined(CONFIG_INT128)
+    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
+    unsigned __int128 q = n / d;
+    *r = q >> 64;
+    return q;

Because that is not what the assembly does, for one.

But perhaps

    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    *r = n % d;
    return n / d;

will allow the compiler to do what the assembly does for some 64-bit hosts.

r~

#6 lore raw
Quoted 67 lines · Richard Henderson
Quoted 66 lines · Alex Bennée
Quoted 36 lines · Thomas Huth
Quoted 35 lines · Alex Bennée
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 22 lines · Thomas Huth

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/fpu/softfloat-macros.h +0 -0
index b1d772e..bd5b641 100644

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 644

Quoted 5 lines
include/fpu/softfloat-macros.h +1 -1
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)

Can we rather check if __int128 is natively supported? So this part get compiled once Clang do support it, else we'll never use it...

Quoted 7 lines
Quoted 3 lines

We already define CONFIG_INT128 so you could just use that.

Thomas does the s390 clang leave CONFIG_INT128=y in config-host.mak?

Yes, CONFIG_INT128=y is also set with Clang on s390x. It's really just that it does not like __int128 to be passed as parameters for inline assembly...

What about something like this:

modified include/fpu/softfloat-macros.h

(quoted diff) +0 -6
@@ -641,12 +641,6 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
-    /* Need to use a TImode type to get an even register pair for DLGR.  */
-    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
-    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
-    *r = n >> 64;
-    return n;
 #elif defined(_ARCH_PPC64) && defined(_ARCH_PWR7)
     /* From Power ISA 2.06, programming note for divdeu.  */
     uint64_t q1, q2, Q, r1, r2, R;
@@ -663,6 +657,11 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     }
     *r = R;
Quoted 8 lines

Review static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 664

Quoted 6 lines
(quoted diff) +5 -0
     return Q;
+#elif defined(CONFIG_INT128)
+    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
+    unsigned __int128 q = n / d;
+    *r = q >> 64;
+    return q;

Because that is not what the assembly does, for one.

Doh...

Quoted 8 lines

But perhaps

    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    *r = n % d;
    return n / d;

will allow the compiler to do what the assembly does for some 64-bit hosts.

I wonder how much cost is incurred by the jumping to the (libgcc?) div helper? Anyone got an s390x about so we can benchmark the two approaches?

If it's in the noise then it would be nice to avoid getting too #ifdef happy.

--
Alex Bennée

#7 lore raw
Quoted 9 lines · Alex Bennée
Quoted 6 lines · Richard Henderson

But perhaps

    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    *r = n % d;
    return n / d;
Quoted 6 lines
Quoted 2 lines

will allow the compiler to do what the assembly does for some 64-bit hosts.

I wonder how much cost is incurred by the jumping to the (libgcc?) div helper? Anyone got an s390x about so we can benchmark the two approaches?

The project has an s390x system available; however it's usually running merge build tests so not so useful for benchmarking. (I can set up accounts on it but that requires me to faff about figuring out how to create new accounts :-))

thanks
-- PMM

#8 lore raw
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Alex Bennée
Quoted 8 lines · Richard Henderson

But perhaps

    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    *r = n % d;
    return n / d;

will allow the compiler to do what the assembly does for some 64-bit hosts.

Quoted 8 lines
Quoted 3 lines

I wonder how much cost is incurred by the jumping to the (libgcc?) div helper? Anyone got an s390x about so we can benchmark the two approaches?

The project has an s390x system available; however it's usually running merge build tests so not so useful for benchmarking. (I can set up accounts on it but that requires me to faff about figuring out how to create new accounts :-))

I'm happy to leave this up to those who care about s390x host performance (Thomas, Cornelia?). I'm just keen to avoid the divide helper getting too #ifdefy.

I'll include a CONFIG_INT128 patch in my next patch queue review once I've double checked and tested under linux-user ;-)

--
Alex Bennée

#9 lore raw
Quoted 20 lines · Alex Bennée
Quoted 17 lines · Peter Maydell
Quoted 15 lines · Alex Bennée
Quoted 8 lines · Richard Henderson

But perhaps

    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    *r = n % d;
    return n / d;

will allow the compiler to do what the assembly does for some 64-bit hosts.

I wonder how much cost is incurred by the jumping to the (libgcc?) div helper? Anyone got an s390x about so we can benchmark the two approaches?

Quoted 8 lines
Quoted 4 lines

The project has an s390x system available; however it's usually running merge build tests so not so useful for benchmarking. (I can set up accounts on it but that requires me to faff about figuring out how to create new accounts :-))

I'm happy to leave this up to those who care about s390x host performance (Thomas, Cornelia?). I'm just keen to avoid the divide helper getting too #ifdefy.

Ok, I just did a quick'n'dirty "benchmark" on the s390x that I've got available:

#include <stdio.h>
#include <time.h>
#include <stdint.h>

uint64_t udiv_qrnnd1(uint64_t *r, uint64_t n1, uint64_t n0, uint64_t d)

{
    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
    *r = n >> 64;
    return n;
}

uint64_t udiv_qrnnd2(uint64_t *r, uint64_t n1, uint64_t n0, uint64_t d)

{
    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    *r = n % d;
    return n / d;
}

uint64_t udiv_qrnnd3(uint64_t *r, uint64_t n1, uint64_t n0, uint64_t d)

{
    uint64_t d0, d1, q0, q1, r1, r0, m;

    d0 = (uint32_t)d;
    d1 = d >> 32;

    r1 = n1 % d1;
    q1 = n1 / d1;
    m = q1 * d0;
    r1 = (r1 << 32) | (n0 >> 32);
    if (r1 < m) {
        q1 -= 1;
        r1 += d;
        if (r1 >= d) {
            if (r1 < m) {
                q1 -= 1;
                r1 += d;
            }
        }
    }
    r1 -= m;

    r0 = r1 % d1;
    q0 = r1 / d1;
    m = q0 * d0;
    r0 = (r0 << 32) | (uint32_t)n0;
    if (r0 < m) {
        q0 -= 1;
        r0 += d;
        if (r0 >= d) {
            if (r0 < m) {
                q0 -= 1;
                r0 += d;
            }
        }
    }
    r0 -= m;

    *r = r0;
    return (q1 << 32) | q0;

}

int main()

{
	uint64_t r = 0, n1 = 0, n0 = 0, d = 0;
	uint64_t rs = 0, rn = 0;
	clock_t start, end;
	long i;

	start = clock();
	for (i=0; i<200000000L; i++) {
		n1 += 3;
		n0 += 987654321;
		d += 0x123456789;
		rs += udiv_qrnnd1(&r, n1, n0, d);
		rn += r;
	}
	end = clock();
	printf("test 1: time=%li\t, rs=%li , rn = %li\n", (end-start)/1000, rs, rn);

	r = n1 = n0 = d = rs = rn = 0;

	start = clock();
	for (i=0; i<200000000L; i++) {
		n1 += 3;
		n0 += 987654321;
		d += 0x123456789;
		rs += udiv_qrnnd2(&r, n1, n0, d);
		rn += r;
	}
	end = clock();
	printf("test 2: time=%li\t, rs=%li , rn = %li\n", (end-start)/1000, rs, rn);

	r = n1 = n0 = d = rs = rn = 0;

	start = clock();
	for (i=0; i<200000000L; i++) {
		n1 += 3;
		n0 += 987654321;
		d += 0x123456789;
		rs += udiv_qrnnd3(&r, n1, n0, d);
		rn += r;
	}
	end = clock();
	printf("test 3: time=%li\t, rs=%li , rn = %li\n", (end-start)/1000, rs, rn);

	return 0;
}

... and results with GCC v8.2.1 are (using -O2):

test 1: time=609 , rs=2264924160200000000 , rn = 6136218997527160832
test 2: time=10127 , rs=2264924160200000000 , rn = 6136218997527160832
test 3: time=2350 , rs=2264924183048928865 , rn = 4842822048162311089

Thus the int128 version is the slowest!

... but at least it gives the same results as the DLGR instruction. The 64-bit version gives different results - do we have a bug here?

Results with Clang v7.0.1 (using -O2, too) are these:

test 2: time=5035 , rs=2264924160200000000 , rn = 6136218997527160832
test 3: time=1970 , rs=2264924183048928865 , rn = 4842822048162311089

Thomas

#10 lore raw
Quoted 26 lines · Thomas Huth
Quoted 25 lines · Alex Bennée
Quoted 21 lines · Peter Maydell
Quoted 9 lines · Alex Bennée
Quoted 6 lines · Richard Henderson

But perhaps

    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    *r = n % d;
    return n / d;
Quoted 6 lines
Quoted 2 lines

will allow the compiler to do what the assembly does for some 64-bit hosts.

I wonder how much cost is incurred by the jumping to the (libgcc?) div helper? Anyone got an s390x about so we can benchmark the two approaches?

The project has an s390x system available; however it's usually running merge build tests so not so useful for benchmarking. (I can set up accounts on it but that requires me to faff about figuring out how to create new accounts :-))

Quoted 6 lines
Quoted 3 lines

I'm happy to leave this up to those who care about s390x host performance (Thomas, Cornelia?). I'm just keen to avoid the divide helper getting too #ifdefy.

Ok, I just did a quick'n'dirty "benchmark" on the s390x that I've got available:

Ahh I should have mentioned we already have the technology for this ;-)

If you build the fpu/next tree on a s390x you can then run:

./tests/fp/fp-bench f64_div

with and without the CONFIG_128 path. To get an idea of the real world impact you can compile a foreign binary and run it on a s390x system with:

$QEMU ./tests/fp/fp-bench f64_div -t host

And that will give you the peak performance assuming your program is doing nothing but f64_div operations. If the two QEMU's are basically in

Quoted 19 lines · the same ballpark then it doesn't make enough difference. That said:

#include <stdio.h>
#include <time.h>
#include <stdint.h>

uint64_t udiv_qrnnd1(uint64_t *r, uint64_t n1, uint64_t n0, uint64_t d)

{
    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
    *r = n >> 64;
    return n;
}

uint64_t udiv_qrnnd2(uint64_t *r, uint64_t n1, uint64_t n0, uint64_t d)

{
    unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
    *r = n % d;
    return n / d;
}

<snip>

Quoted 48 lines

int main()

{
	uint64_t r = 0, n1 = 0, n0 = 0, d = 0;
	uint64_t rs = 0, rn = 0;
	clock_t start, end;
	long i;

	start = clock();
	for (i=0; i<200000000L; i++) {
		n1 += 3;
		n0 += 987654321;
		d += 0x123456789;
		rs += udiv_qrnnd1(&r, n1, n0, d);
		rn += r;
	}
	end = clock();
	printf("test 1: time=%li\t, rs=%li , rn = %li\n", (end-start)/1000, rs, rn);

	r = n1 = n0 = d = rs = rn = 0;

	start = clock();
	for (i=0; i<200000000L; i++) {
		n1 += 3;
		n0 += 987654321;
		d += 0x123456789;
		rs += udiv_qrnnd2(&r, n1, n0, d);
		rn += r;
	}
	end = clock();
	printf("test 2: time=%li\t, rs=%li , rn = %li\n", (end-start)/1000, rs, rn);

	r = n1 = n0 = d = rs = rn = 0;

	start = clock();
	for (i=0; i<200000000L; i++) {
		n1 += 3;
		n0 += 987654321;
		d += 0x123456789;
		rs += udiv_qrnnd3(&r, n1, n0, d);
		rn += r;
	}
	end = clock();
	printf("test 3: time=%li\t, rs=%li , rn = %li\n", (end-start)/1000, rs, rn);

	return 0;
}
Quoted 7 lines

... and results with GCC v8.2.1 are (using -O2):

test 1: time=609 , rs=2264924160200000000 , rn = 6136218997527160832
test 2: time=10127 , rs=2264924160200000000 , rn = 6136218997527160832
test 3: time=2350 , rs=2264924183048928865 , rn = 4842822048162311089

Thus the int128 version is the slowest!

I'd expect a little slow down due to the indirection into libgcc.. but that seems pretty high.

Quoted 9 lines

... but at least it gives the same results as the DLGR instruction. The 64-bit version gives different results - do we have a bug here?

Results with Clang v7.0.1 (using -O2, too) are these:

test 2: time=5035 , rs=2264924160200000000 , rn = 6136218997527160832
test 3: time=1970 , rs=2264924183048928865 , rn =
4842822048162311089

You can run:

./tests/fp/fp-test f64_div -l 2 -r all

For a proper comprehensive test.

--
Alex Bennée

#11 lore raw
Quoted 10 lines · Alex Bennée

Ahh I should have mentioned we already have the technology for this ;-)

If you build the fpu/next tree on a s390x you can then run:

./tests/fp/fp-bench f64_div

with and without the CONFIG_128 path. To get an idea of the real world impact you can compile a foreign binary and run it on a s390x system with:

Quoted 5 lines

$QEMU ./tests/fp/fp-bench f64_div -t host

And that will give you the peak performance assuming your program is doing nothing but f64_div operations. If the two QEMU's are basically in the same ballpark then it doesn't make enough difference. That said:

I think you mean here `tests/fp/fp-bench -o div -p double', otherwise you'll get the default op (-o add).

Thanks,

		E.
#12 lore raw
Quoted 11 lines · Emilio G. Cota
Quoted 10 lines · Alex Bennée

Ahh I should have mentioned we already have the technology for this ;-)

If you build the fpu/next tree on a s390x you can then run:

./tests/fp/fp-bench f64_div

with and without the CONFIG_128 path. To get an idea of the real world impact you can compile a foreign binary and run it on a s390x system with:

Quoted 8 lines
Quoted 5 lines

$QEMU ./tests/fp/fp-bench f64_div -t host

And that will give you the peak performance assuming your program is doing nothing but f64_div operations. If the two QEMU's are basically in the same ballpark then it doesn't make enough difference. That said:

I think you mean here `tests/fp/fp-bench -o div -p double', otherwise you'll get the default op (-o add).

I tried that now, too, and -o div -p double does not really seem to exercise this function at all.

Here are my results (disclaimer: that system is likely not really usable for benchmarks since it's CPUs are shared with other LPARs, but I ran all the tests at least twice and got similar results):

With the DGLR inline assembly:

time ./fp-test f64_div -l 2 -r all
real 6m43,648s
user 6m43,362s
sys 0m0,160s

time ./fp-bench -o div -p double
204.98 MFlops
real 0m1,002s
user 0m1,001s
sys 0m0,001s

With the "#else" default 64-bit code:

time ./fp-test f64_div -l 2 -r all
real 6m44,910s
user 6m44,616s
sys 0m0,165s

time ./fp-bench -o div -p double
205.41 MFlops
real 0m1,002s
user 0m1,001s
sys 0m0,001s

With the new CONFIG_INT128 code:

time ./fp-test f64_div -l 2 -r all
real 6m58,371s
user 6m58,078s
sys 0m0,164s

time ./fp-bench -o div -p double
205.17 MFlops
real 0m1,002s
user 0m1,000s
sys 0m0,001s

==> The new CONFIG_INT128 code is really worse than the 64-bit code, so I don't think we should include this yet (unless we know a system where the compiler can create optimized assembly code without libgcc here).

Thomas

#13 lore raw
Quoted 18 lines · Thomas Huth
Quoted 17 lines · Emilio G. Cota
Quoted 15 lines · Alex Bennée

Ahh I should have mentioned we already have the technology for this ;-)

If you build the fpu/next tree on a s390x you can then run:

./tests/fp/fp-bench f64_div

with and without the CONFIG_128 path. To get an idea of the real world impact you can compile a foreign binary and run it on a s390x system with:

$QEMU ./tests/fp/fp-bench f64_div -t host

And that will give you the peak performance assuming your program is doing nothing but f64_div operations. If the two QEMU's are basically in the same ballpark then it doesn't make enough difference. That said:

Quoted 5 lines
Quoted 2 lines

I think you mean here `tests/fp/fp-bench -o div -p double', otherwise you'll get the default op (-o add).

I tried that now, too, and -o div -p double does not really seem to exercise this function at all.

How do you mean? It should do because by default it should be calling the softfloat implementations.

Quoted 7 lines

Here are my results (disclaimer: that system is likely not really usable for benchmarks since it's CPUs are shared with other LPARs, but I ran all the tests at least twice and got similar results):

With the DGLR inline assembly:

<snip>

Quoted 2 lines

time ./fp-bench -o div -p double
204.98 MFlops

<snip>

Quoted 2 lines

With the "#else" default 64-bit code:

<snip>

Quoted 2 lines

time ./fp-bench -o div -p double
205.41 MFlops

<snip>

Quoted 2 lines

With the new CONFIG_INT128 code:

<snip>

Quoted 2 lines

time ./fp-bench -o div -p double
205.17 MFlops

<snip>

Quoted 5 lines

==> The new CONFIG_INT128 code is really worse than the 64-bit code, so I don't think we should include this yet (unless we know a system where the compiler can create optimized assembly code without libgcc here).

I mean to me that looks like it is easily in the noise range and that the dglr instruction didn't actually beat the unrolled 64 bit code - which is just weird.

--
Alex Bennée

#14 lore raw
Quoted 21 lines · Alex Bennée
Quoted 18 lines · Thomas Huth
Quoted 17 lines · Emilio G. Cota
Quoted 15 lines · Alex Bennée

Ahh I should have mentioned we already have the technology for this ;-)

If you build the fpu/next tree on a s390x you can then run:

./tests/fp/fp-bench f64_div

with and without the CONFIG_128 path. To get an idea of the real world impact you can compile a foreign binary and run it on a s390x system with:

$QEMU ./tests/fp/fp-bench f64_div -t host

And that will give you the peak performance assuming your program is doing nothing but f64_div operations. If the two QEMU's are basically in the same ballpark then it doesn't make enough difference. That said:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I think you mean here `tests/fp/fp-bench -o div -p double', otherwise you'll get the default op (-o add).

I tried that now, too, and -o div -p double does not really seem to exercise this function at all.

How do you mean? It should do because by default it should be calling the softfloat implementations.

I've added a puts("hello") into the udiv_qrnd() function. When I then run "fp-bench -o div -p double", it only prints out "hello" a single time, so the function is only called once during the whole test.

Thomas

#15 lore raw
Quoted 28 lines · Thomas Huth
Quoted 27 lines · Alex Bennée
Quoted 23 lines · Thomas Huth
Quoted 11 lines · Emilio G. Cota
Quoted 10 lines · Alex Bennée

Ahh I should have mentioned we already have the technology for this ;-)

If you build the fpu/next tree on a s390x you can then run:

./tests/fp/fp-bench f64_div

with and without the CONFIG_128 path. To get an idea of the real world impact you can compile a foreign binary and run it on a s390x system with:

Quoted 8 lines
Quoted 5 lines

$QEMU ./tests/fp/fp-bench f64_div -t host

And that will give you the peak performance assuming your program is doing nothing but f64_div operations. If the two QEMU's are basically in the same ballpark then it doesn't make enough difference. That said:

I think you mean here `tests/fp/fp-bench -o div -p double', otherwise you'll get the default op (-o add).

I tried that now, too, and -o div -p double does not really seem to exercise this function at all.

Quoted 6 lines
Quoted 2 lines

How do you mean? It should do because by default it should be calling the softfloat implementations.

I've added a puts("hello") into the udiv_qrnd() function. When I then run "fp-bench -o div -p double", it only prints out "hello" a single time, so the function is only called once during the whole test.

That's very odd. With the following on my aarch64 box:

modified include/fpu/softfloat-macros.h
@@ -637,6 +637,8 @@ static inline uint64_t estimateDiv128To64(uint64_t a0, uint64_t a1, uint64_t b)
static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,

                                   uint64_t n0, uint64_t d)
 {
+    static int iter = 0;
+    fprintf(stderr, "%s: %d\n", __func__, iter++);
 #if defined(__x86_64__)
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));

I get:

udiv_qrnnd: 0
udiv_qrnnd: 1
..
..<all the way to>
udiv_qrnnd: 99998
udiv_qrnnd: 99999

So I'm not sure what is different on s390

--
Alex Bennée

#16 lore raw
In reply to Thomas Huth
Quoted 18 lines · Thomas Huth
Quoted 17 lines · Emilio G. Cota
Quoted 15 lines · Alex Bennée

Ahh I should have mentioned we already have the technology for this ;-)

If you build the fpu/next tree on a s390x you can then run:

./tests/fp/fp-bench f64_div

with and without the CONFIG_128 path. To get an idea of the real world impact you can compile a foreign binary and run it on a s390x system with:

$QEMU ./tests/fp/fp-bench f64_div -t host

And that will give you the peak performance assuming your program is doing nothing but f64_div operations. If the two QEMU's are basically in the same ballpark then it doesn't make enough difference. That said:

Quoted 5 lines
Quoted 2 lines

I think you mean here `tests/fp/fp-bench -o div -p double', otherwise you'll get the default op (-o add).

I tried that now, too, and -o div -p double does not really seem to exercise this function at all.

You can check what is being called then with perf record/report.

		E.
#17 lore raw
In reply to Thomas Huth
Quoted 2 lines · Thomas Huth

... but at least it gives the same results as the DLGR instruction. The 64-bit version gives different results - do we have a bug here?

Yes, on your inputs. udiv_qrnnd3 requires that D be "normalized", i.e. have the most siginificant bit set. (And thus N1:N0 shifted left to match, and the returned remainder shifted right to match.)

r~

Reviewed-by on v1 1/1
#18 lore raw
In reply to Thomas Huth
Quoted 9 lines · Thomas Huth

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^
Quoted 7 lines

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Regardless of whatever follow-up we might do wrt CONFIG_INT128.

r~

#19 lore raw
In reply to Thomas Huth
Quoted 22 lines · On Mon, 14 Jan 2019 13:12:35 +0100 Thomas Huth <[email protected]> wrote:

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/fpu/softfloat-macros.h +0 -0
index b1d772e..bd5b641 100644

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 647

Quoted 8 lines
include/fpu/softfloat-macros.h +1 -1
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)
     /* Need to use a TImode type to get an even register pair for DLGR.  */
     unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
     asm("dlgr %0, %1" : "+r"(n) : "r"(d));

Ok, so what's the deal with this patch now? Fix compilation now, optimize later?

If yes, should I pick it as an s390x build fix (I plan to send a pull request later this week), or will the fpu maintainers pick it?

#20 lore raw
Quoted 3 lines · Cornelia Huck

On Mon, 14 Jan 2019 13:12:35 +0100
Thomas Huth <[email protected]> wrote:

<snip>

Quoted 18 lines
Quoted 6 lines
include/fpu/softfloat-macros.h +0 -0
index b1d772e..bd5b641 100644

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 647

Quoted 8 lines
include/fpu/softfloat-macros.h +1 -1
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)
     /* Need to use a TImode type to get an even register pair for DLGR.  */
     unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
     asm("dlgr %0, %1" : "+r"(n) : "r"(d));

Ok, so what's the deal with this patch now? Fix compilation now, optimize later?

Quoted 2 lines

If yes, should I pick it as an s390x build fix (I plan to send a pull request later this week), or will the fpu maintainers pick it?

I'm planning to send a FPU PR tomorrow and I'll happily include either version.

I'm personally minded to go with the patch that makes s390 (and others) fall back to the generic CONFIG_INT128 code. The numbers Thomas gathered didn't look like it was much difference either way.

Unless you *really* care about milking that last bit of performance out of the s390 TCG back-end?

--
Alex Bennée

#21 lore raw
Quoted 31 lines · Alex Bennée
Quoted 3 lines · Cornelia Huck

On Mon, 14 Jan 2019 13:12:35 +0100
Thomas Huth <[email protected]> wrote:

<snip>

Quoted 18 lines
Quoted 6 lines
include/fpu/softfloat-macros.h +0 -0
index b1d772e..bd5b641 100644

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 647

Quoted 8 lines
include/fpu/softfloat-macros.h +1 -1
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)
     /* Need to use a TImode type to get an even register pair for DLGR.  */
     unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
     asm("dlgr %0, %1" : "+r"(n) : "r"(d));

Ok, so what's the deal with this patch now? Fix compilation now, optimize later?

Quoted 2 lines

If yes, should I pick it as an s390x build fix (I plan to send a pull request later this week), or will the fpu maintainers pick it?

I'm planning to send a FPU PR tomorrow and I'll happily include either version.

Quoted 6 lines

I'm personally minded to go with the patch that makes s390 (and others) fall back to the generic CONFIG_INT128 code. The numbers Thomas gathered didn't look like it was much difference either way.

Unless you *really* care about milking that last bit of performance out of the s390 TCG back-end?

I don't think that anybody buys a mainframe for this special case ;-) I'd go with the !defined(__clang__) patch above.

Thomas

Acked-by on v1 1/1
#22 lore raw
In reply to Thomas Huth
Quoted 22 lines · On Mon, 14 Jan 2019 13:12:35 +0100 Thomas Huth <[email protected]> wrote:

Clang v7.0.1 does not like the __int128 variable type for inline assembly on s390x:

In file included from fpu/softfloat.c:97:
include/fpu/softfloat-macros.h:647:9: error: inline asm error:
This value type register class is not natively supported!

    asm("dlgr %0, %1" : "+r"(n) : "r"(d));
        ^

Disable this code part there now when compiling with Clang, so that the generic code gets used instead.

---
 include/fpu/softfloat-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/fpu/softfloat-macros.h +0 -0
index b1d772e..bd5b641 100644

include/fpu/softfloat-macros.h static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, · line 647

Quoted 8 lines
include/fpu/softfloat-macros.h +1 -1
@@ -641,7 +641,7 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
     uint64_t q;
     asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
     return q;
-#elif defined(__s390x__)
+#elif defined(__s390x__) && !defined(__clang__)
     /* Need to use a TImode type to get an even register pair for DLGR.  */
     unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
     asm("dlgr %0, %1" : "+r"(n) : "r"(d));

...as this fixed that s390x clang problem for me.