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