Geant4
11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4Exp.hh
Go to the documentation of this file.
1
//
2
// ********************************************************************
3
// * License and Disclaimer *
4
// * *
5
// * The Geant4 software is copyright of the Copyright Holders of *
6
// * the Geant4 Collaboration. It is provided under the terms and *
7
// * conditions of the Geant4 Software License, included in the file *
8
// * LICENSE and available at http://cern.ch/geant4/license . These *
9
// * include a list of copyright holders. *
10
// * *
11
// * Neither the authors of this software system, nor their employing *
12
// * institutes,nor the agencies providing financial support for this *
13
// * work make any representation or warranty, express or implied, *
14
// * regarding this software system or assume any liability for its *
15
// * use. Please see the license in the file LICENSE and URL above *
16
// * for the full disclaimer and the limitation of liability. *
17
// * *
18
// * This code implementation is the result of the scientific and *
19
// * technical work of the GEANT4 collaboration. *
20
// * By using, copying, modifying or distributing the software (or *
21
// * any work based on the software) you agree to acknowledge its *
22
// * use in resulting scientific publications, and indicate your *
23
// * acceptance of all terms of the Geant4 Software license. *
24
// ********************************************************************
25
//
26
// G4Exp
27
//
28
// Class description:
29
//
30
// The basic idea is to exploit Pade polynomials.
31
// A lot of ideas were inspired by the cephes math library
32
// (by Stephen L. Moshier moshier@na-net.ornl.gov) as well as actual code.
33
// The Cephes library can be found here: http://www.netlib.org/cephes/
34
// Code and algorithms for G4Exp have been extracted and adapted for Geant4
35
// from the original implementation in the VDT mathematical library
36
// (https://svnweb.cern.ch/trac/vdt), version 0.3.7.
37
38
// Original implementation created on: Jun 23, 2012
39
// Authors: Danilo Piparo, Thomas Hauth, Vincenzo Innocente
40
//
41
// --------------------------------------------------------------------
42
/*
43
* VDT is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser Public License as published by
45
* the Free Software Foundation, either version 3 of the License, or
46
* (at your option) any later version.
47
*
48
* This program is distributed in the hope that it will be useful,
49
* but WITHOUT ANY WARRANTY; without even the implied warranty of
50
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51
* GNU Lesser Public License for more details.
52
*
53
* You should have received a copy of the GNU Lesser Public License
54
* along with this program. If not, see <http://www.gnu.org/licenses/>.
55
*/
56
// --------------------------------------------------------------------
57
#ifndef G4Exp_hh
58
#define G4Exp_hh 1
59
60
#ifdef WIN32
61
62
# define G4Exp std::exp
63
64
#else
65
66
# include "
G4Types.hh
"
67
# include "
G4IEEE754.hh
"
68
# include <cstdint>
69
# include <limits>
70
71
namespace
G4ExpConsts
72
{
73
const
G4double
EXP_LIMIT
= 708;
74
75
const
G4double
PX1exp
= 1.26177193074810590878E-4;
76
const
G4double
PX2exp
= 3.02994407707441961300E-2;
77
const
G4double
PX3exp
= 9.99999999999999999910E-1;
78
const
G4double
QX1exp
= 3.00198505138664455042E-6;
79
const
G4double
QX2exp
= 2.52448340349684104192E-3;
80
const
G4double
QX3exp
= 2.27265548208155028766E-1;
81
const
G4double
QX4exp
= 2.00000000000000000009E0;
82
83
const
G4double
LOG2E
= 1.4426950408889634073599;
// 1/log(2)
84
85
const
G4float
MAXLOGF
= 88.72283905206835f;
86
const
G4float
MINLOGF
= -88.f;
87
88
const
G4float
C1F
= 0.693359375f;
89
const
G4float
C2F
= -2.12194440e-4f;
90
91
const
G4float
PX1expf
= 1.9875691500E-4f;
92
const
G4float
PX2expf
= 1.3981999507E-3f;
93
const
G4float
PX3expf
= 8.3334519073E-3f;
94
const
G4float
PX4expf
= 4.1665795894E-2f;
95
const
G4float
PX5expf
= 1.6666665459E-1f;
96
const
G4float
PX6expf
= 5.0000001201E-1f;
97
98
const
G4float
LOG2EF
= 1.44269504088896341f;
99
100
//----------------------------------------------------------------------------
101
/**
102
* A vectorisable floor implementation, not only triggered by fast-math.
103
* These functions do not distinguish between -0.0 and 0.0, so are not IEC6509
104
* compliant for argument -0.0
105
**/
106
inline
G4double
fpfloor
(
const
G4double
x)
107
{
108
// no problem since exp is defined between -708 and 708. Int is enough for
109
// it!
110
int32_t ret = int32_t(x);
111
ret -= (
G4IEEE754::sp2uint32
(x) >> 31);
112
return
ret;
113
}
114
115
//----------------------------------------------------------------------------
116
/**
117
* A vectorisable floor implementation, not only triggered by fast-math.
118
* These functions do not distinguish between -0.0 and 0.0, so are not IEC6509
119
* compliant for argument -0.0
120
**/
121
inline
G4float
fpfloor
(
const
G4float
x)
122
{
123
int32_t ret = int32_t(x);
124
ret -= (
G4IEEE754::sp2uint32
(x) >> 31);
125
return
ret;
126
}
127
}
// namespace G4ExpConsts
128
129
// Exp double precision --------------------------------------------------------
130
131
/// Exponential Function double precision
132
inline
G4double
G4Exp
(
G4double
initial_x)
133
{
134
G4double
x = initial_x;
135
G4double
px =
G4ExpConsts::fpfloor
(
G4ExpConsts::LOG2E
* x + 0.5);
136
137
const
int32_t n = int32_t(px);
138
139
x -= px * 6.93145751953125E-1;
140
x -= px * 1.42860682030941723212E-6;
141
142
const
G4double
xx = x * x;
143
144
// px = x * P(x**2).
145
px =
G4ExpConsts::PX1exp
;
146
px *= xx;
147
px +=
G4ExpConsts::PX2exp
;
148
px *= xx;
149
px +=
G4ExpConsts::PX3exp
;
150
px *= x;
151
152
// Evaluate Q(x**2).
153
G4double
qx =
G4ExpConsts::QX1exp
;
154
qx *= xx;
155
qx +=
G4ExpConsts::QX2exp
;
156
qx *= xx;
157
qx +=
G4ExpConsts::QX3exp
;
158
qx *= xx;
159
qx +=
G4ExpConsts::QX4exp
;
160
161
// e**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
162
x = px / (qx - px);
163
x = 1.0 + 2.0 * x;
164
165
// Build 2^n in double.
166
x *=
G4IEEE754::uint642dp
((((uint64_t) n) + 1023) << 52);
167
168
if
(initial_x >
G4ExpConsts::EXP_LIMIT
)
169
x = std::numeric_limits<G4double>::infinity();
170
if
(initial_x < -
G4ExpConsts::EXP_LIMIT
)
171
x = 0.;
172
173
return
x;
174
}
175
176
// Exp single precision --------------------------------------------------------
177
178
/// Exponential Function single precision
179
inline
G4float
G4Expf
(
G4float
initial_x)
180
{
181
G4float
x = initial_x;
182
183
G4float
z =
184
G4ExpConsts::fpfloor
(
G4ExpConsts::LOG2EF
* x +
185
0.5f);
/* std::floor() truncates toward -infinity. */
186
187
x -= z *
G4ExpConsts::C1F
;
188
x -= z *
G4ExpConsts::C2F
;
189
const
int32_t n = int32_t(z);
190
191
const
G4float
x2 = x * x;
192
193
z = x *
G4ExpConsts::PX1expf
;
194
z +=
G4ExpConsts::PX2expf
;
195
z *= x;
196
z +=
G4ExpConsts::PX3expf
;
197
z *= x;
198
z +=
G4ExpConsts::PX4expf
;
199
z *= x;
200
z +=
G4ExpConsts::PX5expf
;
201
z *= x;
202
z +=
G4ExpConsts::PX6expf
;
203
z *= x2;
204
z += x + 1.0f;
205
206
/* multiply by power of 2 */
207
z *=
G4IEEE754::uint322sp
((n + 0x7f) << 23);
208
209
if
(initial_x >
G4ExpConsts::MAXLOGF
)
210
z = std::numeric_limits<G4float>::infinity();
211
if
(initial_x <
G4ExpConsts::MINLOGF
)
212
z = 0.f;
213
214
return
z;
215
}
216
217
#endif
/* WIN32 */
218
219
#endif
G4Exp
G4double G4Exp(G4double initial_x)
Exponential Function double precision.
Definition
G4Exp.hh:132
G4Expf
G4float G4Expf(G4float initial_x)
Exponential Function single precision.
Definition
G4Exp.hh:179
G4IEEE754.hh
G4Types.hh
G4float
float G4float
Definition
G4Types.hh:84
G4double
double G4double
Definition
G4Types.hh:83
G4ExpConsts
Definition
G4Exp.hh:72
G4ExpConsts::QX3exp
const G4double QX3exp
Definition
G4Exp.hh:80
G4ExpConsts::C1F
const G4float C1F
Definition
G4Exp.hh:88
G4ExpConsts::QX4exp
const G4double QX4exp
Definition
G4Exp.hh:81
G4ExpConsts::PX1expf
const G4float PX1expf
Definition
G4Exp.hh:91
G4ExpConsts::PX3exp
const G4double PX3exp
Definition
G4Exp.hh:77
G4ExpConsts::LOG2EF
const G4float LOG2EF
Definition
G4Exp.hh:98
G4ExpConsts::EXP_LIMIT
const G4double EXP_LIMIT
Definition
G4Exp.hh:73
G4ExpConsts::fpfloor
G4double fpfloor(const G4double x)
Definition
G4Exp.hh:106
G4ExpConsts::PX6expf
const G4float PX6expf
Definition
G4Exp.hh:96
G4ExpConsts::PX1exp
const G4double PX1exp
Definition
G4Exp.hh:75
G4ExpConsts::QX1exp
const G4double QX1exp
Definition
G4Exp.hh:78
G4ExpConsts::PX4expf
const G4float PX4expf
Definition
G4Exp.hh:94
G4ExpConsts::MINLOGF
const G4float MINLOGF
Definition
G4Exp.hh:86
G4ExpConsts::PX3expf
const G4float PX3expf
Definition
G4Exp.hh:93
G4ExpConsts::MAXLOGF
const G4float MAXLOGF
Definition
G4Exp.hh:85
G4ExpConsts::PX5expf
const G4float PX5expf
Definition
G4Exp.hh:95
G4ExpConsts::PX2exp
const G4double PX2exp
Definition
G4Exp.hh:76
G4ExpConsts::LOG2E
const G4double LOG2E
Definition
G4Exp.hh:83
G4ExpConsts::PX2expf
const G4float PX2expf
Definition
G4Exp.hh:92
G4ExpConsts::QX2exp
const G4double QX2exp
Definition
G4Exp.hh:79
G4ExpConsts::C2F
const G4float C2F
Definition
G4Exp.hh:89
G4IEEE754::uint642dp
G4double uint642dp(uint64_t ll)
Definition
G4IEEE754.hh:66
G4IEEE754::sp2uint32
uint32_t sp2uint32(G4float x)
Definition
G4IEEE754.hh:86
G4IEEE754::uint322sp
G4float uint322sp(G4int x)
Definition
G4IEEE754.hh:76
geant4-v11.4.0
source
global
management
include
G4Exp.hh
Generated by
1.16.1