Skip to content

Commit 27444a7

Browse files
committed
Initial commit
0 parents  commit 27444a7

31 files changed

+5322
-0
lines changed

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Copyright (c) 2012 The Go Author. All rights reserved.
2+
Copyright (c) 2015 The lux Authors. All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above
11+
copyright notice, this list of conditions and the following disclaimer
12+
in the documentation and/or other materials provided with the
13+
distribution.
14+
* Neither the name of the Google Inc., the Lux authors, nor the
15+
names of its contributors may be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# math
2+
This library is a copy of the standard math library but for float32 (more common in computer graphics and games. Most of the function are simply casting the arguments as `float64` and forwarding to the original math library (something you would have to do anyway, but slowly we are translating the original function to the `float32` version.
3+
ALL tests are passing, meaning whatever program you we're running using the 64 bit version should still be correct using this library (using the IEEE floating point definition of "correct").
4+
I only care about i386 and amd64, but if I receive pull request for other architecture I will merge them (I can't test it so please send me proof that all test pass and benchmark are better :)
5+
6+
## rewriten functions
7+
The benchmark is based on my macbook pro, with an i386 processor but a x86_64 operating system. benchmark may produce different results on different computers. However if you see an architecture for which casting to float64, calling the standard lib then casting back to flaot32 is faster then my implementation please tell me.
8+
✓ = Test passing.
9+
✗ = Not implemented yet.
10+
? = Implemented but not tested.
11+
\* = has no ASM implementation.
12+
13+
14+
|Abs|status|lux math|casting|std math|accel|
15+
|---|---|---:|---:|---:|---:|
16+
|function name|✓, ✗, ? or \* |benchmark for float32 using lux math package|benchmark for casting a var to float64, doing the op then casting back|benchmark for float64 using standard math package|accelleration between casting and lux math|
17+
18+
|Abs|status|lux math|casting|std math|accel|
19+
|---|---|---:|---:|---:|---:|
20+
|software||||||
21+
|amd64||2.10 ns/op|2.37 ns/op|2.08 ns/op|+13%|
22+
|386||2.02 ns/op|2.24 ns/op|2.02 ns/op|+11%|
23+
24+
|Sqrt|status|lux math|casting|std math|accel|
25+
|---|---|---:|---:|---:|---:|
26+
|software||||||
27+
|amd64||2.33 ns/op|4.70 ns/op|0.33 ns/op|+102%|
28+
|386||2.33 ns/op|5.14 ns/op|4.74 ns/op|+121%|
29+
30+
|Acosh|status|lux math|casting|std math|accel|
31+
|---|---|---:|---:|---:|---:|
32+
|software||43.5 ns/op|75.8 ns/op|40.3 ns/op|+74%|
33+
34+
|Asinh|status|lux math|casting|std math|accel|
35+
|---|---|---:|---:|---:|---:|
36+
|software||47.3 ns/op|81.3 ns/op|46.1 ns/op|+72%|
37+
38+
|Gamma|status|lux math|casting|std math|accel|
39+
|---|---|---:|---:|---:|---:|
40+
|software||25.1 ns/op|38.5 ns/op|24.0 ns/op|+53%|
41+
42+
43+
|template|status|lux math|casting|std math|accel|
44+
|---|---|---:|---:|---:|---:|
45+
|software|| ns/op| ns/op| ns/op|%|
46+
|amd64|| ns/op| ns/op| ns/op|%|
47+
|i386|| ns/op| ns/op| ns/op|%|
48+
49+

abs.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2009 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 math
6+
7+
// Abs returns the absolute value of x.
8+
//
9+
// Special cases are:
10+
// Abs(±Inf) = +Inf
11+
// Abs(NaN) = NaN
12+
func Abs(x float32) float32 {
13+
if x < 0 {
14+
return -x
15+
}
16+
if x == 0 {
17+
return 0 // return correctly abs(-0)
18+
}
19+
return x
20+
}
21+
22+
//func Abs(x float32) float32 //{ return float32(m.Abs(float64(x))) }
23+
24+
// Note: the software version of abs is actually faster then the ASM version,
25+
// due to some inlining that doesn't happen with ASM, so we're just gonna remove
26+
// it for now.

acosh.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 math
6+
7+
// The original C code, the long comment, and the constants
8+
// below are from FreeBSD's /usr/src/lib/msun/src/e_acosh.c
9+
// and came with this notice. The go code is a simplified
10+
// version of the original C.
11+
//
12+
// ====================================================
13+
// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
14+
//
15+
// Developed at SunPro, a Sun Microsystems, Inc. business.
16+
// Permission to use, copy, modify, and distribute this
17+
// software is freely granted, provided that this notice
18+
// is preserved.
19+
// ====================================================
20+
//
21+
//
22+
// __ieee754_acosh(x)
23+
// Method :
24+
// Based on
25+
// acosh(x) = log [ x + sqrt(x*x-1) ]
26+
// we have
27+
// acosh(x) := log(x)+ln2, if x is large; else
28+
// acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
29+
// acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
30+
//
31+
// Special cases:
32+
// acosh(x) is NaN with signal if x<1.
33+
// acosh(NaN) is NaN without signal.
34+
//
35+
36+
// Acosh returns the inverse hyperbolic cosine of x.
37+
//
38+
// Special cases are:
39+
// Acosh(+Inf) = +Inf
40+
// Acosh(x) = NaN if x < 1
41+
// Acosh(NaN) = NaN
42+
func Acosh(x float32) float32 {
43+
const (
44+
Ln2 = 6.93147180559945286227e-01 // 0x3FE62E42FEFA39EF
45+
Large = 1 << 28 // 2**28
46+
)
47+
// first case is special case
48+
switch {
49+
case x < 1 || IsNaN(x):
50+
return NaN()
51+
case x == 1:
52+
return 0
53+
case x >= Large:
54+
return Log(x) + Ln2 // x > 2**28
55+
case x > 2:
56+
return Log(2*x - 1/(x+Sqrt(x*x-1))) // 2**28 > x > 2
57+
}
58+
t := x - 1
59+
return Log1p(t + Sqrt(2*t+t*t)) // 2 >= x > 1
60+
}

0 commit comments

Comments
 (0)