src/libm/s_floor.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 6044 35448a5ea044
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/* @(#)s_floor.c 5.1 93/09/24 */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
/*
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
 * ====================================================
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
 *
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
 * Developed at SunPro, a Sun Microsystems, Inc. business.
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
 * Permission to use, copy, modify, and distribute this
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
 * software is freely granted, provided that this notice
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
 * is preserved.
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
 * ====================================================
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
 */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
#if defined(LIBM_SCCS) && !defined(lint)
3162
dc1eb82ffdaa Von: Thomas Zimmermann
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
    14
static const char rcsid[] =
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
    "$NetBSD: s_floor.c,v 1.8 1995/05/10 20:47:20 jtc Exp $";
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#endif
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
/*
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
 * floor(x)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
 * Return x rounded toward -inf to integral value
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
 * Method:
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
 *	Bit twiddling.
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
 * Exception:
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
 *	Inexact flag raised if x not equal to floor(x).
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
 */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 3337
diff changeset
    27
#include "math_libm.h"
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "math_private.h"
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#ifdef __STDC__
3337
9ac6f0782dd6 Fixed bug #814
Sam Lantinga <slouken@libsdl.org>
parents: 3162
diff changeset
    31
static const double huge_val = 1.0e300;
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
#else
3337
9ac6f0782dd6 Fixed bug #814
Sam Lantinga <slouken@libsdl.org>
parents: 3162
diff changeset
    33
static double huge_val = 1.0e300;
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
#endif
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
libm_hidden_proto(floor)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
#ifdef __STDC__
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
     double floor(double x)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
#else
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
     double floor(x)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
     double x;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
#endif
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
{
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
    int32_t i0, i1, j0;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
    u_int32_t i, j;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
    EXTRACT_WORDS(i0, i1, x);
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
    j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
    if (j0 < 20) {
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
        if (j0 < 0) {           /* raise inexact if x != 0 */
3337
9ac6f0782dd6 Fixed bug #814
Sam Lantinga <slouken@libsdl.org>
parents: 3162
diff changeset
    50
            if (huge_val + x > 0.0) {       /* return 0*sign(x) if |x|<1 */
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
                if (i0 >= 0) {
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
                    i0 = i1 = 0;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
                } else if (((i0 & 0x7fffffff) | i1) != 0) {
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
                    i0 = 0xbff00000;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
                    i1 = 0;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
                }
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
            }
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
        } else {
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
            i = (0x000fffff) >> j0;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
            if (((i0 & i) | i1) == 0)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
                return x;       /* x is integral */
3337
9ac6f0782dd6 Fixed bug #814
Sam Lantinga <slouken@libsdl.org>
parents: 3162
diff changeset
    62
            if (huge_val + x > 0.0) {       /* raise inexact flag */
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
                if (i0 < 0)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
                    i0 += (0x00100000) >> j0;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
                i0 &= (~i);
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
                i1 = 0;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
            }
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
        }
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    } else if (j0 > 51) {
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
        if (j0 == 0x400)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
            return x + x;       /* inf or NaN */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
        else
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
            return x;           /* x is integral */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
    } else {
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
        i = ((u_int32_t) (0xffffffff)) >> (j0 - 20);
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
        if ((i1 & i) == 0)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
            return x;           /* x is integral */
3337
9ac6f0782dd6 Fixed bug #814
Sam Lantinga <slouken@libsdl.org>
parents: 3162
diff changeset
    78
        if (huge_val + x > 0.0) {   /* raise inexact flag */
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
            if (i0 < 0) {
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
                if (j0 == 20)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
                    i0 += 1;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
                else {
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
                    j = i1 + (1 << (52 - j0));
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2760
diff changeset
    84
                    if (j < (u_int32_t) i1)
2758
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
                        i0 += 1;        /* got a carry */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
                    i1 = j;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
                }
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
            }
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
            i1 &= (~i);
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
        }
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
    }
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
    INSERT_WORDS(x, i0, i1);
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
    return x;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
}
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
libm_hidden_def(floor)