1
2 .file "wm_sqrt.S"
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 #include "exception.h"
30 #include "fpu_emu.h"
31
32
33 #ifndef NON_REENTRANT_FPU
34
35 #define FPU_accum_3 -4(%ebp)
36 #define FPU_accum_2 -8(%ebp)
37 #define FPU_accum_1 -12(%ebp)
38 #define FPU_accum_0 -16(%ebp)
39
40
41
42
43
44
45
46 #define FPU_fsqrt_arg_2 -20(%ebp)
47 #define FPU_fsqrt_arg_1 -24(%ebp)
48 #define FPU_fsqrt_arg_0 -28(%ebp)
49
50 #else
51
52 .data
53 .align 4,0
54 FPU_accum_3:
55 .long 0
56 FPU_accum_2:
57 .long 0
58 FPU_accum_1:
59 .long 0
60 FPU_accum_0:
61 .long 0
62
63
64
65
66
67
68 FPU_fsqrt_arg_2:
69 .long 0
70 FPU_fsqrt_arg_1:
71 .long 0
72 FPU_fsqrt_arg_0:
73 .long 0
74 #endif
75
76
77 .text
78 ENTRY(wm_sqrt)
79 pushl %ebp
80 movl %esp,%ebp
81 #ifndef NON_REENTRANT_FPU
82 subl $28,%esp
83 #endif
84 pushl %esi
85 pushl %edi
86 pushl %ebx
87
88 movl PARAM1,%esi
89
90 movl SIGH(%esi),%eax
91 movl SIGL(%esi),%ecx
92 xorl %edx,%edx
93
94
95
96 cmpw EXP_BIAS,EXP(%esi)
97 jnz sqrt_arg_ge_2
98
99 shrl $1,%eax
100 rcrl $1,%ecx
101 rcrl $1,%edx
102
103 sqrt_arg_ge_2:
104
105
106
107 movl %eax,FPU_fsqrt_arg_2
108 movl %ecx,FPU_fsqrt_arg_1
109 movl %edx,FPU_fsqrt_arg_0
110
111
112 shrl $1,%eax
113 addl $0x40000000,%eax
114 movl $0xaaaaaaaa,%ecx
115 mull %ecx
116 shll %edx
117 testl $0x80000000,%edx
118 jnz sqrt_prelim_no_adjust
119
120 movl $0x80000000,%edx
121
122 sqrt_prelim_no_adjust:
123 movl %edx,%esi
124
125
126
127
128 movl FPU_fsqrt_arg_2,%ecx
129
130
131
132
133
134
135
136
137 shrl %ecx
138
139
140 movl %ecx,%edx
141 divl %esi
142 shrl %esi
143 addl %eax,%esi
144
145 movl %ecx,%edx
146 divl %esi
147 shrl %esi
148 addl %eax,%esi
149
150 movl %ecx,%edx
151 divl %esi
152 shrl %esi
153 addl %eax,%esi
154
155
156
157
158
159
160
161
162
163
164 movl %esi,%eax
165 mull %esi
166
167
168 movl FPU_fsqrt_arg_1,%ecx
169 subl %ecx,%eax
170 movl FPU_fsqrt_arg_2,%ecx
171 sbbl %ecx,%edx
172 jnc sqrt_stage_2_positive
173
174
175
176 notl %edx
177 notl %eax
178 addl $1,%eax
179 adcl $0,%edx
180
181 divl %esi
182 movl %eax,%ecx
183
184 movl %edx,%eax
185 divl %esi
186 jmp sqrt_stage_2_finish
187
188 sqrt_stage_2_positive:
189 divl %esi
190 movl %eax,%ecx
191
192 movl %edx,%eax
193 divl %esi
194
195 notl %ecx
196 notl %eax
197 addl $1,%eax
198 adcl $0,%ecx
199
200 sqrt_stage_2_finish:
201 sarl $1,%ecx
202 rcrl $1,%eax
203
204
205 movl %eax,%edi
206 addl %ecx,%esi
207
208 jnz sqrt_stage_2_done
209
210 #ifdef PARANOID
211
212 cmp $0xffffffff,FPU_fsqrt_arg_1
213 jnz sqrt_stage_2_error
214 #endif
215
216
217 xorl %eax,%eax
218 decl %eax
219 movl %eax,%edi
220 movl %eax,%esi
221 movl $0x7fffffff,%eax
222 jmp sqrt_round_result
223
224 #ifdef PARANOID
225 sqrt_stage_2_error:
226 pushl EX_INTERNAL|0x213
227 call EXCEPTION
228 #endif
229
230 sqrt_stage_2_done:
231
232
233
234
235 movl %edi,%eax
236 mull %edi
237 movl %edx,FPU_accum_1
238
239 movl %esi,%eax
240 mull %esi
241 movl %edx,FPU_accum_3
242 movl %eax,FPU_accum_2
243
244 movl %edi,%eax
245 mull %esi
246 addl %eax,FPU_accum_1
247 adcl %edx,FPU_accum_2
248 adcl $0,FPU_accum_3
249
250
251
252 addl %eax,FPU_accum_1
253 adcl %edx,FPU_accum_2
254 adcl $0,FPU_accum_3
255
256
257
258 movl FPU_fsqrt_arg_0,%eax
259 subl %eax,FPU_accum_1
260 movl FPU_fsqrt_arg_1,%eax
261 sbbl %eax,FPU_accum_2
262 movl FPU_fsqrt_arg_2,%eax
263 sbbl %eax,FPU_accum_3
264 jnc sqrt_stage_3_positive
265
266
267
268 notl FPU_accum_1
269 notl FPU_accum_2
270 notl FPU_accum_3
271 addl $1,FPU_accum_1
272 adcl $0,FPU_accum_2
273
274 #ifdef PARANOID
275 adcl $0,FPU_accum_3
276 jz sqrt_stage_3_no_error
277
278 sqrt_stage_3_error:
279 pushl EX_INTERNAL|0x207
280 call EXCEPTION
281
282 sqrt_stage_3_no_error:
283 #endif
284
285 movl FPU_accum_2,%edx
286 movl FPU_accum_1,%eax
287 divl %esi
288 movl %eax,%ecx
289
290 movl %edx,%eax
291 divl %esi
292
293 sarl $1,%ecx
294 rcrl $1,%eax
295
296
297
298 addl %ecx,%edi
299 adcl $0,%esi
300
301 jmp sqrt_stage_3_finished
302
303 sqrt_stage_3_positive:
304 movl FPU_accum_2,%edx
305 movl FPU_accum_1,%eax
306 divl %esi
307 movl %eax,%ecx
308
309 movl %edx,%eax
310 divl %esi
311
312 sarl $1,%ecx
313 rcrl $1,%eax
314
315
316
317 notl %eax
318 notl %ecx
319 addl $1,%eax
320 adcl $0,%ecx
321 adcl $0xffffffff,%esi
322
323 addl %ecx,%edi
324 adcl $0,%esi
325
326 sqrt_stage_3_finished:
327
328
329
330
331
332
333 cmpl $0xffffffe0,%eax
334 ja sqrt_near_exact_x
335
336 cmpl $0x00000020,%eax
337 jb sqrt_near_exact
338
339 cmpl $0x7fffffe0,%eax
340 jb sqrt_round_result
341
342 cmpl $0x80000020,%eax
343 jb sqrt_get_more_precision
344
345 sqrt_round_result:
346
347 movl %eax,%edx
348 movl %esi,%eax
349 movl %edi,%ebx
350 movl PARAM1,%edi
351 movw EXP_BIAS,EXP(%edi)
352 jmp fpu_reg_round
353
354
355 sqrt_near_exact_x:
356
357 addl $1,%edi
358 adcl $0,%esi
359
360 sqrt_near_exact:
361
362
363
364
365
366
367
368 movl %edi,%eax
369 mull %edi
370 movl %edx,%ebx
371 movl %eax,%ecx
372
373 movl %edi,%eax
374 mull %esi
375 addl %eax,%ebx
376 addl %eax,%ebx
377
378 #ifdef PARANOID
379 cmp $0xffffffb0,%ebx
380 jb sqrt_near_exact_ok
381
382 cmp $0x00000050,%ebx
383 ja sqrt_near_exact_ok
384
385 pushl EX_INTERNAL|0x214
386 call EXCEPTION
387
388 sqrt_near_exact_ok:
389 #endif
390
391 or %ebx,%ebx
392 js sqrt_near_exact_small
393
394 jnz sqrt_near_exact_large
395
396 or %ebx,%edx
397 jnz sqrt_near_exact_large
398
399
400 xorl %eax,%eax
401 jmp sqrt_round_result
402
403 sqrt_near_exact_small:
404
405 movl $0x000000ff,%eax
406 jmp sqrt_round_result
407
408 sqrt_near_exact_large:
409
410 subl $1,%edi
411 sbbl $0,%esi
412 movl $0xffffff00,%eax
413 jmp sqrt_round_result
414
415
416 sqrt_get_more_precision:
417
418
419 stc
420 rcll $1,%edi
421 rcll $1,%esi
422
423 movl %edi,%eax
424 mull %edi
425 movl %edx,%ebx
426 movl %eax,%ecx
427
428 movl %edi,%eax
429 mull %esi
430 addl %eax,%ebx
431 addl %eax,%ebx
432
433
434 stc
435 rcrl $1,%esi
436 rcrl $1,%edi
437
438 #ifdef PARANOID
439 cmp $0xffffff60,%ebx
440 jb sqrt_more_prec_ok
441
442 cmp $0x000000a0,%ebx
443 ja sqrt_more_prec_ok
444
445 pushl EX_INTERNAL|0x215
446 call EXCEPTION
447
448 sqrt_more_prec_ok:
449 #endif
450
451 or %ebx,%ebx
452 js sqrt_more_prec_small
453
454 jnz sqrt_more_prec_large
455
456 or %ebx,%ecx
457 jnz sqrt_more_prec_large
458
459
460 movl $0x80000000,%eax
461 jmp sqrt_round_result
462
463 sqrt_more_prec_small:
464
465 movl $0x800000ff,%eax
466 jmp sqrt_round_result
467
468 sqrt_more_prec_large:
469
470 movl $0x7fffff00,%eax
471 jmp sqrt_round_result
472 ENDPROC(wm_sqrt)