1 //===--------------- truncsfhf2_test.c - Test __truncsfhf2 ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file tests __truncsfhf2 for the compiler_rt library. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include <stdio.h> 15 16 #include "fp_test.h" 17 18 uint16_t __truncsfhf2(float a); 19 20 int test__truncsfhf2(float a, uint16_t expected) 21 { 22 uint16_t x = __truncsfhf2(a); 23 int ret = compareResultH(x, expected); 24 25 if (ret){ 26 printf("error in test__truncsfhf2(%f) = %#.4x, " 27 "expected %#.4x\n", a, x, fromRep16(expected)); 28 } 29 return ret; 30 } 31 32 char assumption_1[sizeof(__fp16) * CHAR_BIT == 16] = {0}; 33 34 int main() 35 { 36 // qNaN 37 if (test__truncsfhf2(makeQNaN32(), 38 UINT16_C(0x7e00))) 39 return 1; 40 // NaN 41 if (test__truncsfhf2(makeNaN32(UINT32_C(0x8000)), 42 UINT16_C(0x7e00))) 43 return 1; 44 // inf 45 if (test__truncsfhf2(makeInf32(), 46 UINT16_C(0x7c00))) 47 return 1; 48 if (test__truncsfhf2(-makeInf32(), 49 UINT16_C(0xfc00))) 50 return 1; 51 // zero 52 if (test__truncsfhf2(0.0f, UINT16_C(0x0))) 53 return 1; 54 if (test__truncsfhf2(-0.0f, UINT16_C(0x8000))) 55 return 1; 56 57 if (test__truncsfhf2(3.1415926535f, 58 UINT16_C(0x4248))) 59 return 1; 60 if (test__truncsfhf2(-3.1415926535f, 61 UINT16_C(0xc248))) 62 return 1; 63 if (test__truncsfhf2(0x1.987124876876324p+100f, 64 UINT16_C(0x7c00))) 65 return 1; 66 if (test__truncsfhf2(0x1.987124876876324p+12f, 67 UINT16_C(0x6e62))) 68 return 1; 69 if (test__truncsfhf2(0x1.0p+0f, 70 UINT16_C(0x3c00))) 71 return 1; 72 if (test__truncsfhf2(0x1.0p-14f, 73 UINT16_C(0x0400))) 74 return 1; 75 // denormal 76 if (test__truncsfhf2(0x1.0p-20f, 77 UINT16_C(0x0010))) 78 return 1; 79 if (test__truncsfhf2(0x1.0p-24f, 80 UINT16_C(0x0001))) 81 return 1; 82 if (test__truncsfhf2(-0x1.0p-24f, 83 UINT16_C(0x8001))) 84 return 1; 85 if (test__truncsfhf2(0x1.5p-25f, 86 UINT16_C(0x0001))) 87 return 1; 88 // and back to zero 89 if (test__truncsfhf2(0x1.0p-25f, 90 UINT16_C(0x0000))) 91 return 1; 92 if (test__truncsfhf2(-0x1.0p-25f, 93 UINT16_C(0x8000))) 94 return 1; 95 // max (precise) 96 if (test__truncsfhf2(65504.0f, 97 UINT16_C(0x7bff))) 98 return 1; 99 // max (rounded) 100 if (test__truncsfhf2(65519.0f, 101 UINT16_C(0x7bff))) 102 return 1; 103 // max (to +inf) 104 if (test__truncsfhf2(65520.0f, 105 UINT16_C(0x7c00))) 106 return 1; 107 if (test__truncsfhf2(65536.0f, 108 UINT16_C(0x7c00))) 109 return 1; 110 if (test__truncsfhf2(-65520.0f, 111 UINT16_C(0xfc00))) 112 return 1; 113 return 0; 114 } 115