ROOT  6.06/09
Reference Guide
casts.cpp
Go to the documentation of this file.
1 /* This file is part of the Vc library.
2 
3  Copyright (C) 2010-2011 Matthias Kretz <kretz@kde.org>
4 
5  Vc is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as
7  published by the Free Software Foundation, either version 3 of
8  the License, or (at your option) any later version.
9 
10  Vc is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with Vc. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #include "unittest.h"
21 #include <limits>
22 #include <algorithm>
23 
24 using namespace Vc;
25 
26 template<typename V1, typename V2> void testNumber(double n)
27 {
28  typedef typename V1::EntryType T1;
29  typedef typename V2::EntryType T2;
30 
31  // compare casts from T1 -> T2 with casts from V1 -> V2
32 
33  const T1 n1 = static_cast<T1>(n);
34  //std::cerr << "n1 = " << n1 << ", static_cast<T2>(n1) = " << static_cast<T2>(n1) << std::endl;
35  COMPARE(static_cast<V2>(V1(n1)), V2(static_cast<T2>(n1))) << "\n n1: " << n1;
36 }
37 
38 template<typename T> double maxHelper()
39 {
40  return static_cast<double>(std::numeric_limits<T>::max());
41 }
42 
43 template<> double maxHelper<int>()
44 {
45  const int intDigits = std::numeric_limits<int>::digits;
46  const int floatDigits = std::numeric_limits<float>::digits;
47  return static_cast<double>(((int(1) << floatDigits) - 1) << (intDigits - floatDigits));
48 }
49 
50 template<> double maxHelper<unsigned int>()
51 {
52  const int intDigits = std::numeric_limits<unsigned int>::digits;
53  const int floatDigits = std::numeric_limits<float>::digits;
54  return static_cast<double>(((unsigned(1) << floatDigits) - 1) << (intDigits - floatDigits));
55 }
56 
57 template<typename V1, typename V2> void testCast2()
58 {
59  typedef typename V1::EntryType T1;
60  typedef typename V2::EntryType T2;
61 
62  const double max = std::min(maxHelper<T1>(), maxHelper<T2>());
63  const double min = std::max(
64  std::numeric_limits<T1>::is_integer ?
65  static_cast<double>(std::numeric_limits<T1>::min()) :
66  static_cast<double>(-std::numeric_limits<T1>::max()),
67  std::numeric_limits<T2>::is_integer ?
68  static_cast<double>(std::numeric_limits<T2>::min()) :
69  static_cast<double>(-std::numeric_limits<T2>::max())
70  );
71 
72  testNumber<V1, V2>(0.);
73  testNumber<V1, V2>(1.);
74  testNumber<V1, V2>(2.);
75  testNumber<V1, V2>(max);
76  testNumber<V1, V2>(max / 4 + max / 2);
77  testNumber<V1, V2>(max / 2);
78  testNumber<V1, V2>(max / 4);
79  testNumber<V1, V2>(min);
80 }
81 
82 template<typename T> void testCast()
83 {
84  testCast2<typename T::V1, typename T::V2>();
85 }
86 
87 #define _CONCAT(A, B) A ## _ ## B
88 #define CONCAT(A, B) _CONCAT(A, B)
89 template<typename T1, typename T2>
90 struct T2Helper
91 {
92  typedef T1 V1;
93  typedef T2 V2;
94 };
95 
97 {
99  for (int i = 0; i < float_v::Size; ++i) {
100  COMPARE(test[i], float(i));
101  }
102 }
103 
104 int main(int argc, char **argv)
105 {
106  initTest(argc, argv);
107 
108 #define TEST(v1, v2) \
109  typedef T2Helper<v1, v2> CONCAT(v1, v2); \
110  runTest(testCast<CONCAT(v1, v2)>)
111 
112  TEST(float_v, float_v);
113  TEST(float_v, int_v);
114  TEST(float_v, uint_v);
115  // needs special handling for different Size:
116  //TEST(float_v, double_v);
117  //TEST(float_v, short_v);
118  //TEST(float_v, ushort_v);
119 
120  TEST(int_v, float_v);
121  TEST(int_v, int_v);
122  TEST(int_v, uint_v);
123 
124  TEST(uint_v, float_v);
125  TEST(uint_v, int_v);
126  TEST(uint_v, uint_v);
127 
131 
133  TEST(short_v, short_v);
135 
139 #undef TEST
140 
142 
143  return 0;
144 }
void testNumber(double n)
Definition: casts.cpp:26
void testFloatIndexesFromZero()
Definition: casts.cpp:96
VECTOR_NAMESPACE::uint_v uint_v
Definition: vector.h:88
VECTOR_NAMESPACE::sfloat_v sfloat_v
Definition: vector.h:82
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
void initTest(int argc, char **argv)
Definition: unittest.h:169
const char * Size
Definition: TXMLSetup.cxx:56
double maxHelper()
Definition: casts.cpp:38
VECTOR_NAMESPACE::short_v short_v
Definition: vector.h:90
double maxHelper< int >()
Definition: casts.cpp:43
#define COMPARE(a, b)
Definition: unittest.h:509
#define TEST(v1, v2)
VECTOR_NAMESPACE::ushort_v ushort_v
Definition: vector.h:92
int main(int argc, char **argv)
Definition: casts.cpp:104
double maxHelper< unsigned int >()
Definition: casts.cpp:50
VECTOR_NAMESPACE::int_v int_v
Definition: vector.h:86
void testCast2()
Definition: casts.cpp:57
#define T2
Definition: md5.inl:146
VECTOR_NAMESPACE::float_v float_v
Definition: vector.h:84
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
#define T1
Definition: md5.inl:145
Definition: casts.h:28
const Int_t n
Definition: legend1.C:16
void testCast()
Definition: casts.cpp:82
#define runTest(name)
Definition: unittest.h:42