summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/graphite/id-15.c
blob: b57c209698e4e36fa02974284f0c26ab45343424 (plain)
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
/* { dg-require-effective-target int32plus } */

typedef long unsigned int size_t;
extern void *memset (void *__s, int __c, size_t __n) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1)));

static void
encode (words, low, hi)
     long *words;
     unsigned long low;
     long hi;
{
  words[0] = ((low) & (((unsigned long) 1 << (sizeof(unsigned long) / 2)) - 1));
  words[1] = ((unsigned long) (low) >> sizeof(unsigned long) / 2);
  words[2] = ((hi) & (((unsigned long) 1 << (sizeof(unsigned long) / 2)) - 1));
  words[3] = ((unsigned long) (hi) >> sizeof(unsigned long) / 2);
}

static void
decode (words, low, hi)
     long *words;
     unsigned long *low;
     long *hi;
{
  *low = words[0] + words[1] * ((unsigned long) 1 << sizeof(unsigned long) / 2);
  *hi = words[2] + words[3] * ((unsigned long) 1 << sizeof(unsigned long) / 2);
}

int
neg_double (l1, h1, lv, hv)
     unsigned long l1;
     long h1;
     unsigned long *lv;
     long *hv;
{
  if (l1 == 0)
    {
      *lv = 0;
      *hv = - h1;
      return (*hv & h1) < 0;
    }
  else
    {
      *lv = -l1;
      *hv = ~h1;
      return 0;
    }
}

int
add_double (l1, h1, l2, h2, lv, hv)
     unsigned long l1, l2;
     long h1, h2;
     unsigned long *lv;
     long *hv;
{
  unsigned long l;
  long h;

  l = l1 + l2;
  h = h1 + h2 + (l < l1);

  *lv = l;
  *hv = h;
  return ((~((h1) ^ (h2)) & ((h1) ^ (h))) < 0);
}

int
mul_double (l1, h1, l2, h2, lv, hv)
     unsigned long l1, l2;
     long h1, h2;
     unsigned long *lv;
     long *hv;
{
  long arg1[4];
  long arg2[4];
  long prod[4 * 2];
  unsigned long carry;
  int i, j, k;
  unsigned long toplow, neglow;
  long tophigh, neghigh;

  encode (arg1, l1, h1);
  encode (arg2, l2, h2);

  memset ((char *) prod, 0, sizeof prod);

  for (i = 0; i < 4; i++)
    {
      carry = 0;
      for (j = 0; j < 4; j++)
	{
	  k = i + j;

	  carry += arg1[i] * arg2[j];

	  carry += prod[k];
	  prod[k] = ((carry) & (((unsigned long) 1 << (sizeof(unsigned long) / 2)) - 1));
	  carry = ((unsigned long) (carry) >> sizeof(unsigned long) / 2);
	}
      prod[i + 4] = carry;
    }

  decode (prod, lv, hv);



  decode (prod + 4, &toplow, &tophigh);
  if (h1 < 0)
    {
      neg_double (l2, h2, &neglow, &neghigh);
      add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
    }
  if (h2 < 0)
    {
      neg_double (l1, h1, &neglow, &neghigh);
      add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
    }
  return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
}