|
|
|||
File indexing completed on 2026-07-13 08:21:04
0001 // 0002 // ******************************************************************** 0003 // * License and Disclaimer * 0004 // * * 0005 // * The Geant4 software is copyright of the Copyright Holders of * 0006 // * the Geant4 Collaboration. It is provided under the terms and * 0007 // * conditions of the Geant4 Software License, included in the file * 0008 // * LICENSE and available at http://cern.ch/geant4/license . These * 0009 // * include a list of copyright holders. * 0010 // * * 0011 // * Neither the authors of this software system, nor their employing * 0012 // * institutes,nor the agencies providing financial support for this * 0013 // * work make any representation or warranty, express or implied, * 0014 // * regarding this software system or assume any liability for its * 0015 // * use. Please see the license in the file LICENSE and URL above * 0016 // * for the full disclaimer and the limitation of liability. * 0017 // * * 0018 // * This code implementation is the result of the scientific and * 0019 // * technical work of the GEANT4 collaboration. * 0020 // * By using, copying, modifying or distributing the software (or * 0021 // * any work based on the software) you agree to acknowledge its * 0022 // * use in resulting scientific publications, and indicate your * 0023 // * acceptance of all terms of the Geant4 Software license. * 0024 // ******************************************************************** 0025 // 0026 // G4Exp 0027 // 0028 // Class description: 0029 // 0030 // The basic idea is to exploit Pade polynomials. 0031 // A lot of ideas were inspired by the cephes math library 0032 // (by Stephen L. Moshier moshier@na-net.ornl.gov) as well as actual code. 0033 // The Cephes library can be found here: http://www.netlib.org/cephes/ 0034 // Code and algorithms for G4Exp have been extracted and adapted for Geant4 0035 // from the original implementation in the VDT mathematical library 0036 // (https://svnweb.cern.ch/trac/vdt), version 0.3.7. 0037 0038 // Original implementation created on: Jun 23, 2012 0039 // Authors: Danilo Piparo, Thomas Hauth, Vincenzo Innocente 0040 // 0041 // -------------------------------------------------------------------- 0042 /* 0043 * VDT is free software: you can redistribute it and/or modify 0044 * it under the terms of the GNU Lesser Public License as published by 0045 * the Free Software Foundation, either version 3 of the License, or 0046 * (at your option) any later version. 0047 * 0048 * This program is distributed in the hope that it will be useful, 0049 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0050 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0051 * GNU Lesser Public License for more details. 0052 * 0053 * You should have received a copy of the GNU Lesser Public License 0054 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0055 */ 0056 // -------------------------------------------------------------------- 0057 #ifndef G4HepEmExp_HH 0058 #define G4HepEmExp_HH 1 0059 0060 /** 0061 * @file G4HepEmExp.hh 0062 * @author M. Novak 0063 * @date 2021 0064 * 0065 * @brief Just a copy of `G4Exp`, that's in fact a simplified version of VDT exp. 0066 * 0067 * That's why the function names are `VDTExp` and `VDTExpf` for double and float. 0068 * 0069 */ 0070 0071 #ifdef WIN32 0072 0073 #define VDTExp std::exp 0074 0075 #else // WIN32 0076 0077 #include <limits> 0078 #include <stdint.h> 0079 0080 namespace DVTExpConsts { 0081 const double EXP_LIMIT = 708; 0082 0083 const double PX1exp = 1.26177193074810590878E-4; 0084 const double PX2exp = 3.02994407707441961300E-2; 0085 const double PX3exp = 9.99999999999999999910E-1; 0086 const double QX1exp = 3.00198505138664455042E-6; 0087 const double QX2exp = 2.52448340349684104192E-3; 0088 const double QX3exp = 2.27265548208155028766E-1; 0089 const double QX4exp = 2.00000000000000000009E0; 0090 0091 const double LOG2E = 1.4426950408889634073599; // 1/log(2) 0092 0093 const float MAXLOGF = 88.72283905206835f; 0094 const float MINLOGF = -88.f; 0095 0096 const float C1F = 0.693359375f; 0097 const float C2F = -2.12194440e-4f; 0098 0099 const float PX1expf = 1.9875691500E-4f; 0100 const float PX2expf = 1.3981999507E-3f; 0101 const float PX3expf = 8.3334519073E-3f; 0102 const float PX4expf = 4.1665795894E-2f; 0103 const float PX5expf = 1.6666665459E-1f; 0104 const float PX6expf = 5.0000001201E-1f; 0105 0106 const float LOG2EF = 1.44269504088896341f; 0107 0108 //---------------------------------------------------------------------------- 0109 // Used to switch between different type of interpretations of the data 0110 // (64 bits) 0111 // 0112 union ieee754 { 0113 ieee754(){}; 0114 ieee754(double thed) { d = thed; }; 0115 ieee754(uint64_t thell) { ll = thell; }; 0116 ieee754(float thef) { f[0] = thef; }; 0117 ieee754(uint32_t thei) { i[0] = thei; }; 0118 double d; 0119 float f[2]; 0120 uint32_t i[2]; 0121 uint64_t ll; 0122 uint16_t s[4]; 0123 }; 0124 0125 //---------------------------------------------------------------------------- 0126 // Converts an unsigned long long to a double 0127 // 0128 inline double uint642dp(uint64_t ll) { 0129 ieee754 tmp; 0130 tmp.ll = ll; 0131 return tmp.d; 0132 } 0133 0134 //---------------------------------------------------------------------------- 0135 // Converts an int to a float 0136 // 0137 inline float uint322sp(int x) { 0138 ieee754 tmp; 0139 tmp.i[0] = x; 0140 return tmp.f[0]; 0141 } 0142 0143 //---------------------------------------------------------------------------- 0144 // Converts a float to an int 0145 // 0146 inline uint32_t sp2uint32(float x) { 0147 ieee754 tmp; 0148 tmp.f[0] = x; 0149 return tmp.i[0]; 0150 } 0151 0152 //---------------------------------------------------------------------------- 0153 /** 0154 * A vectorisable floor implementation, not only triggered by fast-math. 0155 * These functions do not distinguish between -0.0 and 0.0, so are not IEC6509 0156 * compliant for argument -0.0 0157 **/ 0158 inline double fpfloor(const double x) { 0159 // no problem since exp is defined between -708 and 708. Int is enough for 0160 // it! 0161 int32_t ret = int32_t(x); 0162 ret -= (sp2uint32(x) >> 31); 0163 return ret; 0164 } 0165 0166 //---------------------------------------------------------------------------- 0167 /** 0168 * A vectorisable floor implementation, not only triggered by fast-math. 0169 * These functions do not distinguish between -0.0 and 0.0, so are not IEC6509 0170 * compliant for argument -0.0 0171 **/ 0172 inline float fpfloor(const float x) { 0173 int32_t ret = int32_t(x); 0174 ret -= (sp2uint32(x) >> 31); 0175 return ret; 0176 } 0177 } // namespace DVTExpConsts 0178 0179 // Exp double precision -------------------------------------------------------- 0180 0181 /// Exponential Function double precision 0182 inline double VDTExp(double initial_x) { 0183 double x = initial_x; 0184 double px = DVTExpConsts::fpfloor(DVTExpConsts::LOG2E * x + 0.5); 0185 0186 const int32_t n = int32_t(px); 0187 0188 x -= px * 6.93145751953125E-1; 0189 x -= px * 1.42860682030941723212E-6; 0190 0191 const double xx = x * x; 0192 0193 // px = x * P(x**2). 0194 px = DVTExpConsts::PX1exp; 0195 px *= xx; 0196 px += DVTExpConsts::PX2exp; 0197 px *= xx; 0198 px += DVTExpConsts::PX3exp; 0199 px *= x; 0200 0201 // Evaluate Q(x**2). 0202 double qx = DVTExpConsts::QX1exp; 0203 qx *= xx; 0204 qx += DVTExpConsts::QX2exp; 0205 qx *= xx; 0206 qx += DVTExpConsts::QX3exp; 0207 qx *= xx; 0208 qx += DVTExpConsts::QX4exp; 0209 0210 // e**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) ) 0211 x = px / (qx - px); 0212 x = 1.0 + 2.0 * x; 0213 0214 // Build 2^n in double. 0215 x *= DVTExpConsts::uint642dp((((uint64_t) n) + 1023) << 52); 0216 0217 if(initial_x > DVTExpConsts::EXP_LIMIT) 0218 x = std::numeric_limits<double>::infinity(); 0219 if(initial_x < -DVTExpConsts::EXP_LIMIT) 0220 x = 0.; 0221 0222 return x; 0223 } 0224 0225 // Exp single precision -------------------------------------------------------- 0226 0227 /// Exponential Function single precision 0228 inline float VDTExpf(float initial_x) { 0229 float x = initial_x; 0230 float z = DVTExpConsts::fpfloor(DVTExpConsts::LOG2EF * x +0.5f); /* std::floor() truncates toward -infinity. */ 0231 0232 x -= z * DVTExpConsts::C1F; 0233 x -= z * DVTExpConsts::C2F; 0234 const int32_t n = int32_t(z); 0235 0236 const float x2 = x * x; 0237 0238 z = x * DVTExpConsts::PX1expf; 0239 z += DVTExpConsts::PX2expf; 0240 z *= x; 0241 z += DVTExpConsts::PX3expf; 0242 z *= x; 0243 z += DVTExpConsts::PX4expf; 0244 z *= x; 0245 z += DVTExpConsts::PX5expf; 0246 z *= x; 0247 z += DVTExpConsts::PX6expf; 0248 z *= x2; 0249 z += x + 1.0f; 0250 0251 /* multiply by power of 2 */ 0252 z *= DVTExpConsts::uint322sp((n + 0x7f) << 23); 0253 0254 if(initial_x > DVTExpConsts::MAXLOGF) 0255 z = std::numeric_limits<float>::infinity(); 0256 if(initial_x < DVTExpConsts::MINLOGF) 0257 z = 0.f; 0258 0259 return z; 0260 } 0261 0262 0263 //------------------------------------------------------------------------------ 0264 0265 void expv(const uint32_t size, double const* __restrict__ iarray, 0266 double* __restrict__ oarray); 0267 void expfv(const uint32_t size, float const* __restrict__ iarray, 0268 float* __restrict__ oarray); 0269 0270 #endif // WIN32 0271 0272 #endif // G4HepEmExp
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|