Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-29 09:08:18

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  * \todo Replace with the cuda/hip standard libraries or a modified llvm port
0031  */
0032 template<class Numeric>
0033 struct numeric_limits;
0034 
0035 template<>
0036 struct numeric_limits<float>
0037 {
0038     SCCEF_ float epsilon() { return FLT_EPSILON; }
0039     SCCEF_ float lowest() { return -FLT_MAX; }
0040     SCCEF_ float min() { return FLT_MIN; }
0041     SCCEF_ float max() { return FLT_MAX; }
0042     SCCEF_ float quiet_NaN() { return __builtin_nanf(""); }
0043     SCCEF_ float infinity() { return __builtin_huge_valf(); }
0044 };
0045 
0046 template<>
0047 struct numeric_limits<double>
0048 {
0049     //! Relative difference between 1 and adjacent floating point number
0050     SCCEF_ double epsilon() { return DBL_EPSILON; }
0051     //! Most negative finite value
0052     SCCEF_ double lowest() { return -DBL_MAX; }
0053     //! Smallest positive value
0054     SCCEF_ double min() { return DBL_MIN; }
0055     //! Largest finite value
0056     SCCEF_ double max() { return DBL_MAX; }
0057     //! Special value for not-a-number (always compares false)
0058     SCCEF_ double quiet_NaN() { return __builtin_nan(""); }
0059     //! Special positive infinite value
0060     SCCEF_ double infinity() { return __builtin_huge_val(); }
0061 };
0062 
0063 template<>
0064 struct numeric_limits<int>
0065 {
0066     SCCEF_ int lowest() { return INT_MIN; }
0067     SCCEF_ int min() { return INT_MIN; }
0068     SCCEF_ int max() { return INT_MAX; }
0069 };
0070 
0071 template<>
0072 struct numeric_limits<long>
0073 {
0074     SCCEF_ long lowest() { return LONG_MIN; }
0075     SCCEF_ long min() { return LONG_MIN; }
0076     SCCEF_ long max() { return LONG_MAX; }
0077 };
0078 
0079 template<>
0080 struct numeric_limits<long long>
0081 {
0082     SCCEF_ long long lowest() { return LLONG_MIN; }
0083     SCCEF_ long long min() { return LLONG_MIN; }
0084     SCCEF_ long long max() { return LLONG_MAX; }
0085 };
0086 
0087 template<>
0088 struct numeric_limits<unsigned char>
0089 {
0090     SCCEF_ unsigned char lowest() { return 0; }
0091     SCCEF_ unsigned char min() { return 0; }
0092     SCCEF_ unsigned char max() { return (unsigned char)(UCHAR_MAX); }
0093 };
0094 
0095 template<>
0096 struct numeric_limits<unsigned short>
0097 {
0098     SCCEF_ unsigned short lowest() { return 0; }
0099     SCCEF_ unsigned short min() { return 0; }
0100     SCCEF_ unsigned short max() { return (unsigned short)(USHRT_MAX); }
0101 };
0102 
0103 template<>
0104 struct numeric_limits<unsigned int>
0105 {
0106     SCCEF_ unsigned int lowest() { return 0; }
0107     SCCEF_ unsigned int min() { return 0; }
0108     SCCEF_ unsigned int max() { return UINT_MAX; }
0109 };
0110 
0111 template<>
0112 struct numeric_limits<unsigned long>
0113 {
0114     SCCEF_ unsigned long lowest() { return 0; }
0115     SCCEF_ unsigned long min() { return 0; }
0116     SCCEF_ unsigned long max() { return ULONG_MAX; }
0117 };
0118 
0119 template<>
0120 struct numeric_limits<unsigned long long>
0121 {
0122     SCCEF_ unsigned long long lowest() { return 0; }
0123     SCCEF_ unsigned long long min() { return 0; }
0124     SCCEF_ unsigned long long max() { return ULLONG_MAX; }
0125 };
0126 
0127 #undef SCCEF_
0128 
0129 //! Forward-compatible alias for v1.0
0130 template<class T>
0131 using NumericLimits = numeric_limits<T>;
0132 
0133 }  // namespace celeritas