-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: trying out cbrt as a test math function for specialization with …
…generics fallback
- Loading branch information
1 parent
70e83d4
commit 22243a9
Showing
8 changed files
with
164 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
// cbrt.hpp: generic implementation of a cubic root of a Real | ||
// | ||
// Copyright (C) 2017 Stillwater Supercomputing, Inc. | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// This file is part of the universal numbers project, which is released under an MIT Open Source license. | ||
|
||
namespace sw::universal { | ||
|
||
template<typename Real> | ||
Real cbrt(const Real& x) { | ||
assert(x >= Real(0)); | ||
return std::cbrt(x); | ||
} | ||
|
||
}} // namespace sw::function | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#pragma once | ||
// cbrt.hpp: cbrt function for double-double floating-point | ||
// | ||
// Copyright (C) 2017 Stillwater Supercomputing, Inc. | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// This file is part of the universal numbers project, which is released under an MIT Open Source license. | ||
|
||
namespace sw { namespace universal { | ||
|
||
/// <summary> | ||
/// cbrt is cube root function, that is, x^1/3 | ||
/// </summary> | ||
/// <param name="a">input</param> | ||
/// <returns>cube root of a</returns> | ||
inline dd cbrt(const dd& a) { | ||
using std::pow; | ||
if (!a.isfinite() || a.iszero()) | ||
return a; // NaN returns NaN; +/-Inf returns +/-Inf, +/-0.0 returns +/-0.0 | ||
|
||
bool signA = signbit(a); | ||
int e; // 0.5 <= r < 1.0 | ||
dd r = frexp(abs(a), &e); | ||
while (e % 3 != 0) { | ||
++e; | ||
r = ldexp(r, -1); | ||
} | ||
|
||
// at this point, 0.125 <= r < 1.0 | ||
dd x = pow(r.high(), -dd_third.high()); | ||
|
||
// refine estimate using Newton's iteration | ||
x += x * (1.0 - r * sqr(x) * x) * dd_third; | ||
x += x * (1.0 - r * sqr(x) * x) * dd_third; | ||
x = reciprocal(x); | ||
|
||
if (signA) | ||
x = -x; | ||
|
||
return ldexp(x, e / 3); | ||
} | ||
|
||
}} // namespace sw::universal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters