1 /*
  2  *   Copyright (c) 2011, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 #if defined(MPFR_REAL_HPP) && !defined(FLENS_HACKS_MPFR_REAL_TCC)
 34 #define FLENS_HACKS_MPFR_REAL_TCC 1
 35 
 36 
 37 /*
 38  * NOTE: This hack requires that in the mpfr::real class the '_x' attribute
 39  *       is made public!
 40  */
 41 
 42 namespace mpfr {
 43 
 44 //
 45 // aux-functions used in numeric_limits
 46 //
 47 
 48 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
 49 const mpfr::real<_prec,_rnd>
 50 nextabove(const mpfr::real<_prec,_rnd> &x)
 51 {
 52     mpfr::real<_prec,_rnd> tmp = x;
 53     mpfr_nextabove(tmp._x);
 54     return tmp;
 55 }
 56 
 57 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
 58 const mpfr::real<_prec,_rnd>
 59 get_max()
 60 {
 61     const unsigned long emax = mpfr_get_emax();
 62     mpfr::real<_prec,_rnd> tmp(1);
 63 
 64     mpfr_mul_2ui(tmp._x, tmp._x, emax-1, _rnd);
 65     return tmp;
 66 }
 67 
 68 // namespace mpfr
 69 
 70 //
 71 // numeric_limits
 72 //
 73 
 74 namespace std {
 75 
 76 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
 77     const mpfr::real<_prec,_rnd>
 78     numeric_limits<mpfr::real<_prec,_rnd> >::_max
 79         = mpfr::get_max<_prec,_rnd>();
 80 
 81 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
 82     const mpfr::real<_prec,_rnd>
 83     numeric_limits<mpfr::real<_prec,_rnd> >::_min
 84         = mpfr::nextabove(T(0));
 85 
 86 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
 87     const mpfr::real<_prec,_rnd>
 88     numeric_limits<mpfr::real<_prec,_rnd> >::_eps
 89         = mpfr::nextabove(T(1)) - T(1);
 90 
 91 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
 92 const mpfr::real<_prec,_rnd>
 93 numeric_limits<mpfr::real<_prec,_rnd> >::epsilon()
 94 {
 95      return _eps;
 96 }
 97 
 98 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
 99 const mpfr::real<_prec,_rnd>
100 numeric_limits<mpfr::real<_prec,_rnd> >::max()
101 {
102     return _max;
103 }
104 
105 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
106 const mpfr::real<_prec,_rnd>
107 numeric_limits<mpfr::real<_prec,_rnd> >::min()
108 {
109     return _min;
110 }
111 
112 //
113 // import isnan to namespace std
114 //
115 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
116 bool
117 isnan(const mpfr::real<_prec,_rnd> &x)
118 {
119     //
120     // TODO: There seems to be a bug in clang++ because
121     //
122     //          return mpfr::isnan(x);
123     //
124     //       does not compile (but is fine with g++4.7)
125     return mpfr_nan_p(x._x);
126 }
127 
128 //
129 // import mpfr_real to namespace std
130 //
131 template <mpfr::real_prec_t _prec, mpfr::real_rnd_t _rnd>
132 int
133 signbit(const mpfr::real<_prec,_rnd> &x)
134 {
135     //
136     // TODO: There seems to be a bug in clang++ because
137     //
138     //          return mpfr::signbit(x);
139     //
140     //       does not compile (but is fine with g++4.7)
141     return mpfr_signbit(x._x);
142 }
143 
144 
145 // namespace std
146 
147 #endif // defined(MPFR_REAL_HPP) && !defined(FLENS_HACKS_MPFR_REAL_TCC)