source: XIOS/dev/dev_olga/src/extern/blitz/include/blitz/promote.h @ 1022

Last change on this file since 1022 was 1022, checked in by mhnguyen, 7 years ago
File size: 5.4 KB
Line 
1// -*- C++ -*-
2/***********************************************************************
3 * promote.h   Arithmetic type promotion trait class
4 *
5 * $Id$
6 *
7 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
8 *
9 * This file is a part of Blitz.
10 *
11 * Blitz is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation, either version 3
14 * of the License, or (at your option) any later version.
15 *
16 * Blitz is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 * Suggestions:          blitz-devel@lists.sourceforge.net
25 * Bugs:                 blitz-support@lists.sourceforge.net   
26 *
27 * For more information, please see the Blitz++ Home Page:
28 *    https://sourceforge.net/projects/blitz/
29 *
30 ***************************************************************************/
31
32#ifndef BZ_PROMOTE_H
33#define BZ_PROMOTE_H
34
35#include <blitz/blitz.h>
36
37BZ_NAMESPACE(blitz)
38
39#ifdef BZ_HAVE_TEMPLATE_QUALIFIED_RETURN_TYPE
40    #define BZ_PROMOTE(A,B) _bz_typename BZ_BLITZ_SCOPE(promote_trait)<A,B>::T_promote
41#else
42    #define BZ_PROMOTE(A,B) A
43#endif
44
45#if defined(BZ_HAVE_PARTIAL_SPECIALIZATION) && !defined(BZ_DISABLE_NEW_PROMOTE)
46
47/*
48 * This compiler supports partial specialization, so type promotion
49 * can be done the elegant way.  This implementation is after ideas
50 * by Jean-Louis Leroy.
51 */
52
53template<typename T>
54struct precision_trait {
55    static const int precisionRank = 0;
56    static const bool knowPrecisionRank = false;
57};
58
59#define BZ_DECLARE_PRECISION(T,rank)                  \
60    template<>                                        \
61    struct precision_trait< T > {                     \
62        static const int precisionRank = rank;        \
63        static const bool knowPrecisionRank = true;   \
64    };
65
66BZ_DECLARE_PRECISION(int,100)
67BZ_DECLARE_PRECISION(unsigned int,200)
68BZ_DECLARE_PRECISION(long,300)
69BZ_DECLARE_PRECISION(unsigned long,400)
70BZ_DECLARE_PRECISION(float,500)
71BZ_DECLARE_PRECISION(double,600)
72BZ_DECLARE_PRECISION(long double,700)
73
74#ifdef BZ_HAVE_COMPLEX
75BZ_DECLARE_PRECISION(complex<float>,800)
76BZ_DECLARE_PRECISION(complex<double>,900)
77BZ_DECLARE_PRECISION(complex<long double>,1000)
78#endif
79
80template<typename T>
81struct autopromote_trait {
82    typedef T T_numtype;
83};
84
85#define BZ_DECLARE_AUTOPROMOTE(T1,T2)     \
86    template<>                            \
87    struct autopromote_trait<T1> {        \
88      typedef T2 T_numtype;               \
89    };
90
91// These are the odd cases where small integer types
92// are automatically promoted to int or unsigned int for
93// arithmetic.
94BZ_DECLARE_AUTOPROMOTE(bool, int)
95BZ_DECLARE_AUTOPROMOTE(char, int)
96BZ_DECLARE_AUTOPROMOTE(unsigned char, int)
97BZ_DECLARE_AUTOPROMOTE(short int, int)
98BZ_DECLARE_AUTOPROMOTE(short unsigned int, unsigned int)
99
100template<typename T1, typename T2, bool promoteToT1>
101struct _bz_promote2 {
102    typedef T1 T_promote;
103};
104
105template<typename T1, typename T2>
106struct _bz_promote2<T1,T2,false> {
107    typedef T2 T_promote;
108};
109
110template<typename T1_orig, typename T2_orig>
111struct promote_trait {
112    // Handle promotion of small integers to int/unsigned int
113    typedef _bz_typename autopromote_trait<T1_orig>::T_numtype T1;
114    typedef _bz_typename autopromote_trait<T2_orig>::T_numtype T2;
115
116    // True if T1 is higher ranked
117    static const bool
118        T1IsBetter =
119            precision_trait<T1>::precisionRank >
120            precision_trait<T2>::precisionRank;
121
122    // True if we know ranks for both T1 and T2
123    static const bool
124        knowBothRanks =
125            precision_trait<T1>::knowPrecisionRank && 
126            precision_trait<T2>::knowPrecisionRank;
127
128    // True if we know T1 but not T2
129    static const bool
130        knowT1butNotT2 = 
131            precision_trait<T1>::knowPrecisionRank && 
132            !precision_trait<T2>::knowPrecisionRank;
133
134    // True if we know T2 but not T1
135    static const bool
136        knowT2butNotT1 = 
137            precision_trait<T2>::knowPrecisionRank && 
138            !precision_trait<T1>::knowPrecisionRank;
139
140    // True if T1 is bigger than T2
141    static const bool
142        T1IsLarger = sizeof(T1) >= sizeof(T2);
143
144    // We know T1 but not T2: false
145    // We know T2 but not T1: true
146    // Otherwise, if T1 is bigger than T2: true
147//     static const bool
148//         defaultPromotion = knowT1butNotT2 ? false :
149//             (knowT2butNotT1 ? true : T1IsLarger);
150
151    // If we have both ranks, then use them.
152    // If we have only one rank, then use the unknown type.
153    // If we have neither rank, then promote to the larger type.
154    static const bool
155        promoteToT1 = knowBothRanks ? T1IsBetter : (knowT1butNotT2 ? false : 
156            (knowT2butNotT1 ? true : T1IsLarger));
157//     static const bool
158//         promoteToT1 = knowBothRanks ? T1IsBetter : defaultPromotion;
159
160    typedef _bz_typename _bz_promote2<T1,T2,promoteToT1>::T_promote T_promote;
161};
162
163#else  // !BZ_HAVE_PARTIAL_SPECIALIZATION
164
165  // No partial specialization -- have to do it the ugly way.
166  #include <blitz/promote-old.h>
167
168#endif // !BZ_HAVE_PARTIAL_SPECIALIZATION
169
170BZ_NAMESPACE_END
171
172#endif // BZ_PROMOTE_H
Note: See TracBrowser for help on using the repository browser.