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)) {
1919fn exp32(z: &const Complex(f32)) Complex(f32) {
2020 @setFloatMode(this, @import("builtin").FloatMode.Strict);
2121
22 const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
23 const cexp_overflow = 0x43400074; // (max_exp - min_denom_exp) * ln2
22 const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
23 const cexp_overflow = 0x43400074; // (max_exp - min_denom_exp) * ln2
2424
2525 const x = z.re;
2626 const y = z.im;
......@@ -41,12 +41,10 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
4141 // cexp(finite|nan +- i inf|nan) = nan + i nan
4242 if ((hx & 0x7fffffff) != 0x7f800000) {
4343 return Complex(f32).new(y - y, y - y);
44 }
45 // cexp(-inf +- i inf|nan) = 0 + i0
44 } // cexp(-inf +- i inf|nan) = 0 + i0
4645 else if (hx & 0x80000000 != 0) {
4746 return Complex(f32).new(0, 0);
48 }
49 // cexp(+inf +- i inf|nan) = inf + i nan
47 } // cexp(+inf +- i inf|nan) = inf + i nan
5048 else {
5149 return Complex(f32).new(x, y - y);
5250 }
......@@ -55,8 +53,7 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
5553 // 88.7 <= x <= 192 so must scale
5654 if (hx >= exp_overflow and hx <= cexp_overflow) {
5755 return ldexp_cexp(z, 0);
58 }
59 // - x < exp_overflow => exp(x) won't overflow (common)
56 } // - x < exp_overflow => exp(x) won't overflow (common)
6057 // - x > cexp_overflow, so exp(x) * s overflows for s > 0
6158 // - x = +-inf
6259 // - x = nan
......@@ -67,8 +64,8 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
6764}
6865
6966fn exp64(z: &const Complex(f64)) Complex(f64) {
70 const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
71 const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2
67 const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
68 const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2
7269
7370 const x = z.re;
7471 const y = z.im;
......@@ -95,12 +92,10 @@ fn exp64(z: &const Complex(f64)) Complex(f64) {
9592 // cexp(finite|nan +- i inf|nan) = nan + i nan
9693 if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) {
9794 return Complex(f64).new(y - y, y - y);
98 }
99 // cexp(-inf +- i inf|nan) = 0 + i0
95 } // cexp(-inf +- i inf|nan) = 0 + i0
10096 else if (hx & 0x80000000 != 0) {
10197 return Complex(f64).new(0, 0);
102 }
103 // cexp(+inf +- i inf|nan) = inf + i nan
98 } // cexp(+inf +- i inf|nan) = inf + i nan
10499 else {
105100 return Complex(f64).new(x, y - y);
106101 }
......@@ -109,9 +104,8 @@ fn exp64(z: &const Complex(f64)) Complex(f64) {
109104 // 709.7 <= x <= 1454.3 so must scale
110105 if (hx >= exp_overflow and hx <= cexp_overflow) {
111106 const r = ldexp_cexp(z, 0);
112 return *r;
113 }
114 // - x < exp_overflow => exp(x) won't overflow (common)
107 return r.*;
108 } // - x < exp_overflow => exp(x) won't overflow (common)
115109 // - x > cexp_overflow, so exp(x) * s overflows for s > 0
116110 // - x = +-inf
117111 // - 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)) {
1515}
1616
1717fn frexp_exp32(x: f32, expt: &i32) f32 {
18 const k = 235; // reduction constant
19 const kln2 = 162.88958740; // k * ln2
18 const k = 235; // reduction constant
19 const kln2 = 162.88958740; // k * ln2
2020
2121 const exp_x = math.exp(x - kln2);
2222 const hx = @bitCast(u32, exp_x);
23 *expt = i32(hx >> 23) - (0x7f + 127) + k;
23 expt.* = i32(hx >> 23) - (0x7f + 127) + k;
2424 return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));
2525}
2626
......@@ -35,15 +35,12 @@ fn ldexp_cexp32(z: &const Complex(f32), expt: i32) Complex(f32) {
3535 const half_expt2 = exptf - half_expt1;
3636 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
3737
38 return Complex(f32).new(
39 math.cos(z.im) * exp_x * scale1 * scale2,
40 math.sin(z.im) * exp_x * scale1 * scale2,
41 );
38 return Complex(f32).new(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2);
4239}
4340
4441fn frexp_exp64(x: f64, expt: &i32) f64 {
45 const k = 1799; // reduction constant
46 const kln2 = 1246.97177782734161156; // k * ln2
42 const k = 1799; // reduction constant
43 const kln2 = 1246.97177782734161156; // k * ln2
4744
4845 const exp_x = math.exp(x - kln2);
4946
......@@ -51,7 +48,7 @@ fn frexp_exp64(x: f64, expt: &i32) f64 {
5148 const hx = u32(fx >> 32);
5249 const lx = @truncate(u32, fx);
5350
54 *expt = i32(hx >> 20) - (0x3ff + 1023) + k;
51 expt.* = i32(hx >> 20) - (0x3ff + 1023) + k;
5552
5653 const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);
5754 return @bitCast(f64, (u64(high_word) << 32) | lx);