]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Math/s_floor.c
ArmPkg: ArmLib: purge incorrect ArmDrainWriteBuffer () alias
[mirror_edk2.git] / StdLib / LibC / Math / s_floor.c
CommitLineData
2aa62f2b 1/* @(#)s_floor.c 5.1 93/09/24 */\r
2/*\r
3 * ====================================================\r
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\r
5 *\r
6 * Developed at SunPro, a Sun Microsystems, Inc. business.\r
7 * Permission to use, copy, modify, and distribute this\r
8 * software is freely granted, provided that this notice\r
9 * is preserved.\r
10 * ====================================================\r
11 */\r
12#include <LibConfig.h>\r
13#include <sys/EfiCdefs.h>\r
14#if defined(LIBM_SCCS) && !defined(lint)\r
15__RCSID("$NetBSD: s_floor.c,v 1.11 2002/05/26 22:01:56 wiz Exp $");\r
16#endif\r
17\r
18/*\r
19 * floor(x)\r
20 * Return x rounded toward -inf to integral value\r
21 * Method:\r
22 * Bit twiddling.\r
23 * Exception:\r
24 * Inexact flag raised if x not equal to floor(x).\r
25 */\r
26\r
27#include "math.h"\r
28#include "math_private.h"\r
29\r
30static const double huge = 1.0e300;\r
31\r
32double\r
33floor(double x)\r
34{\r
35 int32_t i0,i1,j0;\r
36 u_int32_t i,j;\r
37 EXTRACT_WORDS(i0,i1,x);\r
38 j0 = ((i0>>20)&0x7ff)-0x3ff;\r
39 if(j0<20) {\r
40 if(j0<0) { /* raise inexact if x != 0 */\r
41 if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */\r
42 if(i0>=0) {i0=i1=0;}\r
43 else if(((i0&0x7fffffff)|i1)!=0)\r
44 { i0=0xbff00000;i1=0;}\r
45 }\r
46 } else {\r
47 i = (0x000fffff)>>j0;\r
48 if(((i0&i)|i1)==0) return x; /* x is integral */\r
49 if(huge+x>0.0) { /* raise inexact flag */\r
50 if(i0<0) i0 += (0x00100000)>>j0;\r
51 i0 &= (~i); i1=0;\r
52 }\r
53 }\r
54 } else if (j0>51) {\r
55 if(j0==0x400) return x+x; /* inf or NaN */\r
56 else return x; /* x is integral */\r
57 } else {\r
58 i = ((u_int32_t)(0xffffffff))>>(j0-20);\r
59 if((i1&i)==0) return x; /* x is integral */\r
60 if(huge+x>0.0) { /* raise inexact flag */\r
61 if(i0<0) {\r
62 if(j0==20) i0+=1;\r
63 else {\r
64 j = i1+(1<<(52-j0));\r
65 if((int32_t)j<i1) i0 +=1 ; /* got a carry */\r
66 i1=j;\r
67 }\r
68 }\r
69 i1 &= (~i);\r
70 }\r
71 }\r
72 INSERT_WORDS(x,i0,i1);\r
73 return x;\r
74}\r