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