Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2989_MovingPeaksBenchmark/HeuristicLab.ExtLibs/HeuristicLab.NativeInterpreter/0.1/NativeInterpreter-0.1/lib/vdt/inv.h @ 17717

Last change on this file since 17717 was 16269, checked in by bburlacu, 6 years ago

#2958: Add C++ source code

File size: 3.2 KB
Line 
1/*
2 * inv.h
3 * An experiment: implement division with the square fo the approximate
4 * inverse square root.
5 * In other words one transforms a shift, multiplications and sums into a
6 * sqrt.
7 *
8 *  Created on: Jun 24, 2012
9 *      Author: Danilo Piparo, Thomas Hauth, Vincenzo Innocente
10 *
11 * VDT is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25#ifndef INV_H_
26#define INV_H_
27
28#include "vdtcore_common.h"
29#include "sqrt.h"
30#include <cmath>
31#include <limits>
32
33namespace vdt{
34
35//------------------------------------------------------------------------------
36
37/// General implementation of the inversion
38inline double fast_inv_general(double x, const uint32_t isqrt_iterations) { 
39  const uint64_t sign_mask = details::getSignMask(x);
40  const double sqrt_one_over_x = fast_isqrt_general(std::fabs(x),
41                                                   isqrt_iterations);
42  return sqrt_one_over_x*(details::dpORuint64(sqrt_one_over_x , sign_mask ));
43}
44
45//------------------------------------------------------------------------------
46
47/// Four iterations inversion
48inline double fast_inv(double x) {return fast_inv_general(x,4);}
49
50//------------------------------------------------------------------------------
51
52/// Three iterations
53inline double fast_approx_inv(double x) {return fast_inv_general(x,3);}
54
55//------------------------------------------------------------------------------
56
57/// For comparisons
58inline double inv (double x) {return 1./x;}
59
60//------------------------------------------------------------------------------
61// Single precision         
62
63
64
65/// General implementation of the inversion
66inline float fast_invf_general(float x, const uint32_t isqrt_iterations) {
67  const uint32_t sign_mask = details::getSignMask(x);
68  const float sqrt_one_over_x = fast_isqrtf_general(std::fabs(x),
69                                                   isqrt_iterations);
70  return sqrt_one_over_x*(details::spORuint32(sqrt_one_over_x , sign_mask ));
71}
72
73//------------------------------------------------------------------------------
74
75/// Two iterations
76inline float fast_invf(float x) {return fast_invf_general(x,2);}
77
78//------------------------------------------------------------------------------
79
80/// One iterations
81inline float fast_approx_invf(float x) {return fast_invf_general(x,1);}
82
83//------------------------------------------------------------------------------
84
85/// For comparisons
86inline float invf (float x) {return 1.f/x;}
87
88//------------------------------------------------------------------------------
89
90} // end namespace vdt
91
92#endif /* INV_H_ */
Note: See TracBrowser for help on using the repository browser.