/ src / ref / floorf.c
floorf.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 "fn_macros.h"
29  #include "libm_util_amd.h"
30  #include "libm_special.h"
31  
32  float FN_PROTOTYPE_REF(floorf)(float x)
33  {
34    float r;
35    int rexp, xneg;
36    unsigned int ux, ax, ur, mask;
37  
38    GET_BITS_SP32(x, ux);
39    ax = ux & (~SIGNBIT_SP32);
40    xneg = (ux != ax);
41  
42    if (ax >= 0x4b800000)
43      {
44        /* abs(x) is either NaN, infinity, or >= 2^24 */
45        if (ax > 0x7f800000)
46        {
47          /* x is NaN */
48          #ifdef WINDOWS
49                 return __amd_handle_errorf("floorf", __amd_floor, ux|0x00400000, _DOMAIN, 0, EDOM, x, 0.0, 1);
50          #else
51                 if(!(ax & 0x00400000)) //x is snan
52                       return __amd_handle_errorf("floorf", __amd_floor, ux|0x00400000, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1);
53                 else // x is qnan or inf
54                       return x;
55          #endif
56  	  }
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  
85