2 * linux/kernel/math/math_emulate.c
4 * (C) 1991 Linus Torvalds
6 * [expediant "port" of linux 8087 emulator to 386BSD, with apologies -wfj]
9 * $FreeBSD: src/sys/i386/i386/math_emulate.c,v 1.35 1999/08/28 00:43:47 peter Exp $
10 * $DragonFly: src/sys/platform/pc32/i386/math_emulate.c,v 1.9 2007/02/03 17:05:58 corecode Exp $
14 * Limited emulation 27.12.91 - mostly loads/stores, which gcc wants
15 * even for soft-float, unless you use bruce evans' patches. The patches
16 * are great, but they have to be re-applied for every version, and the
17 * library is different for soft-float and 80387. So emulation is more
18 * practical, even though it's slower.
20 * 28.12.91 - loads/stores work, even BCD. I'll have to start thinking
21 * about add/sub/mul/div. Urgel. I should find some good source, but I'll
22 * just fake up something.
24 * 30.12.91 - add/sub/mul/div/com seem to work mostly. I should really
25 * test every possible combination.
29 * This file is full of ugly macros etc: one problem was that gcc simply
30 * didn't want to make the structures as they should be: it has to try to
31 * align them. Sickening code, but at least I've hidden the ugly things
32 * in this one file: the other files don't need to know about these things.
34 * The other files also don't care about ST(x) etc - they just get addresses
35 * to 80-bit temporary reals, and do with them as they please. I wanted to
36 * hide most of the 387-specific things here.
39 #include <sys/param.h>
40 #include <sys/systm.h>
43 #include <machine/frame.h>
46 #include <sys/kernel.h>
51 #include <vm/vm_map.h>
54 #define __ALIGNED_TEMP_REAL 1
57 #define bswapw(x) __asm__("xchgb %%al,%%ah":"=a" (x):"0" ((short)x))
58 #define ST(x) (*__st((x)))
59 #define PST(x) ((const temp_real *) __st((x)))
60 #define math_abort(tfp, signo) tfp->tf_eip = oldeip; return (signo);
63 * We don't want these inlined - it gets too messy in the machine-code.
65 static void fpop(void);
66 static void fpush(void);
67 static void fxchg(temp_real_unaligned *a, temp_real_unaligned *b);
68 static temp_real_unaligned *__st(int i);
71 get_fs_byte(char *adr)
72 { return(fubyte(adr)); }
75 get_fs_word(unsigned short *adr)
76 { return(fuword(adr)); }
79 get_fs_long(u_int32_t *adr)
80 { return(fuword(adr)); }
83 put_fs_byte(unsigned char val, char *adr)
84 { (void)subyte(adr,val); }
87 put_fs_word(unsigned short val, short *adr)
88 { (void)susword(adr,val); }
91 put_fs_long(u_long val, u_int32_t *adr)
92 { (void)suword(adr,val); }
95 math_emulate(struct trapframe *info)
103 if ((curthread->td_pcb->pcb_flags & FP_SOFTFP) == 0) {
104 curthread->td_pcb->pcb_flags |= FP_SOFTFP;
110 if (I387.cwd & I387.swd & 0x3f)
114 oldeip = info->tf_eip;
115 /* 0x001f means user code space */
116 if ((u_short)info->tf_cs != 0x001F) {
117 kprintf("math_emulate: %04x:%08lx\n", (u_short)info->tf_cs,
119 panic("?Math emulation needed in kernel?");
121 /* completely ignore an operand-size prefix */
122 if (get_fs_byte((char *) info->tf_eip) == 0x66)
124 code = get_fs_word((unsigned short *) info->tf_eip);
128 *(unsigned short *) &I387.fcs = (u_short) info->tf_cs;
129 *(1+(unsigned short *) &I387.fcs) = code;
132 case 0x1d0: /* fnop */
134 case 0x1d1: case 0x1d2: case 0x1d3: /* fst to 32-bit mem */
135 case 0x1d4: case 0x1d5: case 0x1d6: case 0x1d7:
136 math_abort(info,SIGILL);
137 case 0x1e0: /* fchs */
138 ST(0).exponent ^= 0x8000;
140 case 0x1e1: /* fabs */
141 ST(0).exponent &= 0x7fff;
143 case 0x1e2: case 0x1e3:
144 math_abort(info,SIGILL);
145 case 0x1e4: /* ftst */
148 case 0x1e5: /* fxam */
149 kprintf("fxam not implemented\n");
150 math_abort(info,SIGILL);
151 case 0x1e6: case 0x1e7: /* fldenv */
152 math_abort(info,SIGILL);
153 case 0x1e8: /* fld1 */
157 case 0x1e9: /* fld2t */
161 case 0x1ea: /* fld2e */
165 case 0x1eb: /* fldpi */
169 case 0x1ec: /* fldlg2 */
173 case 0x1ed: /* fldln2 */
177 case 0x1ee: /* fldz */
182 math_abort(info,SIGILL);
183 case 0x1f0: /* f2xm1 */
184 case 0x1f1: /* fyl2x */
185 case 0x1f2: /* fptan */
186 case 0x1f3: /* fpatan */
187 case 0x1f4: /* fxtract */
188 case 0x1f5: /* fprem1 */
189 case 0x1f6: /* fdecstp */
190 case 0x1f7: /* fincstp */
191 case 0x1f8: /* fprem */
192 case 0x1f9: /* fyl2xp1 */
193 case 0x1fa: /* fsqrt */
194 case 0x1fb: /* fsincos */
195 case 0x1fe: /* fsin */
196 case 0x1ff: /* fcos */
198 "math_emulate: instruction %04x not implemented\n",
200 math_abort(info,SIGILL);
201 case 0x1fc: /* frndint */
202 frndint(PST(0),&tmp);
203 real_to_real(&tmp,&ST(0));
205 case 0x1fd: /* fscale */
206 /* incomplete and totally inadequate -wfj */
207 Fscale(PST(0), PST(1), &tmp);
208 real_to_real(&tmp,&ST(0));
209 return(0); /* 19 Sep 92*/
210 case 0x2e9: /* ????? */
211 /* if this should be a fucomp ST(0),ST(1) , it must be a 0x3e9 ATS */
212 fucom(PST(1),PST(0));
215 case 0x3d0: case 0x3d1: /* fist ?? */
217 case 0x3e2: /* fclex */
220 case 0x3e3: /* fninit */
227 case 0x6d9: /* fcompp */
231 case 0x7e0: /* fstsw ax */
232 *(short *) &info->tf_eax = I387.swd;
236 case 0x18: /* fadd */
237 fadd(PST(0),PST(code & 7),&tmp);
238 real_to_real(&tmp,&ST(0));
240 case 0x19: /* fmul */
241 fmul(PST(0),PST(code & 7),&tmp);
242 real_to_real(&tmp,&ST(0));
244 case 0x1a: /* fcom */
245 fcom(PST(code & 7),PST(0));
247 case 0x1b: /* fcomp */
248 fcom(PST(code & 7),PST(0));
251 case 0x1c: /* fsubr */
252 real_to_real(&ST(code & 7),&tmp);
253 tmp.exponent ^= 0x8000;
254 fadd(PST(0),&tmp,&tmp);
255 real_to_real(&tmp,&ST(0));
257 case 0x1d: /* fsub */
258 ST(0).exponent ^= 0x8000;
259 fadd(PST(0),PST(code & 7),&tmp);
260 real_to_real(&tmp,&ST(0));
262 case 0x1e: /* fdivr */
263 fdiv(PST(0),PST(code & 7),&tmp);
264 real_to_real(&tmp,&ST(0));
266 case 0x1f: /* fdiv */
267 fdiv(PST(code & 7),PST(0),&tmp);
268 real_to_real(&tmp,&ST(0));
272 ST(0) = ST((code & 7)+1); /* why plus 1 ????? ATS */
274 case 0x39: /* fxch */
275 fxchg(&ST(0),&ST(code & 7));
277 case 0x3b: /* ??? ??? wrong ???? ATS */
278 ST(code & 7) = ST(0);
281 case 0x98: /* fadd */
282 fadd(PST(0),PST(code & 7),&tmp);
283 real_to_real(&tmp,&ST(code & 7));
285 case 0x99: /* fmul */
286 fmul(PST(0),PST(code & 7),&tmp);
287 real_to_real(&tmp,&ST(code & 7));
289 case 0x9a: /* ???? , my manual don't list a direction bit
290 for fcom , ??? ATS */
291 fcom(PST(code & 7),PST(0));
293 case 0x9b: /* same as above , ATS */
294 fcom(PST(code & 7),PST(0));
297 case 0x9c: /* fsubr */
298 ST(code & 7).exponent ^= 0x8000;
299 fadd(PST(0),PST(code & 7),&tmp);
300 real_to_real(&tmp,&ST(code & 7));
302 case 0x9d: /* fsub */
303 real_to_real(&ST(0),&tmp);
304 tmp.exponent ^= 0x8000;
305 fadd(PST(code & 7),&tmp,&tmp);
306 real_to_real(&tmp,&ST(code & 7));
308 case 0x9e: /* fdivr */
309 fdiv(PST(0),PST(code & 7),&tmp);
310 real_to_real(&tmp,&ST(code & 7));
312 case 0x9f: /* fdiv */
313 fdiv(PST(code & 7),PST(0),&tmp);
314 real_to_real(&tmp,&ST(code & 7));
316 case 0xb8: /* ffree */
317 kprintf("ffree not implemented\n");
318 math_abort(info,SIGILL);
319 case 0xb9: /* fstp ???? where is the pop ? ATS */
320 fxchg(&ST(0),&ST(code & 7));
323 ST(code & 7) = ST(0);
325 case 0xbb: /* ????? encoding of fstp to mem ? ATS */
326 ST(code & 7) = ST(0);
329 case 0xbc: /* fucom */
330 fucom(PST(code & 7),PST(0));
332 case 0xbd: /* fucomp */
333 fucom(PST(code & 7),PST(0));
336 case 0xd8: /* faddp */
337 fadd(PST(code & 7),PST(0),&tmp);
338 real_to_real(&tmp,&ST(code & 7));
341 case 0xd9: /* fmulp */
342 fmul(PST(code & 7),PST(0),&tmp);
343 real_to_real(&tmp,&ST(code & 7));
346 case 0xda: /* ??? encoding of ficom with 16 bit mem ? ATS */
347 fcom(PST(code & 7),PST(0));
350 case 0xdc: /* fsubrp */
351 ST(code & 7).exponent ^= 0x8000;
352 fadd(PST(0),PST(code & 7),&tmp);
353 real_to_real(&tmp,&ST(code & 7));
356 case 0xdd: /* fsubp */
357 real_to_real(&ST(0),&tmp);
358 tmp.exponent ^= 0x8000;
359 fadd(PST(code & 7),&tmp,&tmp);
360 real_to_real(&tmp,&ST(code & 7));
363 case 0xde: /* fdivrp */
364 fdiv(PST(0),PST(code & 7),&tmp);
365 real_to_real(&tmp,&ST(code & 7));
368 case 0xdf: /* fdivp */
369 fdiv(PST(code & 7),PST(0),&tmp);
370 real_to_real(&tmp,&ST(code & 7));
373 case 0xf8: /* fild 16-bit mem ???? ATS */
374 kprintf("ffree not implemented\n");
375 math_abort(info,SIGILL);
378 case 0xf9: /* ????? ATS */
379 fxchg(&ST(0),&ST(code & 7));
381 case 0xfa: /* fist 16-bit mem ? ATS */
382 case 0xfb: /* fistp 16-bit mem ? ATS */
383 ST(code & 7) = ST(0);
387 switch ((code>>3) & 0xe7) {
389 put_short_real(PST(0),info,code);
392 put_short_real(PST(0),info,code);
396 address = ea(info,code);
397 for (code = 0 ; code < 7 ; code++) {
398 ((int32_t *) & I387)[code] =
399 get_fs_long((u_int32_t *) address);
404 address = ea(info,code);
405 *(unsigned short *) &I387.cwd =
406 get_fs_word((unsigned short *) address);
409 address = ea(info,code);
410 /*verify_area(address,28);*/
411 for (code = 0 ; code < 7 ; code++) {
412 put_fs_long( ((int32_t *) & I387)[code],
413 (u_int32_t *) address);
418 address = ea(info,code);
419 /*verify_area(address,2);*/
420 put_fs_word(I387.cwd,(short *) address);
423 put_long_int(PST(0),info,code);
426 put_long_int(PST(0),info,code);
431 get_temp_real(&tmp,info,code);
432 real_to_real(&tmp,&ST(0));
435 put_temp_real(PST(0),info,code);
439 put_long_real(PST(0),info,code);
442 put_long_real(PST(0),info,code);
446 address = ea(info,code);
447 for (code = 0 ; code < 27 ; code++) {
448 ((int32_t *) & I387)[code] =
449 get_fs_long((u_int32_t *) address);
454 address = ea(info,code);
455 /*verify_area(address,108);*/
456 for (code = 0 ; code < 27 ; code++) {
457 put_fs_long( ((int32_t *) & I387)[code],
458 (u_int32_t *) address);
466 address = ea(info,code);
467 /*verify_area(address,2);*/
468 put_fs_word(I387.swd,(short *) address);
471 put_short_int(PST(0),info,code);
474 put_short_int(PST(0),info,code);
479 get_BCD(&tmp,info,code);
480 real_to_real(&tmp,&ST(0));
484 get_longlong_int(&tmp,info,code);
485 real_to_real(&tmp,&ST(0));
488 put_BCD(PST(0),info,code);
492 put_longlong_int(PST(0),info,code);
498 get_short_real(&tmp,info,code);
501 get_long_int(&tmp,info,code);
504 get_long_real(&tmp,info,code);
507 get_short_int(&tmp,info,code);
509 switch ((code>>3) & 0x27) {
511 fadd(&tmp,PST(0),&tmp);
512 real_to_real(&tmp,&ST(0));
515 fmul(&tmp,PST(0),&tmp);
516 real_to_real(&tmp,&ST(0));
526 tmp.exponent ^= 0x8000;
527 fadd(&tmp,PST(0),&tmp);
528 real_to_real(&tmp,&ST(0));
531 ST(0).exponent ^= 0x8000;
532 fadd(&tmp,PST(0),&tmp);
533 real_to_real(&tmp,&ST(0));
536 fdiv(PST(0),&tmp,&tmp);
537 real_to_real(&tmp,&ST(0));
540 fdiv(&tmp,PST(0),&tmp);
541 real_to_real(&tmp,&ST(0));
544 if ((code & 0x138) == 0x100) {
546 real_to_real(&tmp,&ST(0));
549 kprintf("Unknown math-insns: %04x:%08x %04x\n",(u_short)info->tf_cs,
551 math_abort(info,SIGFPE);
559 tmp = I387.swd & 0xffffc7ffUL;
560 I387.swd += 0x00000800;
561 I387.swd &= 0x00003800;
570 tmp = I387.swd & 0xffffc7ffUL;
571 I387.swd += 0x00003800;
572 I387.swd &= 0x00003800;
577 fxchg(temp_real_unaligned *a, temp_real_unaligned *b)
579 temp_real_unaligned c;
586 static temp_real_unaligned *
591 return (temp_real_unaligned *) (i*10 + (char *)(I387.st_space));
595 * linux/kernel/math/ea.c
597 * (C) 1991 Linus Torvalds
601 * Calculate the effective address.
605 static int __regoffset[] = {
606 tEAX, tECX, tEDX, tEBX, tESP, tEBP, tESI, tEDI
609 #define REG(x) (((int *)curthread->td_lwp->lwp_md.md_regs)[__regoffset[(x)]])
612 sib(struct trapframe *info, int mod)
614 unsigned char ss,index,base;
617 base = get_fs_byte((char *) info->tf_eip);
620 index = (base >> 3) & 7;
627 if (mod || base != 5)
630 offset += (signed char) get_fs_byte((char *) info->tf_eip);
632 } else if (mod == 2 || base == 5) {
633 offset += (signed) get_fs_long((u_int32_t *) info->tf_eip);
638 return (char *) offset;
642 ea(struct trapframe *info, unsigned short code)
644 unsigned char mod,rm;
648 mod = (code >> 6) & 3;
650 if (rm == 4 && mod != 3)
651 return sib(info,mod);
652 if (rm == 5 && !mod) {
653 offset = get_fs_long((u_int32_t *) info->tf_eip);
657 return (char *) offset;
659 tmp = (int32_t *) ®(rm);
661 case 0: offset = 0; break;
663 offset = (signed char) get_fs_byte((char *) info->tf_eip);
667 offset = (signed) get_fs_long((u_int32_t *) info->tf_eip);
672 math_abort(info,1<<(SIGILL-1));
677 return offset + (char *) *tmp;
680 * linux/kernel/math/get_put.c
682 * (C) 1991 Linus Torvalds
686 * This file handles all accesses to user memory: getting and putting
687 * ints/reals/BCD etc. This is the only part that concerns itself with
688 * other than temporary real format. All other cals are strictly temp_real.
692 get_short_real(temp_real *tmp, struct trapframe *info, unsigned short code)
697 addr = ea(info,code);
698 sr = get_fs_long((u_int32_t *) addr);
699 short_to_temp(&sr,tmp);
703 get_long_real(temp_real *tmp, struct trapframe *info, unsigned short code)
708 addr = ea(info,code);
709 lr.a = get_fs_long((u_int32_t *) addr);
710 lr.b = get_fs_long(1 + (u_int32_t *) addr);
711 long_to_temp(&lr,tmp);
715 get_temp_real(temp_real *tmp, struct trapframe *info, unsigned short code)
719 addr = ea(info,code);
720 tmp->a = get_fs_long((u_int32_t *) addr);
721 tmp->b = get_fs_long(1 + (u_int32_t *) addr);
722 tmp->exponent = get_fs_word(4 + (unsigned short *) addr);
726 get_short_int(temp_real *tmp, struct trapframe *info, unsigned short code)
731 addr = ea(info,code);
732 ti.a = (signed short) get_fs_word((unsigned short *) addr);
734 if ((ti.sign = (ti.a < 0)) != 0)
736 int_to_real(&ti,tmp);
740 get_long_int(temp_real *tmp, struct trapframe *info, unsigned short code)
745 addr = ea(info,code);
746 ti.a = get_fs_long((u_int32_t *) addr);
748 if ((ti.sign = (ti.a < 0)) != 0)
750 int_to_real(&ti,tmp);
754 get_longlong_int(temp_real *tmp, struct trapframe *info, unsigned short code)
759 addr = ea(info,code);
760 ti.a = get_fs_long((u_int32_t *) addr);
761 ti.b = get_fs_long(1 + (u_int32_t *) addr);
762 if ((ti.sign = (ti.b < 0)) != 0)
763 __asm__("notl %0 ; notl %1\n\t"
764 "addl $1,%0 ; adcl $0,%1"
765 :"=r" (ti.a),"=r" (ti.b)
766 :"0" (ti.a),"1" (ti.b));
767 int_to_real(&ti,tmp);
770 #define MUL10(low,high) \
771 __asm__("addl %0,%0 ; adcl %1,%1\n\t" \
772 "movl %0,%%ecx ; movl %1,%%ebx\n\t" \
773 "addl %0,%0 ; adcl %1,%1\n\t" \
774 "addl %0,%0 ; adcl %1,%1\n\t" \
775 "addl %%ecx,%0 ; adcl %%ebx,%1" \
776 :"=a" (low),"=d" (high) \
777 :"0" (low),"1" (high):"cx","bx")
779 #define ADD64(val,low,high) \
780 __asm__("addl %4,%0 ; adcl $0,%1":"=r" (low),"=r" (high) \
781 :"0" (low),"1" (high),"r" ((u_int32_t) (val)))
784 get_BCD(temp_real *tmp, struct trapframe *info, unsigned short code)
791 addr = ea(info,code);
793 i.sign = 0x80 & get_fs_byte(addr--);
795 for (k = 0; k < 9; k++) {
796 c = get_fs_byte(addr--);
798 ADD64((c>>4), i.a, i.b);
800 ADD64((c&0xf), i.a, i.b);
806 put_short_real(const temp_real *tmp,
807 struct trapframe *info, unsigned short code)
812 addr = ea(info,code);
813 /*verify_area(addr,4);*/
814 temp_to_short(tmp,&sr);
815 put_fs_long(sr,(u_int32_t *) addr);
819 put_long_real(const temp_real *tmp,
820 struct trapframe *info, unsigned short code)
825 addr = ea(info,code);
826 /*verify_area(addr,8);*/
827 temp_to_long(tmp,&lr);
828 put_fs_long(lr.a, (u_int32_t *) addr);
829 put_fs_long(lr.b, 1 + (u_int32_t *) addr);
833 put_temp_real(const temp_real *tmp,
834 struct trapframe *info, unsigned short code)
838 addr = ea(info,code);
839 /*verify_area(addr,10);*/
840 put_fs_long(tmp->a, (u_int32_t *) addr);
841 put_fs_long(tmp->b, 1 + (u_int32_t *) addr);
842 put_fs_word(tmp->exponent, 4 + (short *) addr);
846 put_short_int(const temp_real *tmp,
847 struct trapframe *info, unsigned short code)
852 addr = ea(info,code);
853 real_to_int(tmp,&ti);
854 /*verify_area(addr,2);*/
857 put_fs_word(ti.a,(short *) addr);
861 put_long_int(const temp_real *tmp,
862 struct trapframe *info, unsigned short code)
867 addr = ea(info,code);
868 real_to_int(tmp,&ti);
869 /*verify_area(addr,4);*/
872 put_fs_long(ti.a,(u_int32_t *) addr);
876 put_longlong_int(const temp_real *tmp,
877 struct trapframe *info, unsigned short code)
882 addr = ea(info,code);
883 real_to_int(tmp,&ti);
884 /*verify_area(addr,8);*/
886 __asm__("notl %0 ; notl %1\n\t"
887 "addl $1,%0 ; adcl $0,%1"
888 :"=r" (ti.a),"=r" (ti.b)
889 :"0" (ti.a),"1" (ti.b));
890 put_fs_long(ti.a,(u_int32_t *) addr);
891 put_fs_long(ti.b,1 + (u_int32_t *) addr);
894 #define DIV10(low,high,rem) \
895 __asm__("divl %6 ; xchgl %1,%2 ; divl %6" \
896 :"=d" (rem),"=a" (low),"=r" (high) \
897 :"0" (0),"1" (high),"2" (low),"c" (10))
900 put_BCD(const temp_real *tmp,struct trapframe *info, unsigned short code)
907 addr = ea(info,code);
908 /*verify_area(addr,10);*/
911 put_fs_byte(0x80, addr+9);
913 put_fs_byte(0, addr+9);
914 for (k = 0; k < 9; k++) {
919 put_fs_byte(c,addr++);
924 * linux/kernel/math/mul.c
926 * (C) 1991 Linus Torvalds
930 * temporary real multiplication routine.
937 __asm__("movl (%0),%%eax ; addl %%eax,(%0)\n\t"
938 "movl 4(%0),%%eax ; adcl %%eax,4(%0)\n\t"
939 "movl 8(%0),%%eax ; adcl %%eax,8(%0)\n\t"
940 "movl 12(%0),%%eax ; adcl %%eax,12(%0)"
945 mul64(const temp_real *a, const temp_real *b, int *c)
947 __asm__("movl (%0),%%eax\n\t"
949 "movl %%eax,(%2)\n\t"
950 "movl %%edx,4(%2)\n\t"
951 "movl 4(%0),%%eax\n\t"
953 "movl %%eax,8(%2)\n\t"
954 "movl %%edx,12(%2)\n\t"
955 "movl (%0),%%eax\n\t"
957 "addl %%eax,4(%2)\n\t"
958 "adcl %%edx,8(%2)\n\t"
960 "movl 4(%0),%%eax\n\t"
962 "addl %%eax,4(%2)\n\t"
963 "adcl %%edx,8(%2)\n\t"
965 ::"S" (a),"c" (b),"D" (c)
970 fmul(const temp_real *src1, const temp_real *src2, temp_real *result)
973 int tmp[4] = {0,0,0,0};
975 sign = (src1->exponent ^ src2->exponent) & 0x8000;
976 i = (src1->exponent & 0x7fff) + (src2->exponent & 0x7fff) - 16383 + 1;
978 result->exponent = sign;
979 result->a = result->b = 0;
986 mul64(src1,src2,tmp);
987 if (tmp[0] || tmp[1] || tmp[2] || tmp[3])
988 while (i && tmp[3] >= 0) {
994 result->exponent = i | sign;
1000 * linux/kernel/math/div.c
1002 * (C) 1991 Linus Torvalds
1006 * temporary real division routine.
1012 __asm__ __volatile__("movl (%0),%%eax ; addl %%eax,(%0)\n\t"
1013 "movl 4(%0),%%eax ; adcl %%eax,4(%0)\n\t"
1014 "movl 8(%0),%%eax ; adcl %%eax,8(%0)\n\t"
1015 "movl 12(%0),%%eax ; adcl %%eax,12(%0)"
1022 __asm__("shrl $1,12(%0) ; rcrl $1,8(%0) ; rcrl $1,4(%0) ; rcrl $1,(%0)"
1027 try_sub(int *a, int *b)
1031 __asm__ __volatile__("movl (%1),%%eax ; subl %%eax,(%2)\n\t"
1032 "movl 4(%1),%%eax ; sbbl %%eax,4(%2)\n\t"
1033 "movl 8(%1),%%eax ; sbbl %%eax,8(%2)\n\t"
1034 "movl 12(%1),%%eax ; sbbl %%eax,12(%2)\n\t"
1035 "setae %%al":"=a" (ok):"c" (a),"d" (b));
1040 div64(int *a, int *b, int *c)
1044 unsigned int mask = 0;
1047 for (i = 0 ; i<64 ; i++) {
1048 if (!(mask >>= 1)) {
1050 mask = 0x80000000UL;
1052 tmp[0] = a[0]; tmp[1] = a[1];
1053 tmp[2] = a[2]; tmp[3] = a[3];
1054 if (try_sub(b,tmp)) {
1056 a[0] = tmp[0]; a[1] = tmp[1];
1057 a[2] = tmp[2]; a[3] = tmp[3];
1064 fdiv(const temp_real *src1, const temp_real *src2, temp_real *result)
1067 int a[4],b[4],tmp[4] = {0,0,0,0};
1069 sign = (src1->exponent ^ src2->exponent) & 0x8000;
1070 if (!(src2->a || src2->b)) {
1074 i = (src1->exponent & 0x7fff) - (src2->exponent & 0x7fff) + 16383;
1077 result->exponent = sign;
1078 result->a = result->b = 0;
1092 if (tmp[0] || tmp[1] || tmp[2] || tmp[3]) {
1093 while (i && tmp[3] >= 0) {
1105 if (tmp[0] || tmp[1])
1107 result->exponent = i | sign;
1113 * linux/kernel/math/add.c
1115 * (C) 1991 Linus Torvalds
1119 * temporary real addition routine.
1121 * NOTE! These aren't exact: they are only 62 bits wide, and don't do
1122 * correct rounding. Fast hack. The reason is that we shift right the
1123 * values by two, in order not to have overflow (1 bit), and to be able
1124 * to move the sign into the mantissa (1 bit). Much simpler algorithms,
1125 * and 62 bits (61 really - no rounding) accuracy is usually enough. The
1126 * only time you should notice anything weird is when adding 64-bit
1127 * integers together. When using doubles (52 bits accuracy), the
1128 * 61-bit accuracy never shows at all.
1132 __asm__("notl %0 ; notl %1 ; addl $1,%0 ; adcl $0,%1" \
1133 :"=r" (a->a),"=r" (a->b) \
1134 :"0" (a->a),"1" (a->b))
1137 signify(temp_real *a)
1140 __asm__("shrdl $2,%1,%0 ; shrl $2,%1"
1141 :"=r" (a->a),"=r" (a->b)
1142 :"0" (a->a),"1" (a->b));
1143 if (a->exponent < 0)
1145 a->exponent &= 0x7fff;
1149 unsignify(temp_real *a)
1151 if (!(a->a || a->b)) {
1155 a->exponent &= 0x7fff;
1158 a->exponent |= 0x8000;
1162 __asm__("addl %0,%0 ; adcl %1,%1"
1163 :"=r" (a->a),"=r" (a->b)
1164 :"0" (a->a),"1" (a->b));
1169 fadd(const temp_real *src1, const temp_real *src2, temp_real *result)
1174 x1 = src1->exponent & 0x7fff;
1175 x2 = src2->exponent & 0x7fff;
1194 __asm__("shrdl %4,%1,%0 ; shrl %4,%1"
1195 :"=r" (b.a),"=r" (b.b)
1196 :"0" (b.a),"1" (b.b),"c" ((char) shift));
1199 __asm__("addl %4,%0 ; adcl %5,%1"
1200 :"=r" (a.a),"=r" (a.b)
1201 :"0" (a.a),"1" (a.b),"g" (b.a),"g" (b.b));
1207 * linux/kernel/math/compare.c
1209 * (C) 1991 Linus Torvalds
1213 * temporary real comparison routines
1217 #define clear_Cx() (I387.swd &= ~0x4500)
1220 normalize(temp_real *a)
1222 int i = a->exponent & 0x7fff;
1223 int sign = a->exponent & 0x8000;
1225 if (!(a->a || a->b)) {
1229 while (i && a->b >= 0) {
1231 __asm__("addl %0,%0 ; adcl %1,%1"
1232 :"=r" (a->a),"=r" (a->b)
1233 :"0" (a->a),"1" (a->b));
1235 a->exponent = i | sign;
1239 ftst(const temp_real *a)
1246 if (b.a || b.b || b.exponent) {
1254 fcom(const temp_real *src1, const temp_real *src2)
1259 a.exponent ^= 0x8000;
1265 fucom(const temp_real *src1, const temp_real *src2)
1271 * linux/kernel/math/convert.c
1273 * (C) 1991 Linus Torvalds
1278 * NOTE!!! There is some "non-obvious" optimisations in the temp_to_long
1279 * and temp_to_short conversion routines: don't touch them if you don't
1280 * know what's going on. They are the adding of one in the rounding: the
1281 * overflow bit is also used for adding one into the exponent. Thus it
1282 * looks like the overflow would be incorrectly handled, but due to the
1283 * way the IEEE numbers work, things are correct.
1285 * There is no checking for total overflow in the conversions, though (ie
1286 * if the temp-real number simply won't fit in a short- or long-real.)
1290 short_to_temp(const short_real *a, temp_real *b)
1292 if (!(*a & 0x7fffffff)) {
1295 b->exponent = 0x8000;
1300 b->exponent = ((*a>>23) & 0xff)-127+16383;
1302 b->exponent |= 0x8000;
1303 b->b = (*a<<8) | 0x80000000UL;
1308 long_to_temp(const long_real *a, temp_real *b)
1310 if (!a->a && !(a->b & 0x7fffffff)) {
1313 b->exponent = 0x8000;
1318 b->exponent = ((a->b >> 20) & 0x7ff)-1023+16383;
1320 b->exponent |= 0x8000;
1321 b->b = 0x80000000UL | (a->b<<11) | (((u_int32_t)a->a)>>21);
1326 temp_to_short(const temp_real *a, short_real *b)
1328 if (!(a->exponent & 0x7fff)) {
1329 *b = (a->exponent)?0x80000000UL:0;
1332 *b = ((((int32_t) a->exponent)-16383+127) << 23) & 0x7f800000;
1333 if (a->exponent < 0)
1335 *b |= (a->b >> 8) & 0x007fffff;
1336 switch ((int)ROUNDING) {
1338 if ((a->b & 0xff) > 0x80)
1342 if ((a->exponent & 0x8000) && (a->b & 0xff))
1346 if (!(a->exponent & 0x8000) && (a->b & 0xff))
1353 temp_to_long(const temp_real *a, long_real *b)
1355 if (!(a->exponent & 0x7fff)) {
1357 b->b = (a->exponent)?0x80000000UL:0;
1360 b->b = (((0x7fff & (int32_t) a->exponent)-16383+1023) << 20) &
1362 if (a->exponent < 0)
1363 b->b |= 0x80000000UL;
1364 b->b |= (a->b >> 11) & 0x000fffff;
1366 b->a |= (a->a >> 11) & 0x001fffff;
1367 switch ((int)ROUNDING) {
1369 if ((a->a & 0x7ff) > 0x400)
1370 __asm__("addl $1,%0 ; adcl $0,%1"
1371 :"=r" (b->a),"=r" (b->b)
1372 :"0" (b->a),"1" (b->b));
1375 if ((a->exponent & 0x8000) && (a->b & 0xff))
1376 __asm__("addl $1,%0 ; adcl $0,%1"
1377 :"=r" (b->a),"=r" (b->b)
1378 :"0" (b->a),"1" (b->b));
1381 if (!(a->exponent & 0x8000) && (a->b & 0xff))
1382 __asm__("addl $1,%0 ; adcl $0,%1"
1383 :"=r" (b->a),"=r" (b->b)
1384 :"0" (b->a),"1" (b->b));
1390 frndint(const temp_real *a, temp_real *b)
1392 int shift = 16383 + 63 - (a->exponent & 0x7fff);
1393 u_int32_t underflow;
1395 if ((shift < 0) || (shift == 16383+63)) {
1399 b->a = b->b = underflow = 0;
1400 b->exponent = a->exponent;
1402 b->b = a->b; b->a = a->a;
1403 } else if (shift < 64) {
1404 b->a = a->b; underflow = a->a;
1407 } else if (shift < 96) {
1415 b->exponent += shift;
1416 __asm__("shrdl %2,%1,%0"
1417 :"=r" (underflow),"=r" (b->a)
1418 :"c" ((char) shift),"0" (underflow),"1" (b->a));
1419 __asm__("shrdl %2,%1,%0"
1420 :"=r" (b->a),"=r" (b->b)
1421 :"c" ((char) shift),"0" (b->a),"1" (b->b));
1422 __asm__("shrl %1,%0"
1424 :"c" ((char) shift),"0" (b->b));
1425 switch ((int)ROUNDING) {
1427 __asm__("addl %4,%5 ; adcl $0,%0 ; adcl $0,%1"
1428 :"=r" (b->a),"=r" (b->b)
1429 :"0" (b->a),"1" (b->b)
1430 ,"r" (0x7fffffff + (b->a & 1))
1431 ,"m" (*&underflow));
1434 if ((b->exponent >= 0) && underflow)
1435 __asm__("addl $1,%0 ; adcl $0,%1"
1436 :"=r" (b->a),"=r" (b->b)
1437 :"0" (b->a),"1" (b->b));
1440 if ((b->exponent < 0) && underflow)
1441 __asm__("addl $1,%0 ; adcl $0,%1"
1442 :"=r" (b->a),"=r" (b->b)
1443 :"0" (b->a),"1" (b->b));
1449 __asm__("addl %0,%0 ; adcl %1,%1"
1450 :"=r" (b->a),"=r" (b->b)
1451 :"0" (b->a),"1" (b->b));
1458 Fscale(const temp_real *a, const temp_real *b, temp_real *c)
1463 if(!c->a && !c->b) { /* 19 Sep 92*/
1467 real_to_int(b, &ti);
1469 c->exponent -= ti.a;
1471 c->exponent += ti.a;
1475 real_to_int(const temp_real *a, temp_int *b)
1477 int shift = 16383 + 63 - (a->exponent & 0x7fff);
1478 u_int32_t underflow;
1480 b->a = b->b = underflow = 0;
1481 b->sign = (a->exponent < 0);
1487 b->b = a->b; b->a = a->a;
1488 } else if (shift < 64) {
1489 b->a = a->b; underflow = a->a;
1491 } else if (shift < 96) {
1498 __asm__("shrdl %2,%1,%0"
1499 :"=r" (underflow),"=r" (b->a)
1500 :"c" ((char) shift),"0" (underflow),"1" (b->a));
1501 __asm__("shrdl %2,%1,%0"
1502 :"=r" (b->a),"=r" (b->b)
1503 :"c" ((char) shift),"0" (b->a),"1" (b->b));
1504 __asm__("shrl %1,%0"
1506 :"c" ((char) shift),"0" (b->b));
1507 switch ((int)ROUNDING) {
1509 __asm__("addl %4,%5 ; adcl $0,%0 ; adcl $0,%1"
1510 :"=r" (b->a),"=r" (b->b)
1511 :"0" (b->a),"1" (b->b)
1512 ,"r" (0x7fffffff + (b->a & 1))
1513 ,"m" (*&underflow));
1516 if (!b->sign && underflow)
1517 __asm__("addl $1,%0 ; adcl $0,%1"
1518 :"=r" (b->a),"=r" (b->b)
1519 :"0" (b->a),"1" (b->b));
1522 if (b->sign && underflow)
1523 __asm__("addl $1,%0 ; adcl $0,%1"
1524 :"=r" (b->a),"=r" (b->b)
1525 :"0" (b->a),"1" (b->b));
1531 int_to_real(const temp_int *a, temp_real *b)
1536 b->exponent = 16383 + 63 + (a->sign? 0x8000:0);
1543 __asm__("addl %0,%0 ; adcl %1,%1"
1544 :"=r" (b->a),"=r" (b->b)
1545 :"0" (b->a),"1" (b->b));
1550 fpu_modevent(module_t mod, int type, void *unused)
1554 if (pmath_emulate) {
1555 kprintf("Another Math emulator already present\n");
1558 pmath_emulate = math_emulate;
1560 kprintf("Math emulator present\n");
1563 if (pmath_emulate != math_emulate) {
1564 kprintf("Cannot unload another math emulator\n");
1569 kprintf("Math emulator unloaded\n");
1576 static moduledata_t fpumod = {
1581 DECLARE_MODULE(fpu, fpumod, SI_SUB_DRIVERS, SI_ORDER_ANY);