Source file test/codegen/math.go
1 // asmcheck 2 3 // Copyright 2018 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package codegen 8 9 import "math" 10 11 var sink64 [8]float64 12 13 func approx(x float64) { 14 // amd64/v2:-".*x86HasSSE41" amd64/v3:-".*x86HasSSE41" 15 // amd64:"ROUNDSD\t[$]2" 16 // s390x:"FIDBR\t[$]6" 17 // arm64:"FRINTPD" 18 // ppc64x:"FRIP" 19 // wasm:"F64Ceil" 20 sink64[0] = math.Ceil(x) 21 22 // amd64/v2:-".*x86HasSSE41" amd64/v3:-".*x86HasSSE41" 23 // amd64:"ROUNDSD\t[$]1" 24 // s390x:"FIDBR\t[$]7" 25 // arm64:"FRINTMD" 26 // ppc64x:"FRIM" 27 // wasm:"F64Floor" 28 sink64[1] = math.Floor(x) 29 30 // s390x:"FIDBR\t[$]1" 31 // arm64:"FRINTAD" 32 // ppc64x:"FRIN" 33 sink64[2] = math.Round(x) 34 35 // amd64/v2:-".*x86HasSSE41" amd64/v3:-".*x86HasSSE41" 36 // amd64:"ROUNDSD\t[$]3" 37 // s390x:"FIDBR\t[$]5" 38 // arm64:"FRINTZD" 39 // ppc64x:"FRIZ" 40 // wasm:"F64Trunc" 41 sink64[3] = math.Trunc(x) 42 43 // amd64/v2:-".*x86HasSSE41" amd64/v3:-".*x86HasSSE41" 44 // amd64:"ROUNDSD\t[$]0" 45 // s390x:"FIDBR\t[$]4" 46 // arm64:"FRINTND" 47 // wasm:"F64Nearest" 48 sink64[4] = math.RoundToEven(x) 49 } 50 51 func sqrt(x float64) float64 { 52 // amd64:"SQRTSD" 53 // 386/sse2:"SQRTSD" 386/softfloat:-"SQRTD" 54 // arm64:"FSQRTD" 55 // arm/7:"SQRTD" 56 // mips/hardfloat:"SQRTD" mips/softfloat:-"SQRTD" 57 // mips64/hardfloat:"SQRTD" mips64/softfloat:-"SQRTD" 58 // wasm:"F64Sqrt" 59 // ppc64x:"FSQRT" 60 // riscv64: "FSQRTD" 61 return math.Sqrt(x) 62 } 63 64 func sqrt32(x float32) float32 { 65 // amd64:"SQRTSS" 66 // 386/sse2:"SQRTSS" 386/softfloat:-"SQRTS" 67 // arm64:"FSQRTS" 68 // arm/7:"SQRTF" 69 // mips/hardfloat:"SQRTF" mips/softfloat:-"SQRTF" 70 // mips64/hardfloat:"SQRTF" mips64/softfloat:-"SQRTF" 71 // wasm:"F32Sqrt" 72 // ppc64x:"FSQRTS" 73 // riscv64: "FSQRTS" 74 return float32(math.Sqrt(float64(x))) 75 } 76 77 // Check that it's using integer registers 78 func abs(x, y float64) { 79 // amd64:"BTRQ\t[$]63" 80 // arm64:"FABSD\t" 81 // s390x:"LPDFR\t",-"MOVD\t" (no integer load/store) 82 // ppc64x:"FABS\t" 83 // riscv64:"FABSD\t" 84 // wasm:"F64Abs" 85 // arm/6:"ABSD\t" 86 // mips64/hardfloat:"ABSD\t" 87 // mips/hardfloat:"ABSD\t" 88 sink64[0] = math.Abs(x) 89 90 // amd64:"BTRQ\t[$]63","PXOR" (TODO: this should be BTSQ) 91 // s390x:"LNDFR\t",-"MOVD\t" (no integer load/store) 92 // ppc64x:"FNABS\t" 93 sink64[1] = -math.Abs(y) 94 } 95 96 // Check that it's using integer registers 97 func abs32(x float32) float32 { 98 // s390x:"LPDFR",-"LDEBR",-"LEDBR" (no float64 conversion) 99 return float32(math.Abs(float64(x))) 100 } 101 102 // Check that it's using integer registers 103 func copysign(a, b, c float64) { 104 // amd64:"BTRQ\t[$]63","ANDQ","ORQ" 105 // s390x:"CPSDR",-"MOVD" (no integer load/store) 106 // ppc64x:"FCPSGN" 107 // riscv64:"FSGNJD" 108 // wasm:"F64Copysign" 109 sink64[0] = math.Copysign(a, b) 110 111 // amd64:"BTSQ\t[$]63" 112 // s390x:"LNDFR\t",-"MOVD\t" (no integer load/store) 113 // ppc64x:"FCPSGN" 114 // riscv64:"FSGNJD" 115 // arm64:"ORR", -"AND" 116 sink64[1] = math.Copysign(c, -1) 117 118 // Like math.Copysign(c, -1), but with integer operations. Useful 119 // for platforms that have a copysign opcode to see if it's detected. 120 // s390x:"LNDFR\t",-"MOVD\t" (no integer load/store) 121 sink64[2] = math.Float64frombits(math.Float64bits(a) | 1<<63) 122 123 // amd64:"ANDQ","ORQ" 124 // s390x:"CPSDR\t",-"MOVD\t" (no integer load/store) 125 // ppc64x:"FCPSGN" 126 // riscv64:"FSGNJD" 127 sink64[3] = math.Copysign(-1, c) 128 } 129 130 func fma(x, y, z float64) float64 { 131 // amd64/v3:-".*x86HasFMA" 132 // amd64:"VFMADD231SD" 133 // arm/6:"FMULAD" 134 // arm64:"FMADDD" 135 // loong64:"FMADDD" 136 // s390x:"FMADD" 137 // ppc64x:"FMADD" 138 // riscv64:"FMADDD" 139 return math.FMA(x, y, z) 140 } 141 142 func fms(x, y, z float64) float64 { 143 // riscv64:"FMSUBD" 144 return math.FMA(x, y, -z) 145 } 146 147 func fnms(x, y, z float64) float64 { 148 // riscv64:"FNMSUBD",-"FNMADDD" 149 return math.FMA(-x, y, z) 150 } 151 152 func fnma(x, y, z float64) float64 { 153 // riscv64:"FNMADDD",-"FNMSUBD" 154 return math.FMA(x, -y, -z) 155 } 156 157 func fromFloat64(f64 float64) uint64 { 158 // amd64:"MOVQ\tX.*, [^X].*" 159 // arm64:"FMOVD\tF.*, R.*" 160 // loong64:"MOVV\tF.*, R.*" 161 // ppc64x:"MFVSRD" 162 // mips64/hardfloat:"MOVV\tF.*, R.*" 163 return math.Float64bits(f64+1) + 1 164 } 165 166 func fromFloat32(f32 float32) uint32 { 167 // amd64:"MOVL\tX.*, [^X].*" 168 // arm64:"FMOVS\tF.*, R.*" 169 // loong64:"MOVW\tF.*, R.*" 170 // mips64/hardfloat:"MOVW\tF.*, R.*" 171 return math.Float32bits(f32+1) + 1 172 } 173 174 func toFloat64(u64 uint64) float64 { 175 // amd64:"MOVQ\t[^X].*, X.*" 176 // arm64:"FMOVD\tR.*, F.*" 177 // loong64:"MOVV\tR.*, F.*" 178 // ppc64x:"MTVSRD" 179 // mips64/hardfloat:"MOVV\tR.*, F.*" 180 return math.Float64frombits(u64+1) + 1 181 } 182 183 func toFloat32(u32 uint32) float32 { 184 // amd64:"MOVL\t[^X].*, X.*" 185 // arm64:"FMOVS\tR.*, F.*" 186 // loong64:"MOVW\tR.*, F.*" 187 // mips64/hardfloat:"MOVW\tR.*, F.*" 188 return math.Float32frombits(u32+1) + 1 189 } 190 191 // Test that comparisons with constants converted to float 192 // are evaluated at compile-time 193 194 func constantCheck64() bool { 195 // amd64:"(MOVB\t[$]0)|(XORL\t[A-Z][A-Z0-9]+, [A-Z][A-Z0-9]+)",-"FCMP",-"MOVB\t[$]1" 196 // s390x:"MOV(B|BZ|D)\t[$]0,",-"FCMPU",-"MOV(B|BZ|D)\t[$]1," 197 return 0.5 == float64(uint32(1)) || 1.5 > float64(uint64(1<<63)) 198 } 199 200 func constantCheck32() bool { 201 // amd64:"MOV(B|L)\t[$]1",-"FCMP",-"MOV(B|L)\t[$]0" 202 // s390x:"MOV(B|BZ|D)\t[$]1,",-"FCMPU",-"MOV(B|BZ|D)\t[$]0," 203 return float32(0.5) <= float32(int64(1)) && float32(1.5) >= float32(int32(-1<<31)) 204 } 205 206 // Test that integer constants are converted to floating point constants 207 // at compile-time 208 209 func constantConvert32(x float32) float32 { 210 // amd64:"MOVSS\t[$]f32.3f800000\\(SB\\)" 211 // s390x:"FMOVS\t[$]f32.3f800000\\(SB\\)" 212 // ppc64x/power8:"FMOVS\t[$]f32.3f800000\\(SB\\)" 213 // ppc64x/power9:"FMOVS\t[$]f32.3f800000\\(SB\\)" 214 // ppc64x/power10:"XXSPLTIDP\t[$]1065353216, VS0" 215 // arm64:"FMOVS\t[$]\\(1.0\\)" 216 if x > math.Float32frombits(0x3f800000) { 217 return -x 218 } 219 return x 220 } 221 222 func constantConvertInt32(x uint32) uint32 { 223 // amd64:-"MOVSS" 224 // s390x:-"FMOVS" 225 // ppc64x:-"FMOVS" 226 // arm64:-"FMOVS" 227 if x > math.Float32bits(1) { 228 return -x 229 } 230 return x 231 } 232 233 func nanGenerate64() float64 { 234 // Test to make sure we don't generate a NaN while constant propagating. 235 // See issue 36400. 236 zero := 0.0 237 // amd64:-"DIVSD" 238 inf := 1 / zero // +inf. We can constant propagate this one. 239 negone := -1.0 240 241 // amd64:"DIVSD" 242 z0 := zero / zero 243 // amd64:"MULSD" 244 z1 := zero * inf 245 // amd64:"SQRTSD" 246 z2 := math.Sqrt(negone) 247 return z0 + z1 + z2 248 } 249 250 func nanGenerate32() float32 { 251 zero := float32(0.0) 252 // amd64:-"DIVSS" 253 inf := 1 / zero // +inf. We can constant propagate this one. 254 255 // amd64:"DIVSS" 256 z0 := zero / zero 257 // amd64:"MULSS" 258 z1 := zero * inf 259 return z0 + z1 260 } 261