//===- subzero/crosstest/test_cast.cpp - Cast operator tests --------------===// // // The Subzero Code Generator // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // Implementation for crosstesting cast operations. // //===----------------------------------------------------------------------===// // This aims to test all the conversion bitcode instructions across // all PNaCl primitive data types. #include #include "test_cast.h" #include "xdefs.h" template ToType __attribute__((noinline)) cast(FromType a) { return (ToType)a; } template ToType __attribute__((noinline)) castBits(FromType a) { return *(ToType *)&a; } template ToType __attribute__((noinline)) cast(int i, FromType a, int j) { (void)i; (void)j; return (ToType)a; } template ToType __attribute__((noinline)) castBits(int i, FromType a, int j) { (void)i; (void)j; return *(ToType *)&a; } // The purpose of the following sets of templates is to force // cast() to be instantiated in the resulting bitcode file for // all , so that they can be called from the driver. template class Caster { static ToType f(bool a) { return cast(a); } static ToType f(myint8_t a) { return cast(a); } static ToType f(uint8_t a) { return cast(a); } static ToType f(int16_t a) { return cast(a); } static ToType f(uint16_t a) { return cast(a); } static ToType f(int32_t a) { return cast(a); } static ToType f(uint32_t a) { return cast(a); } static ToType f(int64 a) { return cast(a); } static ToType f(uint64 a) { return cast(a); } static ToType f(float a) { return cast(a); } static ToType f(double a) { return cast(a); } static ToType f(int i, bool a) { return cast(i, a, i); } static ToType f(int i, myint8_t a) { return cast(i, a, i); } static ToType f(int i, uint8_t a) { return cast(i, a, i); } static ToType f(int i, int16_t a) { return cast(i, a, i); } static ToType f(int i, uint16_t a) { return cast(i, a, i); } static ToType f(int i, int32_t a) { return cast(i, a, i); } static ToType f(int i, uint32_t a) { return cast(i, a, i); } static ToType f(int i, int64 a) { return cast(i, a, i); } static ToType f(int i, uint64 a) { return cast(i, a, i); } static ToType f(int i, float a) { return cast(i, a, i); } static ToType f(int i, double a) { return cast(i, a, i); } }; // Comment out the definition of Caster because clang compiles // casts to bool using icmp instead of the desired cast instruction. // The corrected definitions are in test_cast_to_u1.ll. // template class Caster; template class Caster; template class Caster; template class Caster; template class Caster; template class Caster; template class Caster; template class Caster; template class Caster; template class Caster; template class Caster; // This function definition forces castBits() to be instantiated // in the resulting bitcode file for the 4 relevant // combinations, so that they can be called from the driver. double makeBitCasters() { double Result = 0; Result += castBits(0); Result += castBits(0); Result += castBits(0); Result += castBits(0); Result += castBits(1, 0, 2); Result += castBits(1, 0, 2); Result += castBits(1, 0, 2); Result += castBits(1, 0, 2); return Result; }