authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-17 00:56:14-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-17 00:56:14-04:00
logb73307befb49dd3b131d99ecf1c7f3fb54578ec8
tree259f4eccb92739e8f420a704ff3b49e7b0c1762f
parent288fc3a8d361972daeded19d207b410128d70d67

more std lib to postfix deref with zig fmt


2 files changed, 18 insertions(+), 27 deletions(-)

std/math/complex/exp.zig+11-17
...@@ -19,8 +19,8 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) {...@@ -19,8 +19,8 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) {
19fn exp32(z: &const Complex(f32)) Complex(f32) {19fn exp32(z: &const Complex(f32)) Complex(f32) {
20 @setFloatMode(this, @import("builtin").FloatMode.Strict);20 @setFloatMode(this, @import("builtin").FloatMode.Strict);
2121
22 const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.7228395522 const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
23 const cexp_overflow = 0x43400074; // (max_exp - min_denom_exp) * ln223 const cexp_overflow = 0x43400074; // (max_exp - min_denom_exp) * ln2
2424
25 const x = z.re;25 const x = z.re;
26 const y = z.im;26 const y = z.im;
...@@ -41,12 +41,10 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {...@@ -41,12 +41,10 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
41 // cexp(finite|nan +- i inf|nan) = nan + i nan41 // cexp(finite|nan +- i inf|nan) = nan + i nan
42 if ((hx & 0x7fffffff) != 0x7f800000) {42 if ((hx & 0x7fffffff) != 0x7f800000) {
43 return Complex(f32).new(y - y, y - y);43 return Complex(f32).new(y - y, y - y);
44 }44 } // cexp(-inf +- i inf|nan) = 0 + i0
45 // cexp(-inf +- i inf|nan) = 0 + i0
46 else if (hx & 0x80000000 != 0) {45 else if (hx & 0x80000000 != 0) {
47 return Complex(f32).new(0, 0);46 return Complex(f32).new(0, 0);
48 }47 } // cexp(+inf +- i inf|nan) = inf + i nan
49 // cexp(+inf +- i inf|nan) = inf + i nan
50 else {48 else {
51 return Complex(f32).new(x, y - y);49 return Complex(f32).new(x, y - y);
52 }50 }
...@@ -55,8 +53,7 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {...@@ -55,8 +53,7 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
55 // 88.7 <= x <= 192 so must scale53 // 88.7 <= x <= 192 so must scale
56 if (hx >= exp_overflow and hx <= cexp_overflow) {54 if (hx >= exp_overflow and hx <= cexp_overflow) {
57 return ldexp_cexp(z, 0);55 return ldexp_cexp(z, 0);
58 }56 } // - x < exp_overflow => exp(x) won't overflow (common)
59 // - x < exp_overflow => exp(x) won't overflow (common)
60 // - x > cexp_overflow, so exp(x) * s overflows for s > 057 // - x > cexp_overflow, so exp(x) * s overflows for s > 0
61 // - x = +-inf58 // - x = +-inf
62 // - x = nan59 // - x = nan
...@@ -67,8 +64,8 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {...@@ -67,8 +64,8 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
67}64}
6865
69fn exp64(z: &const Complex(f64)) Complex(f64) {66fn exp64(z: &const Complex(f64)) Complex(f64) {
70 const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 71067 const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
71 const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln268 const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2
7269
73 const x = z.re;70 const x = z.re;
74 const y = z.im;71 const y = z.im;
...@@ -95,12 +92,10 @@ fn exp64(z: &const Complex(f64)) Complex(f64) {...@@ -95,12 +92,10 @@ fn exp64(z: &const Complex(f64)) Complex(f64) {
95 // cexp(finite|nan +- i inf|nan) = nan + i nan92 // cexp(finite|nan +- i inf|nan) = nan + i nan
96 if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) {93 if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) {
97 return Complex(f64).new(y - y, y - y);94 return Complex(f64).new(y - y, y - y);
98 }95 } // cexp(-inf +- i inf|nan) = 0 + i0
99 // cexp(-inf +- i inf|nan) = 0 + i0
100 else if (hx & 0x80000000 != 0) {96 else if (hx & 0x80000000 != 0) {
101 return Complex(f64).new(0, 0);97 return Complex(f64).new(0, 0);
102 }98 } // cexp(+inf +- i inf|nan) = inf + i nan
103 // cexp(+inf +- i inf|nan) = inf + i nan
104 else {99 else {
105 return Complex(f64).new(x, y - y);100 return Complex(f64).new(x, y - y);
106 }101 }
...@@ -109,9 +104,8 @@ fn exp64(z: &const Complex(f64)) Complex(f64) {...@@ -109,9 +104,8 @@ fn exp64(z: &const Complex(f64)) Complex(f64) {
109 // 709.7 <= x <= 1454.3 so must scale104 // 709.7 <= x <= 1454.3 so must scale
110 if (hx >= exp_overflow and hx <= cexp_overflow) {105 if (hx >= exp_overflow and hx <= cexp_overflow) {
111 const r = ldexp_cexp(z, 0);106 const r = ldexp_cexp(z, 0);
112 return *r;107 return r.*;
113 }108 } // - x < exp_overflow => exp(x) won't overflow (common)
114 // - x < exp_overflow => exp(x) won't overflow (common)
115 // - x > cexp_overflow, so exp(x) * s overflows for s > 0109 // - x > cexp_overflow, so exp(x) * s overflows for s > 0
116 // - x = +-inf110 // - x = +-inf
117 // - x = nan111 // - x = nan
std/math/complex/ldexp.zig+7-10
...@@ -15,12 +15,12 @@ pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) {...@@ -15,12 +15,12 @@ pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) {
15}15}
1616
17fn frexp_exp32(x: f32, expt: &i32) f32 {17fn frexp_exp32(x: f32, expt: &i32) f32 {
18 const k = 235; // reduction constant18 const k = 235; // reduction constant
19 const kln2 = 162.88958740; // k * ln219 const kln2 = 162.88958740; // k * ln2
2020
21 const exp_x = math.exp(x - kln2);21 const exp_x = math.exp(x - kln2);
22 const hx = @bitCast(u32, exp_x);22 const hx = @bitCast(u32, exp_x);
23 *expt = i32(hx >> 23) - (0x7f + 127) + k;23 expt.* = i32(hx >> 23) - (0x7f + 127) + k;
24 return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));24 return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));
25}25}
2626
...@@ -35,15 +35,12 @@ fn ldexp_cexp32(z: &const Complex(f32), expt: i32) Complex(f32) {...@@ -35,15 +35,12 @@ fn ldexp_cexp32(z: &const Complex(f32), expt: i32) Complex(f32) {
35 const half_expt2 = exptf - half_expt1;35 const half_expt2 = exptf - half_expt1;
36 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);36 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
3737
38 return Complex(f32).new(38 return Complex(f32).new(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2);
39 math.cos(z.im) * exp_x * scale1 * scale2,
40 math.sin(z.im) * exp_x * scale1 * scale2,
41 );
42}39}
4340
44fn frexp_exp64(x: f64, expt: &i32) f64 {41fn frexp_exp64(x: f64, expt: &i32) f64 {
45 const k = 1799; // reduction constant42 const k = 1799; // reduction constant
46 const kln2 = 1246.97177782734161156; // k * ln243 const kln2 = 1246.97177782734161156; // k * ln2
4744
48 const exp_x = math.exp(x - kln2);45 const exp_x = math.exp(x - kln2);
4946
...@@ -51,7 +48,7 @@ fn frexp_exp64(x: f64, expt: &i32) f64 {...@@ -51,7 +48,7 @@ fn frexp_exp64(x: f64, expt: &i32) f64 {
51 const hx = u32(fx >> 32);48 const hx = u32(fx >> 32);
52 const lx = @truncate(u32, fx);49 const lx = @truncate(u32, fx);
5350
54 *expt = i32(hx >> 20) - (0x3ff + 1023) + k;51 expt.* = i32(hx >> 20) - (0x3ff + 1023) + k;
5552
56 const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);53 const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);
57 return @bitCast(f64, (u64(high_word) << 32) | lx);54 return @bitCast(f64, (u64(high_word) << 32) | lx);