Keith Randall | c403af8 | 2014-07-25 15:12:45 -0700 | [diff] [blame] | 1 | // Copyright 2010 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package runtime |
| 6 | |
Russ Cox | 0c3c2c1 | 2014-11-11 17:07:06 -0500 | [diff] [blame] | 7 | func isposinf(f float64) bool { return f > maxFloat64 } |
| 8 | func isneginf(f float64) bool { return f < -maxFloat64 } |
| 9 | func isnan(f float64) bool { return f != f } |
| 10 | |
| 11 | func nan() float64 { |
| 12 | var f float64 = 0 |
| 13 | return f / f |
| 14 | } |
| 15 | |
| 16 | func posinf() float64 { |
| 17 | var f float64 = maxFloat64 |
| 18 | return f * f |
| 19 | } |
| 20 | |
| 21 | func neginf() float64 { |
| 22 | var f float64 = maxFloat64 |
| 23 | return -f * f |
| 24 | } |
| 25 | |
Keith Randall | c403af8 | 2014-07-25 15:12:45 -0700 | [diff] [blame] | 26 | func complex128div(n complex128, d complex128) complex128 { |
| 27 | // Special cases as in C99. |
Russ Cox | 0c3c2c1 | 2014-11-11 17:07:06 -0500 | [diff] [blame] | 28 | ninf := isposinf(real(n)) || isneginf(real(n)) || |
| 29 | isposinf(imag(n)) || isneginf(imag(n)) |
| 30 | dinf := isposinf(real(d)) || isneginf(real(d)) || |
| 31 | isposinf(imag(d)) || isneginf(imag(d)) |
Keith Randall | c403af8 | 2014-07-25 15:12:45 -0700 | [diff] [blame] | 32 | |
Russ Cox | 0c3c2c1 | 2014-11-11 17:07:06 -0500 | [diff] [blame] | 33 | nnan := !ninf && (isnan(real(n)) || isnan(imag(n))) |
| 34 | dnan := !dinf && (isnan(real(d)) || isnan(imag(d))) |
Keith Randall | c403af8 | 2014-07-25 15:12:45 -0700 | [diff] [blame] | 35 | |
| 36 | switch { |
| 37 | case nnan || dnan: |
Russ Cox | 0c3c2c1 | 2014-11-11 17:07:06 -0500 | [diff] [blame] | 38 | return complex(nan(), nan()) |
Keith Randall | c403af8 | 2014-07-25 15:12:45 -0700 | [diff] [blame] | 39 | case ninf && !dinf: |
Russ Cox | 0c3c2c1 | 2014-11-11 17:07:06 -0500 | [diff] [blame] | 40 | return complex(posinf(), posinf()) |
Keith Randall | c403af8 | 2014-07-25 15:12:45 -0700 | [diff] [blame] | 41 | case !ninf && dinf: |
| 42 | return complex(0, 0) |
| 43 | case real(d) == 0 && imag(d) == 0: |
| 44 | if real(n) == 0 && imag(n) == 0 { |
Russ Cox | 0c3c2c1 | 2014-11-11 17:07:06 -0500 | [diff] [blame] | 45 | return complex(nan(), nan()) |
Keith Randall | c403af8 | 2014-07-25 15:12:45 -0700 | [diff] [blame] | 46 | } else { |
Russ Cox | 0c3c2c1 | 2014-11-11 17:07:06 -0500 | [diff] [blame] | 47 | return complex(posinf(), posinf()) |
Keith Randall | c403af8 | 2014-07-25 15:12:45 -0700 | [diff] [blame] | 48 | } |
| 49 | default: |
| 50 | // Standard complex arithmetic, factored to avoid unnecessary overflow. |
| 51 | a := real(d) |
| 52 | if a < 0 { |
| 53 | a = -a |
| 54 | } |
| 55 | b := imag(d) |
| 56 | if b < 0 { |
| 57 | b = -b |
| 58 | } |
| 59 | if a <= b { |
| 60 | ratio := real(d) / imag(d) |
| 61 | denom := real(d)*ratio + imag(d) |
| 62 | return complex((real(n)*ratio+imag(n))/denom, |
| 63 | (imag(n)*ratio-real(n))/denom) |
| 64 | } else { |
| 65 | ratio := imag(d) / real(d) |
| 66 | denom := imag(d)*ratio + real(d) |
| 67 | return complex((imag(n)*ratio+real(n))/denom, |
| 68 | (imag(n)-real(n)*ratio)/denom) |
| 69 | } |
| 70 | } |
| 71 | } |