diff --git a/src/runtime/complex.go b/src/runtime/complex.go index 73f1161a50d..07c596fc0bb 100644 --- a/src/runtime/complex.go +++ b/src/runtime/complex.go @@ -4,68 +4,58 @@ package runtime -func isposinf(f float64) bool { return f > maxFloat64 } -func isneginf(f float64) bool { return f < -maxFloat64 } -func isnan(f float64) bool { return f != f } - -func nan() float64 { - var f float64 = 0 - return f / f +// inf2one returns a signed 1 if f is an infinity and a signed 0 otherwise. +// The sign of the result is the sign of f. +func inf2one(f float64) float64 { + g := 0.0 + if isInf(f) { + g = 1.0 + } + return copysign(g, f) } -func posinf() float64 { - var f float64 = maxFloat64 - return f * f -} +func complex128div(n complex128, m complex128) complex128 { + var e, f float64 // complex(e, f) = n/m -func neginf() float64 { - var f float64 = maxFloat64 - return -f * f -} + // Algorithm for robust complex division as described in + // Robert L. Smith: Algorithm 116: Complex division. Commun. ACM 5(8): 435 (1962). + if abs(real(m)) >= abs(imag(m)) { + ratio := imag(m) / real(m) + denom := real(m) + ratio*imag(m) + e = (real(n) + imag(n)*ratio) / denom + f = (imag(n) - real(n)*ratio) / denom + } else { + ratio := real(m) / imag(m) + denom := imag(m) + ratio*real(m) + e = (real(n)*ratio + imag(n)) / denom + f = (imag(n)*ratio - real(n)) / denom + } -func complex128div(n complex128, d complex128) complex128 { - // Special cases as in C99. - ninf := isposinf(real(n)) || isneginf(real(n)) || - isposinf(imag(n)) || isneginf(imag(n)) - dinf := isposinf(real(d)) || isneginf(real(d)) || - isposinf(imag(d)) || isneginf(imag(d)) + if isNaN(e) && isNaN(f) { + // Correct final result to infinities and zeros if applicable. + // Matches C99: ISO/IEC 9899:1999 - G.5.1 Multiplicative operators. - nnan := !ninf && (isnan(real(n)) || isnan(imag(n))) - dnan := !dinf && (isnan(real(d)) || isnan(imag(d))) + a, b := real(n), imag(n) + c, d := real(m), imag(m) - switch { - case nnan || dnan: - return complex(nan(), nan()) - case ninf && !dinf: - return complex(posinf(), posinf()) - case !ninf && dinf: - return complex(0, 0) - case real(d) == 0 && imag(d) == 0: - if real(n) == 0 && imag(n) == 0 { - return complex(nan(), nan()) - } else { - return complex(posinf(), posinf()) - } - default: - // Standard complex arithmetic, factored to avoid unnecessary overflow. - a := real(d) - if a < 0 { - a = -a - } - b := imag(d) - if b < 0 { - b = -b - } - if a <= b { - ratio := real(d) / imag(d) - denom := real(d)*ratio + imag(d) - return complex((real(n)*ratio+imag(n))/denom, - (imag(n)*ratio-real(n))/denom) - } else { - ratio := imag(d) / real(d) - denom := imag(d)*ratio + real(d) - return complex((imag(n)*ratio+real(n))/denom, - (imag(n)-real(n)*ratio)/denom) + switch { + case m == 0 && (!isNaN(a) || !isNaN(b)): + e = copysign(inf, c) * a + f = copysign(inf, c) * b + + case (isInf(a) || isInf(b)) && isFinite(c) && isFinite(d): + a = inf2one(a) + b = inf2one(b) + e = inf * (a*c + b*d) + f = inf * (b*c - a*d) + + case (isInf(c) || isInf(d)) && isFinite(a) && isFinite(b): + c = inf2one(c) + d = inf2one(d) + e = 0 * (a*c + b*d) + f = 0 * (b*c - a*d) } } + + return complex(e, f) } diff --git a/src/runtime/fastlog2.go b/src/runtime/fastlog2.go index 5f3fb534232..1f251bfaab2 100644 --- a/src/runtime/fastlog2.go +++ b/src/runtime/fastlog2.go @@ -4,8 +4,6 @@ package runtime -import "unsafe" - // fastlog2 implements a fast approximation to the base 2 log of a // float64. This is used to compute a geometric distribution for heap // sampling, without introducing dependencies into package math. This @@ -27,7 +25,3 @@ func fastlog2(x float64) float64 { low, high := fastlog2Table[xManIndex], fastlog2Table[xManIndex+1] return float64(xExp) + low + (high-low)*float64(xManScale)*fastlogScaleRatio } - -// float64bits returns the IEEE 754 binary representation of f. -// Taken from math.Float64bits to avoid dependencies into package math. -func float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) } diff --git a/src/runtime/float.go b/src/runtime/float.go new file mode 100644 index 00000000000..459e58dd7ef --- /dev/null +++ b/src/runtime/float.go @@ -0,0 +1,53 @@ +// Copyright 2017 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. + +package runtime + +import "unsafe" + +var inf = float64frombits(0x7FF0000000000000) + +// isNaN reports whether f is an IEEE 754 ``not-a-number'' value. +func isNaN(f float64) (is bool) { + // IEEE 754 says that only NaNs satisfy f != f. + return f != f +} + +// isFinite reports whether f is neither NaN nor an infinity. +func isFinite(f float64) bool { + return !isNaN(f - f) +} + +// isInf reports whether f is an infinity. +func isInf(f float64) bool { + return !isNaN(f) && !isFinite(f) +} + +// Abs returns the absolute value of x. +// +// Special cases are: +// Abs(±Inf) = +Inf +// Abs(NaN) = NaN +func abs(x float64) float64 { + const sign = 1 << 63 + return float64frombits(float64bits(x) &^ sign) +} + +// copysign returns a value with the magnitude +// of x and the sign of y. +func copysign(x, y float64) float64 { + const sign = 1 << 63 + return float64frombits(float64bits(x)&^sign | float64bits(y)&sign) +} + +// Float64bits returns the IEEE 754 binary representation of f. +func float64bits(f float64) uint64 { + return *(*uint64)(unsafe.Pointer(&f)) +} + +// Float64frombits returns the floating point number corresponding +// the IEEE 754 binary representation b. +func float64frombits(b uint64) float64 { + return *(*float64)(unsafe.Pointer(&b)) +} diff --git a/test/cmplxdivide.c b/test/cmplxdivide.c index a475cc24c97..89a2868b75b 100644 --- a/test/cmplxdivide.c +++ b/test/cmplxdivide.c @@ -23,50 +23,63 @@ #define nelem(x) (sizeof(x)/sizeof((x)[0])) double f[] = { - 0, - 1, - -1, - 2, + 0.0, + -0.0, + 1.0, + -1.0, + 2.0, NAN, INFINITY, -INFINITY, }; -char* -fmt(double g) -{ +char* fmt(double g) { static char buf[10][30]; static int n; char *p; - + p = buf[n++]; - if(n == 10) + if(n == 10) { n = 0; + } + sprintf(p, "%g", g); - if(strcmp(p, "-0") == 0) - strcpy(p, "negzero"); + + if(strcmp(p, "0") == 0) { + strcpy(p, "zero"); + return p; + } + + if(strcmp(p, "-0") == 0) { + strcpy(p, "-zero"); + return p; + } + return p; } -int -iscnan(double complex d) -{ - return !isinf(creal(d)) && !isinf(cimag(d)) && (isnan(creal(d)) || isnan(cimag(d))); -} - -double complex zero; // attempt to hide zero division from gcc - -int -main(void) -{ +int main(void) { int i, j, k, l; double complex n, d, q; - + printf("// skip\n"); printf("// # generated by cmplxdivide.c\n"); printf("\n"); printf("package main\n"); - printf("var tests = []Test{\n"); + printf("\n"); + printf("import \"math\"\n"); + printf("\n"); + printf("var (\n"); + printf("\tnan = math.NaN()\n"); + printf("\tinf = math.Inf(1)\n"); + printf("\tzero = 0.0\n"); + printf(")\n"); + printf("\n"); + printf("var tests = []struct {\n"); + printf("\tf, g complex128\n"); + printf("\tout complex128\n"); + printf("}{\n"); + for(i=0; i