Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SWIFTsim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SWIFT
SWIFTsim
Commits
b8bf8e7f
Commit
b8bf8e7f
authored
8 years ago
by
Matthieu Schaller
Browse files
Options
Downloads
Patches
Plain Diff
Added a note reagrding performance of the cbrtf() function in its documentation.
parent
5faa3c0f
No related branches found
No related tags found
1 merge request
!266
Cube root
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/cbrt.h
+21
-12
21 additions, 12 deletions
src/cbrt.h
with
21 additions
and
12 deletions
src/cbrt.h
+
21
−
12
View file @
b8bf8e7f
...
...
@@ -26,16 +26,25 @@
/* Some standard headers. */
#include
<math.h>
/* Local headers. */
#include
"inline.h"
/**
* @brief Compute the inverse cube root of a single-precision floating-point
* number.
*
* This function does not care about non-finite inputs.
*
* @warning This function is faster than both gcc and Intel's `cbrtf()`
* functions on x86 systems. However, Other compilers or other architectures
* may have faster implementations of the standard function `cbrtf()` that
* will potentionally outperform this function.
*
* @param x_in The input value.
*
* @return The inverse cubic root of @c x_in. Note that this function
* does not care about non-finite inputs.
* @return The inverse cubic root of @c x_in (i.e. \f$x_{in}^{-1/3} \f$) .
*/
__attribute__
((
always_inline
))
inline
float
icbrtf
(
float
x_in
)
{
__attribute__
((
always_inline
))
INLINE
static
float
icbrtf
(
float
x_in
)
{
union
{
float
as_float
;
...
...
@@ -43,26 +52,26 @@ __attribute__((always_inline)) inline float icbrtf(float x_in) {
int
as_int
;
}
cast
;
/
/
Extract the exponent.
/
*
Extract the exponent.
*/
cast
.
as_float
=
x_in
;
const
int
exponent
=
((
cast
.
as_int
&
0x7f800000
)
>>
23
)
-
127
;
/
/
Clear the exponent and sign to get the mantissa.
/
*
Clear the exponent and sign to get the mantissa.
*/
cast
.
as_uint
=
(
cast
.
as_uint
&
~
0xff800000
)
|
0x3f800000
;
const
float
x_norm
=
cast
.
as_float
;
/
/
Multiply by sqrt(1/2) and subtract one, should then be in the
//
range [sqrt(1/2) - 1, sqrt(2) - 1).
/
*
Multiply by sqrt(1/2) and subtract one, should then be in the
range [sqrt(1/2) - 1, sqrt(2) - 1).
*/
const
float
x
=
x_norm
*
(
float
)
M_SQRT1_2
-
1
.
0
f
;
/
/
Compute the polynomial interpolant.
/
*
Compute the polynomial interpolant.
*/
float
res
=
9.99976591940035e-01
f
+
x
*
(
-
3.32901212909283e-01
f
+
x
*
(
2.24361110929912e-01
f
+
x
*
(
-
1.88913279594895e-01
f
+
x
*
1.28384036492344e-01
f
)));
/
/
Compute the new exponent and the correction factor.
/
*
Compute the new exponent and the correction factor.
*/
int
exponent_new
=
exponent
;
if
(
exponent_new
<
0
)
exponent_new
-=
2
;
exponent_new
=
-
exponent_new
/
3
;
...
...
@@ -72,13 +81,13 @@ __attribute__((always_inline)) inline float icbrtf(float x_in) {
5.61231024154687e-01
f
};
const
float
exponent_scale
=
cast
.
as_float
*
scale
[
exponent_rem
];
/
/
Scale the result and set the correct sign.
/
*
Scale the result and set the correct sign.
*/
res
=
copysignf
(
res
*
exponent_scale
,
x_in
);
/
/
One step of Newton iteration to refine the result.
/
*
One step of Newton iteration to refine the result.
*/
res
*=
(
1
.
0
f
/
3
.
0
f
)
*
(
4
.
0
f
-
x_in
*
res
*
res
*
res
);
/
/
We're done.
/
*
We're done.
*/
return
res
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment