xref: /reactos/sdk/lib/crt/math/libm_sse2/logb.c (revision e5993f13)
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 #define USE_INFINITY_WITH_FLAGS
31 #define USE_HANDLE_ERROR
32 #include "libm_inlines.h"
33 #undef USE_INFINITY_WITH_FLAGS
34 #undef USE_HANDLE_ERROR
35 
36 #include "libm_errno.h"
37 
38 double _logb(double x)
39 {
40 
41   unsigned long long ux;
42   long long u;
43   GET_BITS_DP64(x, ux);
44   u = ((ux & EXPBITS_DP64) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64;
45   if ((ux & ~SIGNBIT_DP64) == 0)
46     /* x is +/-zero. Return -infinity with div-by-zero flag. */
47     return _handle_error("_logb", OP_LOGB, NINFBITPATT_DP64, _SING,
48                         AMD_F_DIVBYZERO, ERANGE, x, 0.0, 1);
49   else if (EMIN_DP64 <= u && u <= EMAX_DP64)
50     /* x is a normal number */
51     return (double)u;
52   else if (u > EMAX_DP64)
53     {
54       /* x is infinity or NaN */
55       if ((ux & MANTBITS_DP64) == 0)
56         /* x is +/-infinity. For VC++, return infinity of same sign. */
57         return x;
58       else
59         /* x is NaN, result is NaN */
60         return _handle_error("_logb", OP_LOGB, ux|0x0008000000000000, _DOMAIN,
61                             0, EDOM, x, 0.0, 1);
62     }
63   else
64     {
65       /* x is denormalized. */
66 #ifdef FOLLOW_IEEE754_LOGB
67       /* Return the value of the minimum exponent to ensure that
68          the relationship between logb and scalb, defined in
69          IEEE 754, holds. */
70       return EMIN_DP64;
71 #else
72       /* Follow the rule set by IEEE 854 for logb */
73       ux &= MANTBITS_DP64;
74       u = EMIN_DP64;
75       while (ux < IMPBIT_DP64)
76         {
77           ux <<= 1;
78           u--;
79         }
80       return (double)u;
81 #endif
82     }
83 
84 }
85