This is a variation on
https://github.com/gcc-mirror/gcc/blob/2f2aeda98f3aa24034a700e7efcb6c1a9397836f/gcc/testsuite/gcc.c-torture/execute/ieee/rbug.c

----

package main

/*

float s(unsigned long long k) {
        float x;
        x = (float)k;
        return x;
}

unsigned long long cmain() {
        unsigned long long int k;
        double x;
        k = 0x8234508000000001ULL;
        x = s(k);
        k = (unsigned long long)x;
        return k;
}

*/
import "C"

import (
        "fmt"
        "runtime"
)

func s(k uint64) float32 {
        var x float32
        x = float32(k)
        return x
}

func main() {
        ck := uint64(C.cmain())
        k := uint64(0x8234508000000001)
        x := float64(s(k))
        k = uint64(x)
        fmt.Printf("%s, %s/%s\tC: %#x Go: %#x, equal: %v\n",
runtime.Version(), runtime.GOOS, runtime.GOARCH, ck, k, ck == k)
}

----

AFAICT, the values involved in the conversions are not out of range for
float32 or uint64. That's why I would expect the Go results to be the same
on any supported target. That's not the case, see linux/{386,arm}:

----

go1.16.2, openbsd/amd64 C: 0x8234510000000000 Go: 0x8234510000000000,
equal: true
go1.17.1, darwin/amd64  C: 0x8234510000000000 Go: 0x8234510000000000,
equal: true
go1.17.1, freebsd/amd64 C: 0x8234510000000000 Go: 0x8234510000000000,
equal: true
go1.17.1, linux/386     C: 0x8234500000000000 Go: 0x8234500000000000,
equal: true
go1.17.1, linux/amd64   C: 0x8234510000000000 Go: 0x8234510000000000,
equal: true
go1.17.1, linux/arm     C: 0x8234510000000000 Go: 0x8234500000000000,
equal: false
go1.17.1, linux/arm64   C: 0x8234510000000000 Go: 0x8234510000000000,
equal: true
go1.17.1, linux/s390x   C: 0x8234510000000000 Go: 0x8234510000000000,
equal: true
go1.17.1, netbsd/amd64  C: 0x8234510000000000 Go: 0x8234510000000000,
equal: true
go1.17.1, windows/amd64 C: 0x8234510000000000 Go: 0x8234510000000000,
equal: true

----

[Q1] Is that actually permitted by the Go specification? (If so I failed to
find that.)

Note that the results agree with C on all targets I have easy access to -
except linux/arm.

[Q2] Is that expected or not? I'm aware that the Go specification is silent
about such compatibility with C wrt rounding/conversions.

Thanks in advance for any insights.

-j

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-V_4qunm3QzcAr6NnCzRJhFGGh5h%2B1w%3DxVy%3DitYf1cqHA%40mail.gmail.com.

Reply via email to