aboutsummaryrefslogtreecommitdiff
path: root/test/abi/idata.go
blob: af2b87b9d68958df22a5ef6c211ce43485047c91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// run

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Excerpted from go/constant/value.go to capture a bug from there.

package main

import (
	"fmt"
	"math"
	"math/big"
)

type (
	unknownVal struct{}
	intVal     struct{ val *big.Int }   // Int values not representable as an int64
	ratVal     struct{ val *big.Rat }   // Float values representable as a fraction
	floatVal   struct{ val *big.Float } // Float values not representable as a fraction
	complexVal struct{ re, im Value }
)

const prec = 512

func (unknownVal) String() string { return "unknown" }

func (x intVal) String() string   { return x.val.String() }
func (x ratVal) String() string   { return rtof(x).String() }

func (x floatVal) String() string {
	f := x.val

	// Use exact fmt formatting if in float64 range (common case):
	// proceed if f doesn't underflow to 0 or overflow to inf.
	if x, _ := f.Float64(); f.Sign() == 0 == (x == 0) && !math.IsInf(x, 0) {
		return fmt.Sprintf("%.6g", x)
	}

	return "OOPS"
}

func (x complexVal) String() string { return fmt.Sprintf("(%s + %si)", x.re, x.im) }

func newFloat() *big.Float { return new(big.Float).SetPrec(prec) }

//go:noinline
//go:registerparams
func itor(x intVal) ratVal       { return ratVal{nil} }

//go:noinline
//go:registerparams
func itof(x intVal) floatVal     { return floatVal{nil} }
func rtof(x ratVal) floatVal     { return floatVal{newFloat().SetRat(x.val)} }

type Value interface {
	String() string
}

//go:noinline
//go:registerparams
func ToFloat(x Value) Value {
	switch x := x.(type) {
	case intVal:
		if smallInt(x.val) {
			return itor(x)
		}
		return itof(x)
	case ratVal, floatVal:
		return x
	case complexVal:
		if Sign(x.im) == 0 {
			return ToFloat(x.re)
		}
	}
	return unknownVal{}
}

//go:noinline
//go:registerparams
func smallInt(x *big.Int) bool {
	return false
}

//go:noinline
//go:registerparams
func Sign(x Value) int {
	return 0
}


func main() {
	v := ratVal{big.NewRat(22,7)}
	s := ToFloat(v).String()
	fmt.Printf("s=%s\n", s)
}