Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:41

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/math/NumericLimits.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cfloat>
0010 #include <climits>
0011 
0012 #include "corecel/Macros.hh"
0013 
0014 namespace celeritas
0015 {
0016 #define SCCEF_ static CELER_CONSTEXPR_FUNCTION
0017 
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Subset of numeric limits compatible with both host and device.
0021  *
0022  * \note \c CUDART_NAN and \c CUDART_INF are not \c constexpr in CUDA 10 at
0023  *   least, so we have replaced those with compiler built-ins that work in GCC,
0024  *   Clang, and MSVC.
0025  *
0026  * \deprecated The name of this class will change to NumericLimits to conform
0027  * to the Celeritas naming system, since it is not a complete replacement for
0028  * the \c std implementation.
0029  */
0030 template<class Numeric>
0031 struct numeric_limits;
0032 
0033 template<>
0034 struct numeric_limits<float>
0035 {
0036     SCCEF_ float epsilon() { return FLT_EPSILON; }
0037     SCCEF_ float lowest() { return -FLT_MAX; }
0038     SCCEF_ float min() { return FLT_MIN; }
0039     SCCEF_ float max() { return FLT_MAX; }
0040     SCCEF_ float quiet_NaN() { return __builtin_nanf(""); }
0041     SCCEF_ float infinity() { return __builtin_huge_valf(); }
0042 };
0043 
0044 template<>
0045 struct numeric_limits<double>
0046 {
0047     //! Relative difference between 1 and adjacent floating point number
0048     SCCEF_ double epsilon() { return DBL_EPSILON; }
0049     //! Most negative finite value
0050     SCCEF_ double lowest() { return -DBL_MAX; }
0051     //! Smallest positive value
0052     SCCEF_ double min() { return DBL_MIN; }
0053     //! Largest finite value
0054     SCCEF_ double max() { return DBL_MAX; }
0055     //! Special value for not-a-number (always compares false)
0056     SCCEF_ double quiet_NaN() { return __builtin_nan(""); }
0057     //! Special positive infinite value
0058     SCCEF_ double infinity() { return __builtin_huge_val(); }
0059 };
0060 
0061 template<>
0062 struct numeric_limits<int>
0063 {
0064     SCCEF_ int lowest() { return INT_MIN; }
0065     SCCEF_ int min() { return INT_MIN; }
0066     SCCEF_ int max() { return INT_MAX; }
0067 };
0068 
0069 template<>
0070 struct numeric_limits<long>
0071 {
0072     SCCEF_ long lowest() { return LONG_MIN; }
0073     SCCEF_ long min() { return LONG_MIN; }
0074     SCCEF_ long max() { return LONG_MAX; }
0075 };
0076 
0077 template<>
0078 struct numeric_limits<long long>
0079 {
0080     SCCEF_ long long lowest() { return LLONG_MIN; }
0081     SCCEF_ long long min() { return LLONG_MIN; }
0082     SCCEF_ long long max() { return LLONG_MAX; }
0083 };
0084 
0085 template<>
0086 struct numeric_limits<unsigned int>
0087 {
0088     SCCEF_ unsigned int lowest() { return 0; }
0089     SCCEF_ unsigned int min() { return 0; }
0090     SCCEF_ unsigned int max() { return UINT_MAX; }
0091 };
0092 
0093 template<>
0094 struct numeric_limits<unsigned long>
0095 {
0096     SCCEF_ unsigned long lowest() { return 0; }
0097     SCCEF_ unsigned long min() { return 0; }
0098     SCCEF_ unsigned long max() { return ULONG_MAX; }
0099 };
0100 
0101 template<>
0102 struct numeric_limits<unsigned long long>
0103 {
0104     SCCEF_ unsigned long long lowest() { return 0; }
0105     SCCEF_ unsigned long long min() { return 0; }
0106     SCCEF_ unsigned long long max() { return ULLONG_MAX; }
0107 };
0108 
0109 #undef SCCEF_
0110 
0111 //! Forward-compatible alias for v1.0
0112 template<class T>
0113 using NumericLimits = numeric_limits<T>;
0114 
0115 }  // namespace celeritas