OLD | NEW |
| 1 //===- subzero/crosstest/test_cast.cpp - Cast operator tests --------------===// |
| 2 // |
| 3 // The Subzero Code Generator |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // Implementation for crosstesting cast operations. |
| 11 // |
| 12 //===----------------------------------------------------------------------===// |
| 13 |
1 // This aims to test all the conversion bitcode instructions across | 14 // This aims to test all the conversion bitcode instructions across |
2 // all PNaCl primitive data types. | 15 // all PNaCl primitive data types. |
3 | 16 |
4 #include <stdint.h> | 17 #include <stdint.h> |
5 #include "test_cast.h" | 18 #include "test_cast.h" |
6 | 19 |
7 template <typename FromType, typename ToType> | 20 template <typename FromType, typename ToType> |
8 ToType __attribute__((noinline)) cast(FromType a) { | 21 ToType __attribute__((noinline)) cast(FromType a) { |
9 return (ToType)a; | 22 return (ToType)a; |
10 } | 23 } |
11 | 24 |
12 template <typename FromType, typename ToType> | 25 template <typename FromType, typename ToType> |
13 ToType __attribute__((noinline)) castBits(FromType a) { | 26 ToType __attribute__((noinline)) castBits(FromType a) { |
14 return *(ToType *)&a; | 27 return *(ToType *)&a; |
15 } | 28 } |
16 | 29 |
17 // The purpose of the following sets of templates is to force | 30 // The purpose of the following sets of templates is to force |
18 // cast<A,B>() to be instantiated in the resulting bitcode file for | 31 // cast<A,B>() to be instantiated in the resulting bitcode file for |
19 // all <A,B>, so that they can be called from the driver. | 32 // all <A,B>, so that they can be called from the driver. |
20 template <typename ToType> class Caster { | 33 template <typename ToType> class Caster { |
21 static ToType f(bool a) { return cast<bool, ToType>(a); } | 34 static ToType f(bool a) { return cast<bool, ToType>(a); } |
22 static ToType f(int8_t a) { return cast<int8_t, ToType>(a); } | 35 static ToType f(myint8_t a) { return cast<myint8_t, ToType>(a); } |
23 static ToType f(uint8_t a) { return cast<uint8_t, ToType>(a); } | 36 static ToType f(uint8_t a) { return cast<uint8_t, ToType>(a); } |
24 static ToType f(int16_t a) { return cast<int16_t, ToType>(a); } | 37 static ToType f(int16_t a) { return cast<int16_t, ToType>(a); } |
25 static ToType f(uint16_t a) { return cast<uint16_t, ToType>(a); } | 38 static ToType f(uint16_t a) { return cast<uint16_t, ToType>(a); } |
26 static ToType f(int32_t a) { return cast<int32_t, ToType>(a); } | 39 static ToType f(int32_t a) { return cast<int32_t, ToType>(a); } |
27 static ToType f(uint32_t a) { return cast<uint32_t, ToType>(a); } | 40 static ToType f(uint32_t a) { return cast<uint32_t, ToType>(a); } |
28 static ToType f(int64_t a) { return cast<int64_t, ToType>(a); } | 41 static ToType f(int64_t a) { return cast<int64_t, ToType>(a); } |
29 static ToType f(uint64_t a) { return cast<uint64_t, ToType>(a); } | 42 static ToType f(uint64_t a) { return cast<uint64_t, ToType>(a); } |
30 static ToType f(float a) { return cast<float, ToType>(a); } | 43 static ToType f(float a) { return cast<float, ToType>(a); } |
31 static ToType f(double a) { return cast<double, ToType>(a); } | 44 static ToType f(double a) { return cast<double, ToType>(a); } |
32 }; | 45 }; |
33 | 46 |
34 // Comment out the definition of Caster<bool> because clang compiles | 47 // Comment out the definition of Caster<bool> because clang compiles |
35 // casts to bool using icmp instead of the desired cast instruction. | 48 // casts to bool using icmp instead of the desired cast instruction. |
36 // The corrected definitions are in test_cast_to_u1.ll. | 49 // The corrected definitions are in test_cast_to_u1.ll. |
37 | 50 |
38 // template class Caster<bool>; | 51 // template class Caster<bool>; |
39 | 52 |
40 template class Caster<int8_t>; | 53 template class Caster<myint8_t>; |
41 template class Caster<uint8_t>; | 54 template class Caster<uint8_t>; |
42 template class Caster<int16_t>; | 55 template class Caster<int16_t>; |
43 template class Caster<uint16_t>; | 56 template class Caster<uint16_t>; |
44 template class Caster<int32_t>; | 57 template class Caster<int32_t>; |
45 template class Caster<uint32_t>; | 58 template class Caster<uint32_t>; |
46 template class Caster<int64_t>; | 59 template class Caster<int64_t>; |
47 template class Caster<uint64_t>; | 60 template class Caster<uint64_t>; |
48 template class Caster<float>; | 61 template class Caster<float>; |
49 template class Caster<double>; | 62 template class Caster<double>; |
50 | 63 |
51 // This function definition forces castBits<A,B>() to be instantiated | 64 // This function definition forces castBits<A,B>() to be instantiated |
52 // in the resulting bitcode file for the 4 relevant <A,B> | 65 // in the resulting bitcode file for the 4 relevant <A,B> |
53 // combinations, so that they can be called from the driver. | 66 // combinations, so that they can be called from the driver. |
54 double makeBitCasters() { | 67 double makeBitCasters() { |
55 double Result = 0; | 68 double Result = 0; |
56 Result += castBits<uint32_t, float>(0); | 69 Result += castBits<uint32_t, float>(0); |
57 Result += castBits<uint64_t, double>(0); | 70 Result += castBits<uint64_t, double>(0); |
58 Result += castBits<float, uint32_t>(0); | 71 Result += castBits<float, uint32_t>(0); |
59 Result += castBits<double, uint64_t>(0); | 72 Result += castBits<double, uint64_t>(0); |
60 return Result; | 73 return Result; |
61 } | 74 } |
OLD | NEW |