#include <u.h>
#include <libc.h>

int
pm__tas(int *p)
{
	int     v;

	__asm__(	"movl   $1, %%eax\n\t"
			"xchgl  %%eax,(%%ebx)"
			: "=a" (v)
			: "ebx" (p)
	);
	return v;
}

void
FPsave(void *f)
{
	__asm__(	"fstenv	(%%eax)\n\t"
			: /* no output */
			: "eax"	(f)
	);
}

void
FPrestore(void *f)	
{
	__asm__(	"fldenv (%%eax)\n\t"
			: /* no output */
			: "eax"	(f)
	);
}

void
setfcr(ulong fcr)
{
	__asm__(	"xorb	$0x3f, %%al\n\t"
			"pushw	%%ax\n\t"
			"fwait\n\t"
			"fldcw	(%%esp)\n\t"
			"popw	%%ax\n\t"
			: /* no output */
			: "eax" (fcr)
	);
}

ulong
getfcr(void)
{
	ulong fcr = 0;

	__asm__(	"pushl	%%eax\n\t"
			"fwait\n\t"
			"fstcw	(%%esp)\n\t"
			"popl	%%eax\n\t"
			"xorb	$0x3f, %%al\n\t"
			: "=a"  (fcr)
			: "eax"	(fcr)
	);
	return fcr; 
}

ulong
getfsr(void)
{
	ulong fsr = -1;

	__asm__(	"fwait\n\t"
			"fstsw	(%%eax)\n\t"
			"movl	(%%eax), %%eax\n\t"
			"andl	$0xffff, %%eax\n\t"
			: "=a"  (fsr)
			: "eax" (&fsr)
	);
	return fsr;
}

void
setfsr(ulong fsr)
{
	__asm__("fclex\n\t");
}
