malloc/Makefile: Split and sort tests
[glibc.git] / soft-fp / truncbfhf2.c
blob627f8c65591263f8e5d62f1d2e8daf961259728c
1 /* Software floating-point emulation.
2 Truncate bfloat16 into IEEE half.
3 Copyright (C) 2022-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 In addition to the permissions in the GNU Lesser General Public
12 License, the Free Software Foundation gives you unlimited
13 permission to link the compiled version of this file into
14 combinations with other programs, and to distribute those
15 combinations without any restriction coming from the use of this
16 file. (The Lesser General Public License restrictions do apply in
17 other respects; for example, they cover modification of the file,
18 and distribution when not linked into a combine executable.)
20 The GNU C Library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with the GNU C Library; if not, see
27 <http://www.gnu.org/licenses/>. */
29 #include "soft-fp.h"
30 #include "half.h"
31 #include "brain.h"
32 #include "single.h"
34 /* BFtype and HFtype are unordered, neither is a superset or subset
35 of each other. Convert BFtype to SFtype (lossless) and then
36 truncate to HFtype. */
38 HFtype
39 __truncbfhf2 (BFtype a)
41 FP_DECL_EX;
42 FP_DECL_H (A);
43 FP_DECL_S (B);
44 FP_DECL_B (R);
45 SFtype b;
46 HFtype r;
48 FP_INIT_ROUNDMODE;
49 /* Optimize BFtype to SFtype conversion to simple left shift
50 by 16 if possible, we don't need to raise exceptions on sNaN
51 here as the SFtype to HFtype truncation should do that too. */
52 if (sizeof (BFtype) == 2
53 && sizeof (unsigned short) == 2
54 && sizeof (SFtype) == 4
55 && sizeof (unsigned int) == 4)
57 union { BFtype a; unsigned short b; } u1;
58 union { SFtype a; unsigned int b; } u2;
59 u1.a = a;
60 u2.b = (u1.b << 8) << 8;
61 b = u2.a;
63 else
65 FP_UNPACK_RAW_B (A, a);
66 FP_EXTEND (S, B, 1, 1, B, A);
67 FP_PACK_RAW_S (b, B);
69 FP_UNPACK_SEMIRAW_S (B, b);
70 FP_TRUNC (H, S, 1, 1, R, B);
71 FP_PACK_SEMIRAW_H (r, R);
72 FP_HANDLE_EXCEPTIONS;
74 return r;