Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:49

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file corecel/math/NumericLimits.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <cfloat>
0011 #include <climits>
0012 
0013 #include "corecel/Macros.hh"
0014 
0015 namespace celeritas
0016 {
0017 #define SCCEF_ static CELER_CONSTEXPR_FUNCTION
0018 
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Subset of numeric limits compatible with both host and device.
0022  *
0023  * \note \c CUDART_NAN and \c CUDART_INF are not \c constexpr in CUDA 10 at
0024  *   least, so we have replaced those with compiler built-ins that work in GCC,
0025  *   Clang, and MSVC.
0026  */
0027 template<class Numeric>
0028 struct numeric_limits;
0029 
0030 template<>
0031 struct numeric_limits<float>
0032 {
0033     SCCEF_ float epsilon() { return FLT_EPSILON; }
0034     SCCEF_ float lowest() { return -FLT_MAX; }
0035     SCCEF_ float min() { return FLT_MIN; }
0036     SCCEF_ float max() { return FLT_MAX; }
0037     SCCEF_ float quiet_NaN() { return __builtin_nanf(""); }
0038     SCCEF_ float infinity() { return __builtin_huge_valf(); }
0039 };
0040 
0041 template<>
0042 struct numeric_limits<double>
0043 {
0044     SCCEF_ double epsilon() { return DBL_EPSILON; }
0045     SCCEF_ double lowest() { return -DBL_MAX; }
0046     SCCEF_ double min() { return DBL_MIN; }
0047     SCCEF_ double max() { return DBL_MAX; }
0048     SCCEF_ double quiet_NaN() { return __builtin_nan(""); }
0049     SCCEF_ double infinity() { return __builtin_huge_val(); }
0050 };
0051 
0052 template<>
0053 struct numeric_limits<int>
0054 {
0055     SCCEF_ int lowest() { return INT_MIN; }
0056     SCCEF_ int min() { return INT_MIN; }
0057     SCCEF_ int max() { return INT_MAX; }
0058 };
0059 
0060 template<>
0061 struct numeric_limits<long>
0062 {
0063     SCCEF_ long lowest() { return LONG_MIN; }
0064     SCCEF_ long min() { return LONG_MIN; }
0065     SCCEF_ long max() { return LONG_MAX; }
0066 };
0067 
0068 template<>
0069 struct numeric_limits<long long>
0070 {
0071     SCCEF_ long long lowest() { return LLONG_MIN; }
0072     SCCEF_ long long min() { return LLONG_MIN; }
0073     SCCEF_ long long max() { return LLONG_MAX; }
0074 };
0075 
0076 template<>
0077 struct numeric_limits<unsigned int>
0078 {
0079     SCCEF_ unsigned int lowest() { return 0; }
0080     SCCEF_ unsigned int min() { return 0; }
0081     SCCEF_ unsigned int max() { return UINT_MAX; }
0082 };
0083 
0084 template<>
0085 struct numeric_limits<unsigned long>
0086 {
0087     SCCEF_ unsigned long lowest() { return 0; }
0088     SCCEF_ unsigned long min() { return 0; }
0089     SCCEF_ unsigned long max() { return ULONG_MAX; }
0090 };
0091 
0092 template<>
0093 struct numeric_limits<unsigned long long>
0094 {
0095     SCCEF_ unsigned long long lowest() { return 0; }
0096     SCCEF_ unsigned long long min() { return 0; }
0097     SCCEF_ unsigned long long max() { return ULLONG_MAX; }
0098 };
0099 
0100 #undef SCCEF_
0101 }  // namespace celeritas