Rev 230 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 230 | Rev 266 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Josef Cejka |
2 | * Copyright (C) 2005 Josef Cejka |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <arch/fmath.h> |
29 | #include <arch/fmath.h> |
30 | #include <print.h> |
30 | #include <print.h> |
31 | 31 | ||
32 | #define FMATH_MANTISA_MASK ( 0x000fffffffffffffLL ) |
32 | #define FMATH_MANTISA_MASK ( 0x000fffffffffffffLL ) |
33 | - | ||
34 | int fmath_is_negative(double num) |
33 | #define FMATH_NAN ( 0x0001000000000001LL ) |
35 | { |
- | |
36 | fmath_ld_union_t fmath_ld_union; |
- | |
37 | fmath_ld_union.bf = num; |
- | |
38 | return ((fmath_ld_union.ldd[7])&0x80)==0x80; /*first bit is sign, IA32 is little endian -> 8th byte*/ |
- | |
39 | - | ||
40 | } |
- | |
41 | - | ||
42 | signed short fmath_get_binary_exponent(double num) |
34 | signed short fmath_get_binary_exponent(double num) |
43 | { |
35 | { |
44 | fmath_ld_union_t fmath_ld_union; |
36 | fmath_ld_union_t fmath_ld_union; |
45 | fmath_ld_union.bf = num; |
37 | fmath_ld_union.bf = num; |
46 | return (signed short)((((fmath_ld_union.ldd[7])&0x7f)<<4) + (((fmath_ld_union.ldd[6])&0xf0)>>4)) -FMATH_EXPONENT_BIAS; /* exponent is 11 bits lenght, so sevent bits is in 8th byte and 4 bits in 7th */ |
38 | return (signed short)((((fmath_ld_union.ldd[7])&0x7f)<<4) + (((fmath_ld_union.ldd[6])&0xf0)>>4)) -FMATH_EXPONENT_BIAS; /* exponent is 11 bits lenght, so sevent bits is in 8th byte and 4 bits in 7th */ |
47 | } |
39 | } |
48 | 40 | ||
49 | double fmath_get_decimal_exponent(double num) |
41 | double fmath_get_decimal_exponent(double num) |
50 | { |
42 | { |
51 | double value; |
43 | double value; |
52 | /* log10(2)*log2(x) => log10(x) */ |
44 | /* log10(2)*log2(x) => log10(x) */ |
53 | __asm__ __volatile__ ( \ |
45 | __asm__ __volatile__ ( \ |
54 | "fldlg2 #load log10(2) \n\t" \ |
46 | "fldlg2 #load log10(2) \n\t" \ |
55 | "fxch %%st(1) \n\t" \ |
47 | "fxch %%st(1) \n\t" \ |
56 | "fyl2x #count st(0)*log2(st(1))->st(1); pop st(0) \n\t" \ |
48 | "fyl2x #count st(0)*log2(st(1))->st(1); pop st(0) \n\t" \ |
57 | : "=t" (value) : "0"(num) ); |
49 | : "=t" (value) : "0"(num) ); |
58 | return value; |
50 | return value; |
59 | } |
51 | } |
60 | 52 | ||
61 | __u64 fmath_get_binary_mantisa(double num) |
53 | __u64 fmath_get_binary_mantisa(double num) |
62 | { |
54 | { |
63 | union { __u64 _u; double _d;} un = { _d : num }; |
55 | union { __u64 _u; double _d;} un = { _d : num }; |
64 | un._u=un._u &(FMATH_MANTISA_MASK); /* mask 52 bits of mantisa*/ |
56 | un._u=un._u &(FMATH_MANTISA_MASK); /* mask 52 bits of mantisa*/ |
65 | return un._u; |
57 | return un._u; |
66 | } |
58 | } |
67 | 59 | ||
68 | double fmath_fint(double num, double *intp) |
60 | double fmath_fint(double num, double *intp) |
69 | { |
61 | { |
70 | fmath_ld_union_t fmath_ld_union_num; |
62 | fmath_ld_union_t fmath_ld_union_num; |
71 | fmath_ld_union_t fmath_ld_union_int; |
63 | fmath_ld_union_t fmath_ld_union_int; |
72 | signed short exp; |
64 | signed short exp; |
73 | __u64 mask,mantisa; |
65 | __u64 mask,mantisa; |
74 | int i; |
66 | int i; |
75 | 67 | ||
76 | exp=fmath_get_binary_exponent(num); |
68 | exp=fmath_get_binary_exponent(num); |
77 | 69 | ||
78 | if (exp<0) { |
70 | if (exp<0) { |
79 | *intp = 0.0; |
71 | *intp = 0.0; |
80 | *intp = fmath_set_sign(0.0L,fmath_is_negative(num)); |
- | |
81 | return num; |
72 | return num; |
82 | } |
73 | } |
83 | 74 | ||
84 | 75 | ||
85 | if (exp>51) { |
76 | if (exp>51) { |
86 | *intp=num; |
77 | *intp=num; |
87 | num=0.0; |
78 | num=0.0; |
88 | num= fmath_set_sign(0.0L,fmath_is_negative(*intp)); |
- | |
89 | return num; |
79 | return num; |
90 | } |
80 | } |
91 | 81 | ||
92 | fmath_ld_union_num.bf = num; |
82 | fmath_ld_union_num.bf = num; |
93 | 83 | ||
94 | mask = FMATH_MANTISA_MASK>>exp; |
84 | mask = FMATH_MANTISA_MASK>>exp; |
95 | //mantisa = (fmath_get-binary_mantisa(num))&(~mask); |
85 | //mantisa = (fmath_get-binary_mantisa(num))&(~mask); |
96 | 86 | ||
97 | for (i=0;i<7;i++) { |
87 | for (i=0;i<7;i++) { |
98 | /* Ugly construction for obtain sign, exponent and integer part from num */ |
88 | /* Ugly construction for obtain sign, exponent and integer part from num */ |
99 | fmath_ld_union_int.ldd[i]=fmath_ld_union_num.ldd[i]&(((~mask)>>(i*8))&0xff); |
89 | fmath_ld_union_int.ldd[i]=fmath_ld_union_num.ldd[i]&(((~mask)>>(i*8))&0xff); |
100 | } |
90 | } |
101 | 91 | ||
102 | fmath_ld_union_int.ldd[6]|=((fmath_ld_union_num.ldd[6])&(0xf0)); |
92 | fmath_ld_union_int.ldd[6]|=((fmath_ld_union_num.ldd[6])&(0xf0)); |
103 | fmath_ld_union_int.ldd[7]=fmath_ld_union_num.ldd[7]; |
93 | fmath_ld_union_int.ldd[7]=fmath_ld_union_num.ldd[7]; |
104 | 94 | ||
105 | *intp=fmath_ld_union_int.bf; |
95 | *intp=fmath_ld_union_int.bf; |
106 | return fmath_ld_union_num.bf-fmath_ld_union_int.bf; |
96 | return fmath_ld_union_num.bf-fmath_ld_union_int.bf; |
107 | }; |
97 | }; |
108 | 98 | ||
109 | double fmath_set_sign(double num,__u8 sign) |
- | |
110 | { |
- | |
111 | fmath_ld_union_t fmath_ld_union; |
- | |
112 | fmath_ld_union.bf = num; |
- | |
113 | fmath_ld_union.ldd[7]=((fmath_ld_union.ldd[7])&0x7f)|(sign<<7); /* change 64th bit (IA32 is a little endian)*/ |
- | |
114 | return fmath_ld_union.bf; |
- | |
115 | } |
- | |
116 | - | ||
117 | double fmath_abs(double num) |
- | |
118 | { |
- | |
119 | return fmath_set_sign(num,0); |
- | |
120 | } |
- | |
121 | 99 | ||
122 | double fmath_dpow(double base, double exponent) |
100 | double fmath_dpow(double base, double exponent) |
123 | { |
101 | { |
124 | double value=1.0; |
102 | double value=1.0; |
125 | if (base<=0.0) return base; |
103 | if (base<=0.0) return base; |
126 | 104 | ||
127 | //2^(x*log2(10)) = 2^y = 10^x |
105 | //2^(x*log2(10)) = 2^y = 10^x |
128 | 106 | ||
129 | __asm__ __volatile__ ( \ |
107 | __asm__ __volatile__ ( \ |
130 | "fyl2x # ST(1):=ST(1)*log2(ST(0)), pop st(0) \n\t " \ |
108 | "fyl2x # ST(1):=ST(1)*log2(ST(0)), pop st(0) \n\t " \ |
131 | "fld %%st(0) \n\t" \ |
109 | "fld %%st(0) \n\t" \ |
132 | "frndint \n\t" \ |
110 | "frndint \n\t" \ |
133 | "fxch %%st(1) \n\t" \ |
111 | "fxch %%st(1) \n\t" \ |
134 | "fsub %%st(1),%%st(0) \n\t" \ |
112 | "fsub %%st(1),%%st(0) \n\t" \ |
135 | "f2xm1 # ST := 2^ST -1\n\t" \ |
113 | "f2xm1 # ST := 2^ST -1\n\t" \ |
136 | "fld1 \n\t" \ |
114 | "fld1 \n\t" \ |
137 | "faddp %%st(0),%%st(1) \n\t" \ |
115 | "faddp %%st(0),%%st(1) \n\t" \ |
138 | "fscale #ST:=ST*2^(ST(1))\n\t" \ |
116 | "fscale #ST:=ST*2^(ST(1))\n\t" \ |
139 | "fstp %%st(1) \n\t" \ |
117 | "fstp %%st(1) \n\t" \ |
140 | "" : "=t" (value) : "0" (base), "u" (exponent) ); |
118 | "" : "=t" (value) : "0" (base), "u" (exponent) ); |
141 | return value; |
119 | return value; |
142 | } |
120 | } |
143 | 121 | ||
- | 122 | int fmath_is_nan(double num) |
|
- | 123 | { |
|
- | 124 | __u16 exp; |
|
- | 125 | fmath_ld_union_t fmath_ld_union; |
|
- | 126 | fmath_ld_union.bf = num; |
|
- | 127 | exp=(((fmath_ld_union.ldd[7])&0x7f)<<4) + (((fmath_ld_union.ldd[6])&0xf0)>>4); /* exponent is 11 bits lenght, so sevent bits is in 8th byte and 4 bits in 7th */ |
|
- | 128 | ||
- | 129 | if (exp!=0x07ff) return 0; |
|
- | 130 | if (fmath_get_binary_mantisa(num)>=FMATH_NAN) return 1; |
|
- | 131 | ||
- | 132 | ||
- | 133 | return 0; |
|
- | 134 | } |
|
- | 135 | ||
- | 136 | int fmath_is_infinity(double num) |
|
- | 137 | { |
|
- | 138 | __u16 exp; |
|
- | 139 | fmath_ld_union_t fmath_ld_union; |
|
- | 140 | fmath_ld_union.bf = num; |
|
- | 141 | exp=(((fmath_ld_union.ldd[7])&0x7f)<<4) + (((fmath_ld_union.ldd[6])&0xf0)>>4); /* exponent is 11 bits lenght, so sevent bits is in 8th byte and 4 bits in 7th */ |
|
- | 142 | ||
- | 143 | if (exp!=0x07ff) return 0; |
|
- | 144 | if (fmath_get_binary_mantisa(num)==0x0) return 1; |
|
- | 145 | return 0; |
|
- | 146 | } |
|
144 | 147 |