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
7ac1e02e
Commit
7ac1e02e
authored
8 years ago
by
Pedro Gonnet
Browse files
Options
Downloads
Patches
Plain Diff
fixes to compile without warnings.
parent
e56c30e3
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!266
Cube root
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/cbrt.h
+79
-0
79 additions, 0 deletions
src/cbrt.h
with
79 additions
and
0 deletions
src/cbrt.h
0 → 100644
+
79
−
0
View file @
7ac1e02e
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (c) 2016 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
* Matthieu Schaller (matthieu.schaller@durham.ac.uk)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#ifndef SWIFT_CBRT_H
#define SWIFT_CBRT_H
/* Config parameters. */
#include
"../config.h"
/* Some standard headers. */
#include
<math.h>
/**
* @brief Compute the inverse cube root of a single-precision floating-point
* number.
*
* @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.
*/
__attribute__
((
always_inline
))
inline
float
icbrtf
(
float
x_in
)
{
// Extract the exponent.
const
unsigned
int
x_as_uint
=
*
((
char
*
)
&
x_in
);
const
int
exponent
=
((
x_as_uint
&
0x7f800000
)
>>
23
)
-
127
;
// Clear the exponent and sign to get the mantissa.
const
unsigned
int
x_norm_as_int
=
(
x_as_uint
&
~
0xff800000
)
|
0x3f800000
;
const
float
x_norm
=
*
((
char
*
)
&
x_norm_as_int
);
// 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.
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.
int
exponent_new
=
exponent
;
if
(
exponent_new
<
0
)
exponent_new
-=
2
;
exponent_new
=
-
exponent_new
/
3
;
const
int
exponent_rem
=
exponent
+
3
*
exponent_new
;
const
unsigned
int
exponent_scale_as_int
=
(
exponent_new
+
127
)
<<
23
;
float
exponent_scale
=
*
((
char
*
)
&
exponent_scale_as_int
);
exponent_scale
*=
exponent_rem
>
0
?
(
exponent_rem
>
1
?
5.61231024154687e-01
f
:
7.07106781186548e-01
f
)
:
8.90898718140339e-01
f
;
res
*=
exponent_scale
;
// 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.
return
copysignf
(
res
,
x_in
);
}
#endif
/* SWIFT_CBRT_H */
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