xref: /reactos/sdk/lib/crt/math/libm_sse2/floorf.c (revision 83e13630)
1 
2 /*******************************************************************************
3 MIT License
4 -----------
5 
6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this Software and associated documentaon files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 *******************************************************************************/
26 
27 #include "libm.h"
28 #include "libm_util.h"
29 
30 #include "libm_errno.h"
31 #define USE_HANDLE_ERRORF
32 #include "libm_inlines.h"
33 #undef USE_HANDLE_ERRORF
34 
35 // Disable "C4163: not available as intrinsic function" warning that older
36 // compilers may issue here.
37 #pragma warning(disable:4163)
38 #pragma function(floorf)
39 
40 float FN_PROTOTYPE(floorf)(float x)
41 {
42   float r;
43   int rexp, xneg;
44   unsigned int ux, ax, ur, mask;
45 
46   GET_BITS_SP32(x, ux);
47   ax = ux & (~SIGNBIT_SP32);
48   xneg = (ux != ax);
49 
50   if (ax >= 0x4b800000)
51     {
52       /* abs(x) is either NaN, infinity, or >= 2^24 */
53       if (ax > 0x7f800000)
54         /* x is NaN */
55         return _handle_errorf("floorf", OP_FLOOR, ux|0x00400000, _DOMAIN,
56                              0, EDOM, x, 0.0F, 1);
57       else
58         return x;
59     }
60   else if (ax < 0x3f800000) /* abs(x) < 1.0 */
61     {
62       if (ax == 0x00000000)
63         /* x is +zero or -zero; return the same zero */
64         return x;
65       else if (xneg) /* x < 0.0 */
66         return -1.0F;
67       else
68         return 0.0F;
69     }
70   else
71     {
72       rexp = ((ux & EXPBITS_SP32) >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
73       /* Mask out the bits of r that we don't want */
74       mask = (1 << (EXPSHIFTBITS_SP32 - rexp)) - 1;
75       ur = (ux & ~mask);
76       PUT_BITS_SP32(ur, r);
77       if (xneg && (ux != ur))
78         /* We threw some bits away and x was negative */
79         return r - 1.0F;
80       else
81         return r;
82     }
83 }
84