xref: /reactos/sdk/lib/crt/math/libm_sse2/modff.c (revision ccef43f3)
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 float modff(float x, float *iptr)
31 {
32   /* modff splits the argument x into integer and fraction parts,
33      each with the same sign as x. */
34 
35   unsigned int ux, mask;
36   int xexp;
37 
38   GET_BITS_SP32(x, ux);
39   xexp = ((ux & (~SIGNBIT_SP32)) >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
40 
41   if (xexp < 0)
42     {
43       /* abs(x) < 1.0. Set iptr to zero with the sign of x
44          and return x. */
45       PUT_BITS_SP32(ux & SIGNBIT_SP32, *iptr);
46       return x;
47     }
48   else if (xexp < EXPSHIFTBITS_SP32)
49     {
50       /* x lies between 1.0 and 2**(24) */
51       /* Mask out the bits of x that we don't want */
52       mask = (1 << (EXPSHIFTBITS_SP32 - xexp)) - 1;
53       PUT_BITS_SP32(ux & ~mask, *iptr);
54       return x - *iptr;
55     }
56   else if ((ux & (~SIGNBIT_SP32)) > 0x7f800000)
57     {
58       /* x is NaN */
59       *iptr = x;
60       return x + x; /* Raise invalid if it is a signalling NaN */
61     }
62   else
63     {
64       /* x is infinity or large. Set iptr to x and return zero
65          with the sign of x. */
66       *iptr = x;
67       PUT_BITS_SP32(ux & SIGNBIT_SP32, x);
68       return x;
69     }
70 }
71