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 // s390x:"FMADD" 136 // ppc64x:"FMADD" 137 // riscv64:"FMADDD" 138 return math.FMA(x, y, z) 139 } 140 141 func fms(x, y, z float64) float64 { 142 // riscv64:"FMSUBD" 143 return math.FMA(x, y, -z) 144 } 145 146 func fnms(x, y, z float64) float64 { 147 // riscv64:"FNMSUBD",-"FNMADDD" 148 return math.FMA(-x, y, z) 149 } 150 151 func fnma(x, y, z float64) float64 { 152 // riscv64:"FNMADDD",-"FNMSUBD" 153 return math.FMA(x, -y, -z) 154 } 155 156 func fromFloat64(f64 float64) uint64 { 157 // amd64:"MOVQ\tX.*, [^X].*" 158 // arm64:"FMOVD\tF.*, R.*" 159 // ppc64x:"MFVSRD" 160 // mips64/hardfloat:"MOVV\tF.*, R.*" 161 return math.Float64bits(f64+1) + 1 162 } 163 164 func fromFloat32(f32 float32) uint32 { 165 // amd64:"MOVL\tX.*, [^X].*" 166 // arm64:"FMOVS\tF.*, R.*" 167 // mips64/hardfloat:"MOVW\tF.*, R.*" 168 return math.Float32bits(f32+1) + 1 169 } 170 171 func toFloat64(u64 uint64) float64 { 172 // amd64:"MOVQ\t[^X].*, X.*" 173 // arm64:"FMOVD\tR.*, F.*" 174 // ppc64x:"MTVSRD" 175 // mips64/hardfloat:"MOVV\tR.*, F.*" 176 return math.Float64frombits(u64+1) + 1 177 } 178 179 func toFloat32(u32 uint32) float32 { 180 // amd64:"MOVL\t[^X].*, X.*" 181 // arm64:"FMOVS\tR.*, F.*" 182 // mips64/hardfloat:"MOVW\tR.*, F.*" 183 return math.Float32frombits(u32+1) + 1 184 } 185 186 // Test that comparisons with constants converted to float 187 // are evaluated at compile-time 188 189 func constantCheck64() bool { 190 // amd64:"(MOVB\t[$]0)|(XORL\t[A-Z][A-Z0-9]+, [A-Z][A-Z0-9]+)",-"FCMP",-"MOVB\t[$]1" 191 // s390x:"MOV(B|BZ|D)\t[$]0,",-"FCMPU",-"MOV(B|BZ|D)\t[$]1," 192 return 0.5 == float64(uint32(1)) || 1.5 > float64(uint64(1<<63)) 193 } 194 195 func constantCheck32() bool { 196 // amd64:"MOV(B|L)\t[$]1",-"FCMP",-"MOV(B|L)\t[$]0" 197 // s390x:"MOV(B|BZ|D)\t[$]1,",-"FCMPU",-"MOV(B|BZ|D)\t[$]0," 198 return float32(0.5) <= float32(int64(1)) && float32(1.5) >= float32(int32(-1<<31)) 199 } 200 201 // Test that integer constants are converted to floating point constants 202 // at compile-time 203 204 func constantConvert32(x float32) float32 { 205 // amd64:"MOVSS\t[$]f32.3f800000\\(SB\\)" 206 // s390x:"FMOVS\t[$]f32.3f800000\\(SB\\)" 207 // ppc64x/power8:"FMOVS\t[$]f32.3f800000\\(SB\\)" 208 // ppc64x/power9:"FMOVS\t[$]f32.3f800000\\(SB\\)" 209 // ppc64x/power10:"XXSPLTIDP\t[$]1065353216, VS0" 210 // arm64:"FMOVS\t[$]\\(1.0\\)" 211 if x > math.Float32frombits(0x3f800000) { 212 return -x 213 } 214 return x 215 } 216 217 func constantConvertInt32(x uint32) uint32 { 218 // amd64:-"MOVSS" 219 // s390x:-"FMOVS" 220 // ppc64x:-"FMOVS" 221 // arm64:-"FMOVS" 222 if x > math.Float32bits(1) { 223 return -x 224 } 225 return x 226 } 227 228 func nanGenerate64() float64 { 229 // Test to make sure we don't generate a NaN while constant propagating. 230 // See issue 36400. 231 zero := 0.0 232 // amd64:-"DIVSD" 233 inf := 1 / zero // +inf. We can constant propagate this one. 234 negone := -1.0 235 236 // amd64:"DIVSD" 237 z0 := zero / zero 238 // amd64:"MULSD" 239 z1 := zero * inf 240 // amd64:"SQRTSD" 241 z2 := math.Sqrt(negone) 242 return z0 + z1 + z2 243 } 244 245 func nanGenerate32() float32 { 246 zero := float32(0.0) 247 // amd64:-"DIVSS" 248 inf := 1 / zero // +inf. We can constant propagate this one. 249 250 // amd64:"DIVSS" 251 z0 := zero / zero 252 // amd64:"MULSS" 253 z1 := zero * inf 254 return z0 + z1 255 } 256