Rev 1962 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1962 | Rev 1968 | ||
---|---|---|---|
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 | //TODO: |
32 | //TODO: |
33 | #define FMATH_MANTISA_MASK ( 0x000fffffffffffffLL ) |
33 | #define FMATH_MANTISA_MASK ( 0x000fffffffffffffLL ) |
34 | 34 | ||
35 | signed short fmath_get_binary_exponent(double num) |
35 | signed short fmath_get_binary_exponent(double num) |
36 | { //TODO: |
36 | { //TODO: |
37 | /* fmath_ld_union_t fmath_ld_union; |
37 | /* fmath_ld_union_t fmath_ld_union; |
38 | fmath_ld_union.bf = num; |
38 | fmath_ld_union.bf = num; |
39 | 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 |
39 | 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 |
40 | */ |
40 | */ |
41 | return 0; |
41 | return 0; |
42 | } |
42 | } |
43 | 43 | ||
44 | double fmath_get_decimal_exponent(double num) |
44 | double fmath_get_decimal_exponent(double num) |
45 | { //TODO: |
45 | { //TODO: |
46 | double value; |
46 | double value; |
47 | // log10(2)*log2(x) => log10(x) |
47 | // log10(2)*log2(x) => log10(x) |
48 | /* __asm__ __volatile__ ( \ |
48 | /* __asm__ __volatile__ ( \ |
49 | "fldlg2 #load log10(2) \n\t" \ |
49 | "fldlg2 #load log10(2) \n\t" \ |
50 | "fxch %%st(1) \n\t" \ |
50 | "fxch %%st(1) \n\t" \ |
51 | "fyl2x #count st(0)*log2(st(1))->st(1); pop st(0) \n\t" \ |
51 | "fyl2x #count st(0)*log2(st(1))->st(1); pop st(0) \n\t" \ |
52 | : "=t" (value) : "0"(num) ); |
52 | : "=t" (value) : "0"(num) ); |
53 | */ return value; |
53 | */ return value; |
54 | 54 | ||
55 | } |
55 | } |
56 | 56 | ||
57 | __u64 fmath_get_binary_mantisa(double num) |
57 | __u64 fmath_get_binary_mantisa(double num) |
58 | { //TODO: |
58 | { //TODO: |
59 | /* union { __u64 _u; double _d;} un = { _d : num }; |
59 | /* union { __u64 _u; double _d;} un = { _d : num }; |
60 | un._u=un._u &(FMATH_MANTISA_MASK); // mask 52 bits of mantisa |
60 | un._u=un._u &(FMATH_MANTISA_MASK); // mask 52 bits of mantisa |
61 | return un._u; |
61 | return un._u; |
62 | */ |
62 | */ |
63 | return 0; |
63 | return 0; |
64 | } |
64 | } |
65 | 65 | ||
66 | double fmath_fint(double num, double *intp) |
66 | double fmath_fint(double num, double *intp) |
67 | { //TODO: |
67 | { //TODO: |
68 | /* fmath_ld_union_t fmath_ld_union_num; |
68 | /* fmath_ld_union_t fmath_ld_union_num; |
69 | fmath_ld_union_t fmath_ld_union_int; |
69 | fmath_ld_union_t fmath_ld_union_int; |
70 | signed short exp; |
70 | signed short exp; |
71 | __u64 mask,mantisa; |
71 | __u64 mask,mantisa; |
72 | int i; |
72 | int i; |
73 | |
73 | |
74 | exp=fmath_get_binary_exponent(num); |
74 | exp=fmath_get_binary_exponent(num); |
75 | |
75 | |
76 | if (exp<0) { |
76 | if (exp<0) { |
77 | *intp = 0.0; |
77 | *intp = 0.0; |
78 | *intp = fmath_set_sign(0.0L,fmath_is_negative(num)); |
78 | *intp = fmath_set_sign(0.0L,fmath_is_negative(num)); |
79 | return num; |
79 | return num; |
80 | } |
80 | } |
81 | |
81 | |
82 | 82 | ||
83 | if (exp>51) { |
83 | if (exp>51) { |
84 | *intp=num; |
84 | *intp=num; |
85 | num=0.0; |
85 | num=0.0; |
86 | num= fmath_set_sign(0.0L,fmath_is_negative(*intp)); |
86 | num= fmath_set_sign(0.0L,fmath_is_negative(*intp)); |
87 | return num; |
87 | return num; |
88 | } |
88 | } |
89 | |
89 | |
90 | fmath_ld_union_num.bf = num; |
90 | fmath_ld_union_num.bf = num; |
91 | |
91 | |
92 | mask = FMATH_MANTISA_MASK>>exp; |
92 | mask = FMATH_MANTISA_MASK>>exp; |
93 | //mantisa = (fmath_get-binary_mantisa(num))&(~mask); |
93 | //mantisa = (fmath_get-binary_mantisa(num))&(~mask); |
94 | |
94 | |
95 | for (i=0;i<7;i++) { |
95 | for (i=0;i<7;i++) { |
96 | // Ugly construction for obtain sign, exponent and integer part from num |
96 | // Ugly construction for obtain sign, exponent and integer part from num |
97 | fmath_ld_union_int.ldd[i]=fmath_ld_union_num.ldd[i]&(((~mask)>>(i*8))&0xff); |
97 | fmath_ld_union_int.ldd[i]=fmath_ld_union_num.ldd[i]&(((~mask)>>(i*8))&0xff); |
98 | } |
98 | } |
99 | |
99 | |
100 | fmath_ld_union_int.ldd[6]|=((fmath_ld_union_num.ldd[6])&(0xf0)); |
100 | fmath_ld_union_int.ldd[6]|=((fmath_ld_union_num.ldd[6])&(0xf0)); |
101 | fmath_ld_union_int.ldd[7]=fmath_ld_union_num.ldd[7]; |
101 | fmath_ld_union_int.ldd[7]=fmath_ld_union_num.ldd[7]; |
102 | |
102 | |
103 | *intp=fmath_ld_union_int.bf; |
103 | *intp=fmath_ld_union_int.bf; |
104 | return fmath_ld_union_num.bf-fmath_ld_union_int.bf; |
104 | return fmath_ld_union_num.bf-fmath_ld_union_int.bf; |
105 | */ |
105 | */ |
106 | 106 | ||
107 | return 0.0; |
107 | return 0.0; |
108 | }; |
108 | }; |
109 | 109 | ||
110 | 110 | ||
111 | double fmath_dpow(double base, double exponent) |
111 | double fmath_dpow(double base, double exponent) |
112 | { //TODO: |
112 | { //TODO: |
113 | /* double value=1.0; |
113 | /* double value=1.0; |
114 | if (base<=0.0) return base; |
114 | if (base<=0.0) return base; |
115 | |
115 | |
116 | //2^(x*log2(10)) = 2^y = 10^x |
116 | //2^(x*log2(10)) = 2^y = 10^x |
117 | |
117 | |
118 | __asm__ __volatile__ ( \ |
118 | __asm__ __volatile__ ( \ |
119 | "fyl2x # ST(1):=ST(1)*log2(ST(0)), pop st(0) \n\t " \ |
119 | "fyl2x # ST(1):=ST(1)*log2(ST(0)), pop st(0) \n\t " \ |
120 | "fld %%st(0) \n\t" \ |
120 | "fld %%st(0) \n\t" \ |
121 | "frndint \n\t" \ |
121 | "frndint \n\t" \ |
122 | "fxch %%st(1) \n\t" \ |
122 | "fxch %%st(1) \n\t" \ |
123 | "fsub %%st(1),%%st(0) \n\t" \ |
123 | "fsub %%st(1),%%st(0) \n\t" \ |
124 | "f2xm1 # ST := 2^ST -1\n\t" \ |
124 | "f2xm1 # ST := 2^ST -1\n\t" \ |
125 | "fld1 \n\t" \ |
125 | "fld1 \n\t" \ |
126 | "faddp %%st(0),%%st(1) \n\t" \ |
126 | "faddp %%st(0),%%st(1) \n\t" \ |
127 | "fscale #ST:=ST*2^(ST(1))\n\t" \ |
127 | "fscale #ST:=ST*2^(ST(1))\n\t" \ |
128 | "fstp %%st(1) \n\t" \ |
128 | "fstp %%st(1) \n\t" \ |
129 | "" : "=t" (value) : "0" (base), "u" (exponent) ); |
129 | "" : "=t" (value) : "0" (base), "u" (exponent) ); |
130 | return value; |
130 | return value; |
131 | */ |
131 | */ |
132 | return 1.0; |
132 | return 1.0; |
133 | } |
133 | } |
134 | 134 | ||
135 | 135 | ||
136 | int fmath_is_nan(double num) |
136 | int fmath_is_nan(double num) |
137 | { |
137 | { |
138 | /* __u16 exp; |
138 | /* __u16 exp; |
139 | fmath_ld_union_t fmath_ld_union; |
139 | fmath_ld_union_t fmath_ld_union; |
140 | fmath_ld_union.bf = num; |
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 |
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 | 142 | ||
143 | if (exp!=0x07ff) return 0; |
143 | if (exp!=0x07ff) return 0; |
144 | if (fmath_get_binary_mantisa(num)>=FMATH_NAN) return 1; |
144 | if (fmath_get_binary_mantisa(num)>=FMATH_NAN) return 1; |
145 | |
145 | |
146 | */ |
146 | */ |
147 | return 0; |
147 | return 0; |
148 | } |
148 | } |
149 | 149 | ||
150 | int fmath_is_infinity(double num) |
150 | int fmath_is_infinity(double num) |
151 | { |
151 | { |
152 | /* __u16 exp; |
152 | /* __u16 exp; |
153 | fmath_ld_union_t fmath_ld_union; |
153 | fmath_ld_union_t fmath_ld_union; |
154 | fmath_ld_union.bf = num; |
154 | fmath_ld_union.bf = num; |
155 | 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 |
155 | 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 |
156 | 156 | ||
157 | if (exp!=0x07ff) return 0; |
157 | if (exp!=0x07ff) return 0; |
158 | if (fmath_get_binary_mantisa(num)==0x0) return 1; |
158 | if (fmath_get_binary_mantisa(num)==0x0) return 1; |
159 | */ return 0; |
159 | */ return 0; |
160 | } |
160 | } |
161 | 161 | ||
162 | 162 |