annotate src/libm/s_floor.c @ 2978:4c9130d66e2c

Added a few things to the TODO list. Unit testing is becoming more and more important the more features are added and the more combinations of factors there are to test.
author Sam Lantinga <slouken@libsdl.org>
date Sat, 03 Jan 2009 07:23:11 +0000
parents f55c87ae336b
children dc1eb82ffdaa
rev   line source
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)
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14 static char rcsid[] =
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
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 #include "math.h"
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__
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 static const double huge = 1.0e300;
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 #else
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 static double huge = 1.0e300;
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 */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */
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 */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 if (huge + x > 0.0) { /* raise inexact flag */
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 */
045d9976f285 Yet more math...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 if (huge + x > 0.0) { /* raise inexact flag */
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)