4 // Copyright 2022 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
38 fmt.Fprintf(&prog, "package main\n\n")
39 fmt.Fprintf(&prog, "import ( \"fmt\"; \"math\" )\n")
40 for _, t1 := range types {
41 for _, t2 := range types {
42 fmt.Fprintf(&prog, "func %[1]s_to_%[2]s(x %[1]s) %[2]s { return %[2]s(x) }\n", t1, t2)
49 fmt.Fprintf(&prog, "var (\n")
50 for _, t1 := range types {
54 if t1 == "int64" || bits.UintSize == 64 {
55 inputs = append(inputs, "-0x8000_0000_0000_0000", "-0x7fff_ffff_ffff_ffff", "-0x12_3456_7890", "0x12_3456_7890", "0x7fff_ffff_ffff_ffff")
59 inputs = append(inputs, "-0x8000_0000", "-0x7fff_ffff", "-0x12_3456", "0x12_3456", "0x7fff_ffff")
62 inputs = append(inputs, "-0x8000", "-0x7fff", "-0x1234", "0x1234", "0x7fff")
65 inputs = append(inputs, "-0x80", "-0x7f", "-0x12", "-1", "0", "1", "0x12", "0x7f")
67 case "uint64", "uint", "uintptr":
68 if t1 == "uint64" || bits.UintSize == 64 {
69 inputs = append(inputs, "0x12_3456_7890", "0x7fff_ffff_ffff_ffff", "0x8000_0000_0000_0000", "0xffff_ffff_ffff_ffff")
73 inputs = append(inputs, "0x12_3456", "0x7fff_ffff", "0x8000_0000", "0xffff_ffff")
76 inputs = append(inputs, "0x1234", "0x7fff", "0x8000", "0xffff")
79 inputs = append(inputs, "0", "1", "0x12", "0x7f", "0x80", "0xff")
82 inputs = append(inputs,
83 "-1.79769313486231570814527423731704356798070e+308",
92 "1.79769313486231570814527423731704356798070e+308")
95 inputs = append(inputs,
96 "-3.40282346638528859811704183484516925440e+38",
99 "-1.401298464324817070923729583289916131280e-45",
101 "1.401298464324817070923729583289916131280e-45",
104 "3.40282346638528859811704183484516925440e+38")
106 for _, t2 := range types {
107 for _, x := range inputs {
108 code := fmt.Sprintf("%s_to_%s(%s)", t1, t2, x)
109 fmt.Fprintf(&prog, "\tv%d = %s\n", len(outputs), code)
110 exprs = append(exprs, code)
111 outputs = append(outputs, convert(x, t1, t2))
115 fmt.Fprintf(&prog, ")\n\n")
116 fmt.Fprintf(&prog, "func main() {\n\tok := true\n")
117 for i, out := range outputs {
118 fmt.Fprintf(&prog, "\tif v%d != %s { fmt.Println(%q, \"=\", v%d, \"want\", %s); ok = false }\n", i, out, exprs[i], i, out)
120 fmt.Fprintf(&prog, "\tif !ok { println(\"FAIL\") }\n")
121 fmt.Fprintf(&prog, "}\n")
123 os.Stdout.Write(prog.Bytes())
126 func convert(x, t1, t2 string) string {
127 if strings.HasPrefix(t1, "int") {
128 v, err := strconv.ParseInt(x, 0, 64)
133 return convert1(v, t2)
135 if strings.HasPrefix(t1, "uint") {
136 v, err := strconv.ParseUint(x, 0, 64)
141 return convert1(v, t2)
143 if strings.HasPrefix(t1, "float") {
144 v, err := strconv.ParseFloat(x, 64)
150 v = float64(float32(v))
152 return convert1(v, t2)
157 func convert1[T int64 | uint64 | float64](v T, t2 string) string {
160 return fmt.Sprintf("%s(%#x)", t2, int(v))
162 return fmt.Sprintf("%s(%#x)", t2, int8(v))
164 return fmt.Sprintf("%s(%#x)", t2, int16(v))
166 return fmt.Sprintf("%s(%#x)", t2, int32(v))
168 return fmt.Sprintf("%s(%#x)", t2, int64(v))
170 return fmt.Sprintf("%s(%#x)", t2, uint(v))
172 return fmt.Sprintf("%s(%#x)", t2, uint8(v))
174 return fmt.Sprintf("%s(%#x)", t2, uint16(v))
176 return fmt.Sprintf("%s(%#x)", t2, uint32(v))
178 return fmt.Sprintf("%s(%#x)", t2, uint64(v))
180 return fmt.Sprintf("%s(%#x)", t2, uintptr(v))
183 if math.IsInf(float64(v), -1) {
184 return "float32(math.Inf(-1))"
186 if math.IsInf(float64(v), +1) {
187 return "float32(math.Inf(+1))"
189 return fmt.Sprintf("%s(%v)", t2, float64(v))
191 return fmt.Sprintf("%s(%v)", t2, float64(v))