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