~bkerensa/ubuntu/raring/valgrind/merge-from-deb

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
/* Test conversions between 64- and 80- bit quiet NaNs.  Uses
   "canonical forms" for qNaNs.  It also tests sNaNs but it's not
   clear what the canonical form of them should be, so the results are
   pretty much irrelevant.  Failure to do this right is the cause
   of https://bugzilla.mozilla.org/show_bug.cgi?id=738117
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef  unsigned char  UChar;


void do_64_to_80 ( UChar* dst, UChar* src )
{
   __asm__ __volatile__(
      "fldl (%0); fstpt (%1)"
      : : "r"(src), "r"(dst) : "memory"
   );
}

void do_80_to_64 ( UChar* dst, UChar* src )
{
   __asm__ __volatile__(
      "fldt (%0); fstpl (%1)"
      : : "r"(src), "r"(dst) : "memory"
   );
}

void print80 ( char* s, UChar* v )
{
   int i;
   printf("%s", s);
   for (i = 9; i >= 0; i--)
      printf("%02x", (unsigned int)v[i]);
   printf("\n");
}

void print64 ( char* s, UChar* v )
{
   int i;
   printf("%s", s);
   for (i = 7; i >= 0; i--) {
      printf("%02x", (unsigned int)v[i]);
   }
   printf("\n");
}

#if 0
void gen_qnan_64 ( UChar* dst )
{

}
#endif

#define SWAPC(_xx,_yy) { UChar tmp = _xx; _xx = _yy; _yy = tmp; }

static void rev64 ( UChar* f64 )
{
   SWAPC( f64[0], f64[7] );
   SWAPC( f64[1], f64[6] );
   SWAPC( f64[2], f64[5] );
   SWAPC( f64[3], f64[4] );
}

static void rev80 ( UChar* f80 )
{
   SWAPC( f80[0], f80[9] );
   SWAPC( f80[1], f80[8] );
   SWAPC( f80[2], f80[7] );
   SWAPC( f80[3], f80[6] );
   SWAPC( f80[4], f80[5] );
}

#undef SWAPC

int main ( void )
{
  UChar ref_qnan64[8]
        = { 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

  UChar ref_snan64[8] 
        = { 0x7f, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

  UChar ref_qnan80[10] 
        = { 0x7f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

  UChar ref_snan80[10]
        = { 0x7f, 0xff, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

  rev64( ref_qnan64 );
  rev64( ref_snan64 );
  rev80( ref_qnan80 );
  rev80( ref_snan80 );

  UChar* res = malloc(10);
#define ZAP memset(res, 0x55, 10)


  int pass;
  for (pass = 1; pass <= 2; pass++) {

  ZAP; do_64_to_80( res, ref_qnan64 );
  print64( "src = qnan64: ", ref_qnan64 );
  print80( "dst = qnan80: ", res );
  printf("\n");

  ZAP; do_64_to_80( res, ref_snan64 );
  print64( "src = snan64: ", ref_snan64 );
  print80( "dst = snan80: ", res );
  printf("\n");

  ZAP; do_80_to_64( res, ref_qnan80 );
  print80( "src = qnan80: ", ref_qnan80 );
  print64( "dst = qnan64: ", res );
  printf("\n");

  ZAP; do_80_to_64( res, ref_snan80 );
  print80( "src = snan80: ", ref_snan80 );
  print64( "dst = snan64: ", res );
  printf("\n");

  /* now make all the reference inputs negative and do it again */

  ref_qnan64[7] ^= 0x80;
  ref_snan64[7] ^= 0x80;

  ref_qnan80[9] ^= 0x80;
  ref_snan80[9] ^= 0x80;

  }

#undef ZAP

  free(res);
  return 0;
}