Publié par kod128 le 30/01/2007
Petite librairie asm pour faire des maths avec la FPU. Destinée, a terme à etre inclus dans du code de plus haut niveau. Code NASM (32 bits) pouvant fonctionner sur toute plateforme INTEL-like (tout OS confondu).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
segment .data segment .text global __maths_abs global __maths_mod global __maths_sin global __maths_cos global __maths_tan ; p-e faux a la fin pour les fx trig hyperboliques global __maths_sinh ; sh(x) = (exp(x) - exp(-x)) / 2 global __maths_cosh ; ch(x) = (exp(x) + exp(-x)) / 2 global __maths_tanh ; th(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)) ; asin et acos a verifier global __maths_asin global __maths_acos global __maths_atan global __maths_atan2 ; devrais être OK pour les log global __maths_ln global __maths_log global __maths_sqrt ; double _maths_abs(double x) __maths_abs: push ebp ; Sauvegarde ebp mov ebp, esp ; fld qword [ebp+8] ; Stack : x fabs ; Stack : -x pop ebp ; ret 0 ; double _maths_mod(double x, double y) __maths_mod: push ebp ; Sauvegarde ebp mov ebp, esp ; fld qword [ebp+16] ; x fld qword [ebp+8] ; y L00: fprem ; Reste de la division (partiel) fstsw ax ; Etat de la FPU test ax, 0400h ; mod fini? jnz L00 fstp st1 pop ebp ; ret 0 __maths_sin: push ebp ; Sauvegarde ebp mov ebp, esp ; sub esp, 8 ; Alloue un double (le x en argument) fld qword [ebp+8] ; Stack : x fsin ; Stack : sin(x) mov esp, ebp ; Désalloue le double pop ebp ; ret 0 ; double _maths_cos( double x) __maths_cos: push ebp ; Sauvegarde ebp mov ebp, esp ; sub esp, 8 ; Alloue un double (le x en argument) fld qword [ebp+8] ; Stack : x fcos ; Stack : cos(x) mov esp, ebp ; Désalloue le double pop ebp ; ret 0 ; double _maths_tan( double x) __maths_tan: push ebp ; Sauvegarde ebp mov ebp, esp sub esp, 8 ; Alloue un double (le x en argument) fld qword [ebp+8] ; Stack : x fptan ; Stack : tan(x) mov esp, ebp ; Désalloue le double pop ebp ret 0 ; double _maths_sinh( double x) // Rem : ln2(x) : Logarithme base 2 de x __maths_sinh: push ebp ; Sauvegarde ebp mov ebp, esp sub esp, 8 ; Alloc x fld qword [ebp+8] ; Stack : x fchs ; Stack : -x fldl2e ; ln2(e) / -x fmulp st1, st0 ; x*ln2(e) fst st1 ; copie dans st1 frndint ; arrondi st0 fsub st1, st0 ; st1 = st1 - st0 fxch ; st0 = st1 et st1 = st0 f2xm1 fld1 fadd st0 fscale ; pow2 fstp st1 ; push st1 fstp qword [ebp-8] ; Sauvegarde exp(-x) fld qword [ebp+8] ; Recharge x fldl2e ; Même chose qu'avant mais avec x fmulp st1, st0 ; au lieu de -x fst st1 frndint fsub st1, st0 fxch f2xm1 fld1 fadd st0 fscale fstp st1 ; exp(-x) => st1 fld qword [ebp-8] ; exp(x) => st0 fsub st1 ; exp(x) - exp(-x) => st0 fld1 ; 1 => st0 / st1 fld1 ; 1 => st0 / st1 / st2 faddp st1 ; 1 + 1 => 2 st0 / st1 fdivp st1 ; sinh = [exp(x) - exp(-x)] / 2 mov esp, ebp ; fini!! (ouf :p) pop ebp ret ; double _maths_cosh( double x) __maths_cosh: push ebp ; Sauvegarde ebp mov ebp, esp ; sub esp, 8 ; Alloc x fld qword [ebp+8] ; Stack : x fchs ; Stack : -x fldl2e ; ln2(e) / -x fmulp st1, st0 ; x*ln2(e) fst st1 ; copie dans st1 frndint ; arrondi st0 fsub st1, st0 ; st1 = st1 - st0 fxch ; st0 = st1 et st1 = st0 f2xm1 fld1 fadd st0 fscale ; pow2 fstp st1 ; push st1 fstp qword [ebp-8] ; Sauvegarde exp(-x) fld qword [ebp+8] ; Recharge x fldl2e ; Même chose qu'avant mais avec x fmulp st1, st0 ; au lieu de -x fst st1 frndint fsub st1, st0 fxch f2xm1 fld1 fadd st0 fscale fstp st1 ; exp(-x) => st1 fld qword [ebp-8] ; exp(x) => st0 fadd st1 ; exp(x) + exp(-x) => st0 fld1 ; 1 => st0 / st1 fld1 ; 1 => st0 / st1 / st2 faddp st1 ; 1 + 1 => 2 st0 / st1 fdivp st1 ; cosh = [exp(x) + exp(-x)] : 2 mov esp, ebp pop ebp ret ; double _maths_tanh( double x) __maths_tanh: push ebp ; Sauvegarde ebp mov ebp, esp ; fld qword [ebp+8] ; x fld st0 ; x / x fadd st1 ; 2x / x fldl2e ; ln2(e) / 2x / x fmulp st1, st0 ; x * ln2(e) / x fst st1 ; x * ln2(e) / x * ln2(e) frndint ; (int)x * ln2(e) / x * ln2(e) fsub st1, st0 ; x * ln2(e) - (int)x * ln2(e) / x * ln2(e) fxch ; x * ln2(e) / x * ln2(e) - (int)x * ln2(e) f2xm1 ; 2^(x-1) fld1 fadd st1 ; 2 ^ x fscale ; ^ 2 fstp st1 fld1 fadd st1 ; exp(2*x)+1 fld1 fld1 faddp st1 ; 1 + 1 => 2 fdivr st1 ; 2/(exp(2*x)+1) fld1 fsubr st1 ; tanh pop ebp ret ; double _maths_asin(double x) __maths_asin: push ebp mov ebp,esp fld qword [ebp+8] ; x fld st0 ; x / x fld st0 ; x / x / x fmul st0 ; x² / x / x fld1 ; 1 / x² / x / x fsubr st1 ; 1 - x² / x / x fsqrt ; sqrt(1 - x²) / x / x fpatan ; asin pop ebp ret ; double _maths_acos(double x) __maths_acos: push ebp mov ebp,esp fld qword [ebp+8] ; x fld st0 ; x / x fld st0 ; x / x / x fmul st1 ; x² / x / x fld1 ; 1 / x² / x / x fsubr st1 ; 1 - x² / x / x fsqrt ; sqrt(1 - x²) / x / x fxch ; swap(st0, st1) fpatan ; acos pop ebp ret ; double _maths_atan(double x) __maths_atan: push ebp mov ebp,esp fld qword [ebp+8] ; x fld1 ; 1 fpatan ; atan pop ebp ret ; double _maths_atan(double x, double y) __maths_atan2: push ebp mov ebp,esp fld qword [ebp+8] ; a1 fld qword [ebp+16] ; a2 fpatan ; atan mov esp,ebp pop ebp ret ; double _maths_exp(double x) __maths_exp: push ebp mov ebp,esp sub esp,8 fld qword [ebp+8] fldl2e fmulp st1, st0 fst st1 frndint fsub st1, st0 fxch f2xm1 fld1 fadd st1 fscale fstp st1 fst qword [ebp-8] mov esp,ebp pop ebp ret ; double _maths_ln(double x) __maths_ln: push ebp mov ebp,esp fld qword [ebp+8] fldln2 fxch st1 fyl2x pop ebp ret ; double _maths_log(double x) __maths_log: push ebp mov ebp,esp fld qword [ebp+8] fldlg2 fxch st0 fyl2x pop ebp ret ; double _maths_sqrt(double x) -- Square root __maths_sqrt: push ebp mov ebp,esp fld qword [ebp+8] fsqrt pop ebp ret ; @EOF