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