]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/ncsw/inc/math_ext.h
MFV r296989:
[FreeBSD/FreeBSD.git] / sys / contrib / ncsw / inc / math_ext.h
1 /* Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of Freescale Semiconductor nor the
12  *       names of its contributors may be used to endorse or promote products
13  *       derived from this software without specific prior written permission.
14  *
15  *
16  * ALTERNATIVELY, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") as published by the Free Software
18  * Foundation, either version 2 of that License or (at your option) any
19  * later version.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef __MATH_EXT_H
34 #define __MATH_EXT_H
35
36
37 #if defined(NCSW_LINUX) && defined(__KERNEL__)
38 #include <linux/math.h>
39
40 #elif defined(__MWERKS__)
41 #define LOW(x) ( sizeof(x)==8 ? *(1+(int32_t*)&x) : (*(int32_t*)&x))
42 #define HIGH(x) (*(int32_t*)&x)
43 #define ULOW(x) ( sizeof(x)==8 ? *(1+(uint32_t*)&x) : (*(uint32_t*)&x))
44 #define UHIGH(x) (*(uint32_t*)&x)
45
46 static const double big = 1.0e300;
47
48 /* Macro for checking if a number is a power of 2 */
49 static __inline__ double ceil(double x)
50 {
51     int32_t i0,i1,j0; /*- cc 020130 -*/
52     uint32_t i,j; /*- cc 020130 -*/
53     i0 =  HIGH(x);
54     i1 =  LOW(x);
55     j0 = ((i0>>20)&0x7ff)-0x3ff;
56     if(j0<20) {
57         if(j0<0) {     /* raise inexact if x != 0 */
58         if(big+x>0.0) {/* return 0*sign(x) if |x|<1 */
59             if(i0<0) {i0=0x80000000;i1=0;}
60             else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
61         }
62         } else {
63         i = (uint32_t)(0x000fffff)>>j0;
64         if(((i0&i)|i1)==0) return x; /* x is integral */
65         if(big+x>0.0) {    /* raise inexact flag */
66             if(i0>0) i0 += (0x00100000)>>j0;
67             i0 &= (~i); i1=0;
68         }
69         }
70     } else if (j0>51) {
71         if(j0==0x400) return x+x;    /* inf or NaN */
72         else return x;        /* x is integral */
73     } else {
74         i = ((uint32_t)(0xffffffff))>>(j0-20); /*- cc 020130 -*/
75         if((i1&i)==0) return x;    /* x is integral */
76         if(big+x>0.0) {         /* raise inexact flag */
77         if(i0>0) {
78             if(j0==20) i0+=1;
79             else {
80             j = (uint32_t)(i1 + (1<<(52-j0)));
81             if(j<i1) i0+=1;    /* got a carry */
82             i1 = (int32_t)j;
83             }
84         }
85         i1 &= (~i);
86         }
87     }
88     HIGH(x) = i0;
89     LOW(x) = i1;
90     return x;
91 }
92
93 #else
94 #include <math.h>
95 #endif /* defined(NCSW_LINUX) && defined(__KERNEL__) */
96
97
98 #endif /* __MATH_EXT_H */