/ src / ref / logb.c
logb.c
 1  /*
 2   * Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved.
 3   *
 4   * Redistribution and use in source and binary forms, with or without modification,
 5   * are permitted provided that the following conditions are met:
 6   * 1. Redistributions of source code must retain the above copyright notice,
 7   *    this list of conditions and the following disclaimer.
 8   * 2. Redistributions in binary form must reproduce the above copyright notice,
 9   *    this list of conditions and the following disclaimer in the documentation
10   *    and/or other materials provided with the distribution.
11   * 3. Neither the name of the copyright holder nor the names of its contributors
12   *    may be used to endorse or promote products derived from this software without
13   *    specific prior written permission.
14   *
15   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18   * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
19   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   *
26   */
27  
28  #include "libm_amd.h"
29  #include "libm_util_amd.h"
30  #include "libm_special.h"
31  
32  double FN_PROTOTYPE_REF(logb)(double x)
33  {
34  
35    unsigned long long ux;
36    long long u;
37    GET_BITS_DP64(x, ux);
38    u = ((ux & EXPBITS_DP64) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64;
39    if ((ux & ~SIGNBIT_DP64) == 0)
40      /* x is +/-zero. Return -infinity with div-by-zero flag. */
41  	return __amd_handle_error("logb", __amd_logb, NINFBITPATT_DP64, _SING, AMD_F_DIVBYZERO, ERANGE, x, 0.0, 1);
42    else if (EMIN_DP64 <= u && u <= EMAX_DP64)
43      /* x is a normal number */
44      return (double)u;
45    else if (u > EMAX_DP64)
46      {
47        /* x is infinity or NaN */
48        if ((ux & MANTBITS_DP64) == 0)
49        {
50          /* x is +/-infinity. For VC++, return infinity of same sign. */
51  #ifdef WINDOWS 
52          PUT_BITS_DP64(ux,x);
53  #else              
54          PUT_BITS_DP64(ux&(~SIGNBIT_DP64),x);
55  #endif
56          return x;
57        }
58        else
59          /* x is NaN, result is NaN */
60  #ifdef WINDOWS
61            return __amd_handle_error("logb", __amd_logb, ux|0x0008000000000000, DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1);
62  #else
63            return x+x;
64  #endif
65      }
66    else
67      {
68        /* x is denormalized. */
69  #ifdef FOLLOW_IEEE754_LOGB
70        /* Return the value of the minimum exponent to ensure that
71           the relationship between logb and scalb, defined in
72           IEEE 754, holds. */
73        return EMIN_DP64;
74  #else
75        /* Follow the rule set by IEEE 854 for logb */
76        ux &= MANTBITS_DP64;
77        u = EMIN_DP64;
78        while (ux < IMPBIT_DP64)
79          {
80            ux <<= 1;
81            u--;
82          }
83        return (double)u;
84  #endif
85      }
86  
87  }
88  
89