1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/* Round to integer generic implementation.
Copyright (C) 2023 Free Software .
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library. If not, see
<https://www.gnu.org/licenses/>. */
#ifndef _ROUND_TO_INTEGER_H
#define _ROUND_TO_INTEGER_H
#include <fenv_private.h>
#include <sysdeps/ieee754/flt-32/math_config.h>
#include <stdio.h>
enum round_mode
{
CEIL,
FLOOR,
TRUNC,
};
static inline float
round_to_integer_float (enum round_mode mode, float x)
{
uint32_t hx = asuint (x);
int ex = (hx & ~SIGN_MASK) >> MANTISSA_WIDTH;
float r = x;
fenv_t fe;
libc_feholdexceptf (&fe);
switch (mode)
{
case CEIL:
asm ("ceil.l.s %0, %0" : "+f" (r));
break;
case FLOOR:
asm ("floor.l.s %0, %0" : "+f" (r));
break;
case TRUNC:
asm ("trunc.l.s %0, %0" : "+f" (r));
break;
break;
}
libc_fesetenvf (&fe);
/* Inf or NaN. */
if (__glibc_unlikely (ex == 0xff))
return x + x;
/* Number input and no fraction, return the number itself. */
if (ex >= 127 + 25)
return x;
asm ("cvt.s.l %0, %1" : "+f" (r));
/* The copysign seems to generate better code. */
#if 1
return copysignf (r, x);
#else
if (ex >= 127)
return r;
return hx & SIGN_MASK ? -r : r;
#endif
}
#endif
|