| 1 |
/* Software floating-point emulation. |
| 2 |
Truncate bfloat16 into IEEE half. |
| 3 |
Copyright (C) 2022 Free Software Foundation, Inc. |
| 4 |
This file is part of the GNU C Library. |
| 5 |
|
| 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. |
| 10 |
|
| 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.) |
| 19 |
|
| 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. |
| 24 |
|
| 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/>. */ |
| 28 |
|
| 29 |
#include "soft-fp.h" |
| 30 |
#include "half.h" |
| 31 |
#include "brain.h" |
| 32 |
#include "single.h" |
| 33 |
|
| 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. */ |
| 37 |
|
| 38 |
HFtype |
| 39 |
__truncbfhf2 (BFtype a) |
| 40 |
{ |
| 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; |
| 47 |
|
| 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) |
| 56 |
{ |
| 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; |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
FP_UNPACK_RAW_B (A, a); |
| 66 |
FP_EXTEND (S, B, 1, 1, B, A); |
| 67 |
FP_PACK_RAW_S (b, B); |
| 68 |
} |
| 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; |
| 73 |
|
| 74 |
return r; |
| 75 |
} |