1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 
do_fsave_interesting_stuff(void * p)5 void do_fsave_interesting_stuff ( void* p )
6 {
7    asm __volatile__("fninit");
8    asm __volatile__("fldpi");
9    asm __volatile__("fld1");
10    asm __volatile__("fldln2");
11    asm __volatile__("fsave (%0)" : : "r" (p) : "memory" );
12 }
13 
do_fsave(void * p)14 void do_fsave ( void* p )
15 {
16    asm __volatile__("fsave (%0)" : : "r" (p) : "memory" );
17 }
18 
do_frstor(void * p)19 void do_frstor ( void* p )
20 {
21    asm __volatile__("frstor (%0)" : : "r" (p) : "memory" );
22 }
23 
24 
isFPLsbs(int i)25 int isFPLsbs ( int i )
26 {
27    int q;
28    q = 0; if (i == q || i == q+1) return 1;
29    q = 10; if (i == q || i == q+1) return 1;
30    q = 20; if (i == q || i == q+1) return 1;
31    q = 30; if (i == q || i == q+1) return 1;
32    q = 40; if (i == q || i == q+1) return 1;
33    q = 50; if (i == q || i == q+1) return 1;
34    q = 60; if (i == q || i == q+1) return 1;
35    q = 70; if (i == q || i == q+1) return 1;
36    return 0;
37 }
38 
show_fpustate(unsigned char * buf,int hide64to80)39 void show_fpustate ( unsigned char* buf, int hide64to80 )
40 {
41    int i;
42    printf("  0   ");
43    for (i = 0; i < 14; i++)
44       printf("%02x ", buf[i]);
45    printf("\n");
46 
47    printf(" 14   ");
48    for (i = 14; i < 28; i++)
49       printf("%02x ", buf[i]);
50    printf("\n");
51 
52    for (i = 0; i < 80; i++) {
53       if ((i % 10) == 0)
54          printf("%3d   ", i+28);
55       if (hide64to80 && isFPLsbs(i))
56 	 printf("xx ");
57       else
58          printf("%02x ", buf[i+28]);
59       if (i > 0 && ((i % 10) == 9))
60           printf("\n");
61    }
62 }
63 
main(int argc,char ** argv)64 int main ( int argc, char** argv )
65 {
66    unsigned short* buf1 = malloc(54*sizeof(short));
67    unsigned short* buf2 = malloc(54*sizeof(short));
68    int xx = argc > 1;
69    printf("Re-run with any arg to suppress least-significant\n"
70           "   16 bits of FP numbers\n");
71 
72    /* Create an initial image. */
73    do_fsave_interesting_stuff(buf1);
74    show_fpustate( (unsigned char*)buf1, xx );
75 
76    /* Reload it into buf2. */
77    do_frstor(buf1);
78    do_fsave(buf2);
79    show_fpustate( (unsigned char*)buf2, xx );
80 
81    return 0;
82 }
83