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
14 // This aims to test all the conversion bitcode instructions across
15 // all PNaCl primitive data types.
16
17 #include <stdint.h>
18 #include "test_cast.h"
19 #include "xdefs.h"
20
21 template <typename FromType, typename ToType>
cast(FromType a)22 ToType __attribute__((noinline)) cast(FromType a) {
23 return (ToType)a;
24 }
25
26 template <typename FromType, typename ToType>
castBits(FromType a)27 ToType __attribute__((noinline)) castBits(FromType a) {
28 return *(ToType *)&a;
29 }
30
31 template <typename FromType, typename ToType>
cast(int i,FromType a,int j)32 ToType __attribute__((noinline)) cast(int i, FromType a, int j) {
33 (void)i;
34 (void)j;
35 return (ToType)a;
36 }
37
38 template <typename FromType, typename ToType>
castBits(int i,FromType a,int j)39 ToType __attribute__((noinline)) castBits(int i, FromType a, int j) {
40 (void)i;
41 (void)j;
42 return *(ToType *)&a;
43 }
44
45 // The purpose of the following sets of templates is to force
46 // cast<A,B>() to be instantiated in the resulting bitcode file for
47 // all <A,B>, so that they can be called from the driver.
48 template <typename ToType> class Caster {
f(bool a)49 static ToType f(bool a) { return cast<bool, ToType>(a); }
f(myint8_t a)50 static ToType f(myint8_t a) { return cast<myint8_t, ToType>(a); }
f(uint8_t a)51 static ToType f(uint8_t a) { return cast<uint8_t, ToType>(a); }
f(int16_t a)52 static ToType f(int16_t a) { return cast<int16_t, ToType>(a); }
f(uint16_t a)53 static ToType f(uint16_t a) { return cast<uint16_t, ToType>(a); }
f(int32_t a)54 static ToType f(int32_t a) { return cast<int32_t, ToType>(a); }
f(uint32_t a)55 static ToType f(uint32_t a) { return cast<uint32_t, ToType>(a); }
f(int64 a)56 static ToType f(int64 a) { return cast<int64, ToType>(a); }
f(uint64 a)57 static ToType f(uint64 a) { return cast<uint64, ToType>(a); }
f(float a)58 static ToType f(float a) { return cast<float, ToType>(a); }
f(double a)59 static ToType f(double a) { return cast<double, ToType>(a); }
f(int i,bool a)60 static ToType f(int i, bool a) { return cast<bool, ToType>(i, a, i); }
f(int i,myint8_t a)61 static ToType f(int i, myint8_t a) { return cast<myint8_t, ToType>(i, a, i); }
f(int i,uint8_t a)62 static ToType f(int i, uint8_t a) { return cast<uint8_t, ToType>(i, a, i); }
f(int i,int16_t a)63 static ToType f(int i, int16_t a) { return cast<int16_t, ToType>(i, a, i); }
f(int i,uint16_t a)64 static ToType f(int i, uint16_t a) { return cast<uint16_t, ToType>(i, a, i); }
f(int i,int32_t a)65 static ToType f(int i, int32_t a) { return cast<int32_t, ToType>(i, a, i); }
f(int i,uint32_t a)66 static ToType f(int i, uint32_t a) { return cast<uint32_t, ToType>(i, a, i); }
f(int i,int64 a)67 static ToType f(int i, int64 a) { return cast<int64, ToType>(i, a, i); }
f(int i,uint64 a)68 static ToType f(int i, uint64 a) { return cast<uint64, ToType>(i, a, i); }
f(int i,float a)69 static ToType f(int i, float a) { return cast<float, ToType>(i, a, i); }
f(int i,double a)70 static ToType f(int i, double a) { return cast<double, ToType>(i, a, i); }
71 };
72
73 // Comment out the definition of Caster<bool> because clang compiles
74 // casts to bool using icmp instead of the desired cast instruction.
75 // The corrected definitions are in test_cast_to_u1.ll.
76
77 // template class Caster<bool>;
78
79 template class Caster<myint8_t>;
80 template class Caster<uint8_t>;
81 template class Caster<int16_t>;
82 template class Caster<uint16_t>;
83 template class Caster<int32_t>;
84 template class Caster<uint32_t>;
85 template class Caster<int64>;
86 template class Caster<uint64>;
87 template class Caster<float>;
88 template class Caster<double>;
89
90 // This function definition forces castBits<A,B>() to be instantiated
91 // in the resulting bitcode file for the 4 relevant <A,B>
92 // combinations, so that they can be called from the driver.
makeBitCasters()93 double makeBitCasters() {
94 double Result = 0;
95 Result += castBits<uint32_t, float>(0);
96 Result += castBits<uint64, double>(0);
97 Result += castBits<float, uint32_t>(0);
98 Result += castBits<double, uint64>(0);
99 Result += castBits<uint32_t, float>(1, 0, 2);
100 Result += castBits<uint64, double>(1, 0, 2);
101 Result += castBits<float, uint32_t>(1, 0, 2);
102 Result += castBits<double, uint64>(1, 0, 2);
103 return Result;
104 }
105