| 1 |
// runoutput |
| 2 |
//go:build !wasm |
| 3 |
|
| 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. |
| 7 |
|
| 8 |
package main |
| 9 |
|
| 10 |
import ( |
| 11 |
"bytes" |
| 12 |
"fmt" |
| 13 |
"math" |
| 14 |
"math/bits" |
| 15 |
"os" |
| 16 |
"strconv" |
| 17 |
"strings" |
| 18 |
) |
| 19 |
|
| 20 |
var types = []string{ |
| 21 |
"int", |
| 22 |
"int8", |
| 23 |
"int16", |
| 24 |
"int32", |
| 25 |
"int64", |
| 26 |
"uint", |
| 27 |
"uint8", |
| 28 |
"uint16", |
| 29 |
"uint32", |
| 30 |
"uint64", |
| 31 |
"uintptr", |
| 32 |
"float32", |
| 33 |
"float64", |
| 34 |
} |
| 35 |
|
| 36 |
func main() { |
| 37 |
var prog bytes.Buffer |
| 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) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
var outputs []string |
| 47 |
var exprs []string |
| 48 |
|
| 49 |
fmt.Fprintf(&prog, "var (\n") |
| 50 |
for _, t1 := range types { |
| 51 |
var inputs []string |
| 52 |
switch t1 { |
| 53 |
case "int64", "int": |
| 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") |
| 56 |
} |
| 57 |
fallthrough |
| 58 |
case "int32": |
| 59 |
inputs = append(inputs, "-0x8000_0000", "-0x7fff_ffff", "-0x12_3456", "0x12_3456", "0x7fff_ffff") |
| 60 |
fallthrough |
| 61 |
case "int16": |
| 62 |
inputs = append(inputs, "-0x8000", "-0x7fff", "-0x1234", "0x1234", "0x7fff") |
| 63 |
fallthrough |
| 64 |
case "int8": |
| 65 |
inputs = append(inputs, "-0x80", "-0x7f", "-0x12", "-1", "0", "1", "0x12", "0x7f") |
| 66 |
|
| 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") |
| 70 |
} |
| 71 |
fallthrough |
| 72 |
case "uint32": |
| 73 |
inputs = append(inputs, "0x12_3456", "0x7fff_ffff", "0x8000_0000", "0xffff_ffff") |
| 74 |
fallthrough |
| 75 |
case "uint16": |
| 76 |
inputs = append(inputs, "0x1234", "0x7fff", "0x8000", "0xffff") |
| 77 |
fallthrough |
| 78 |
case "uint8": |
| 79 |
inputs = append(inputs, "0", "1", "0x12", "0x7f", "0x80", "0xff") |
| 80 |
|
| 81 |
case "float64": |
| 82 |
inputs = append(inputs, |
| 83 |
"-1.79769313486231570814527423731704356798070e+308", |
| 84 |
"-1e300", |
| 85 |
"-1e100", |
| 86 |
"-1e40", |
| 87 |
"-3.5e38", |
| 88 |
"3.5e38", |
| 89 |
"1e40", |
| 90 |
"1e100", |
| 91 |
"1e300", |
| 92 |
"1.79769313486231570814527423731704356798070e+308") |
| 93 |
fallthrough |
| 94 |
case "float32": |
| 95 |
inputs = append(inputs, |
| 96 |
"-3.40282346638528859811704183484516925440e+38", |
| 97 |
"-1e38", |
| 98 |
"-1.5", |
| 99 |
"-1.401298464324817070923729583289916131280e-45", |
| 100 |
"0", |
| 101 |
"1.401298464324817070923729583289916131280e-45", |
| 102 |
"1.5", |
| 103 |
"1e38", |
| 104 |
"3.40282346638528859811704183484516925440e+38") |
| 105 |
} |
| 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)) |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 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) |
| 119 |
} |
| 120 |
fmt.Fprintf(&prog, "\tif !ok { println(\"FAIL\") }\n") |
| 121 |
fmt.Fprintf(&prog, "}\n") |
| 122 |
|
| 123 |
os.Stdout.Write(prog.Bytes()) |
| 124 |
} |
| 125 |
|
| 126 |
func convert(x, t1, t2 string) string { |
| 127 |
if strings.HasPrefix(t1, "int") { |
| 128 |
v, err := strconv.ParseInt(x, 0, 64) |
| 129 |
if err != nil { |
| 130 |
println(x, t1, t2) |
| 131 |
panic(err) |
| 132 |
} |
| 133 |
return convert1(v, t2) |
| 134 |
} |
| 135 |
if strings.HasPrefix(t1, "uint") { |
| 136 |
v, err := strconv.ParseUint(x, 0, 64) |
| 137 |
if err != nil { |
| 138 |
println(x, t1, t2) |
| 139 |
panic(err) |
| 140 |
} |
| 141 |
return convert1(v, t2) |
| 142 |
} |
| 143 |
if strings.HasPrefix(t1, "float") { |
| 144 |
v, err := strconv.ParseFloat(x, 64) |
| 145 |
if err != nil { |
| 146 |
println(x, t1, t2) |
| 147 |
panic(err) |
| 148 |
} |
| 149 |
if t1 == "float32" { |
| 150 |
v = float64(float32(v)) |
| 151 |
} |
| 152 |
return convert1(v, t2) |
| 153 |
} |
| 154 |
panic(t1) |
| 155 |
} |
| 156 |
|
| 157 |
func convert1[T int64 | uint64 | float64](v T, t2 string) string { |
| 158 |
switch t2 { |
| 159 |
case "int": |
| 160 |
return fmt.Sprintf("%s(%#x)", t2, int(v)) |
| 161 |
case "int8": |
| 162 |
return fmt.Sprintf("%s(%#x)", t2, int8(v)) |
| 163 |
case "int16": |
| 164 |
return fmt.Sprintf("%s(%#x)", t2, int16(v)) |
| 165 |
case "int32": |
| 166 |
return fmt.Sprintf("%s(%#x)", t2, int32(v)) |
| 167 |
case "int64": |
| 168 |
return fmt.Sprintf("%s(%#x)", t2, int64(v)) |
| 169 |
case "uint": |
| 170 |
return fmt.Sprintf("%s(%#x)", t2, uint(v)) |
| 171 |
case "uint8": |
| 172 |
return fmt.Sprintf("%s(%#x)", t2, uint8(v)) |
| 173 |
case "uint16": |
| 174 |
return fmt.Sprintf("%s(%#x)", t2, uint16(v)) |
| 175 |
case "uint32": |
| 176 |
return fmt.Sprintf("%s(%#x)", t2, uint32(v)) |
| 177 |
case "uint64": |
| 178 |
return fmt.Sprintf("%s(%#x)", t2, uint64(v)) |
| 179 |
case "uintptr": |
| 180 |
return fmt.Sprintf("%s(%#x)", t2, uintptr(v)) |
| 181 |
case "float32": |
| 182 |
v := float32(v) |
| 183 |
if math.IsInf(float64(v), -1) { |
| 184 |
return "float32(math.Inf(-1))" |
| 185 |
} |
| 186 |
if math.IsInf(float64(v), +1) { |
| 187 |
return "float32(math.Inf(+1))" |
| 188 |
} |
| 189 |
return fmt.Sprintf("%s(%v)", t2, float64(v)) |
| 190 |
case "float64": |
| 191 |
return fmt.Sprintf("%s(%v)", t2, float64(v)) |
| 192 |
} |
| 193 |
panic(t2) |
| 194 |
} |