summaryrefslogtreecommitdiff
path: root/gcc/config/riscv/genrvv-type-indexer.cc
blob: 2f0375568a887916c888fba69ccb558b90ef0bc0 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* Generate the RVV type indexer tables.
   Copyright (C) 2023-2023 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "bconfig.h"
#define INCLUDE_SSTREAM
#include "system.h"
#include "errors.h"

#include "coretypes.h"

#include <assert.h>
#include <math.h>

#define BOOL_SIZE_LIST {1}

std::string
to_lmul (int lmul_log2)
{
  std::stringstream lmul_str;
  if (lmul_log2 >= 0)
    lmul_str << "m";
  else
    {
      lmul_str << "mf";
      lmul_log2 = -lmul_log2;
    }

  lmul_str << (1 << lmul_log2);
  return lmul_str.str ();
}

bool
valid_type (unsigned sew, int lmul_log2, bool float_p)
{
  if (lmul_log2 > 3)
    return false;

  switch (sew)
    {
    case 8:
      return lmul_log2 >= -3 && !float_p;
    case 16:
      return lmul_log2 >= -2 && !float_p;
    case 32:
      return lmul_log2 >= -1;
    case 64:
      return lmul_log2 >= 0;
    default:
      return false;
    }
}

bool
valid_type (unsigned sew, int lmul_log2, unsigned nf, bool float_p)
{
  if (!valid_type (sew, lmul_log2, float_p))
    return false;

  if (nf > 8 || nf < 1)
    return false;

  switch (lmul_log2)
    {
    case 1:
      return nf < 5;
    case 2:
      return nf < 3;
    case 3:
      return nf == 1;
    default:
      return true;
    }
}

std::string
inttype (unsigned sew, int lmul_log2, bool unsigned_p)
{
  if (!valid_type (sew, lmul_log2, /*float_t*/ false))
    return "INVALID";

  std::stringstream mode;
  mode << "v";
  if (unsigned_p)
    mode << "u";
  mode << "int" << sew << to_lmul (lmul_log2) << "_t";
  return mode.str ();
}

std::string
inttype (unsigned sew, int lmul_log2, unsigned nf, bool unsigned_p)
{
  if (!valid_type (sew, lmul_log2, nf, /*float_t*/ false))
    return "INVALID";

  std::stringstream mode;
  mode << "v";
  if (unsigned_p)
    mode << "u";
  mode << "int" << sew << to_lmul (lmul_log2);
  if (nf > 1)
    mode << "x" << nf;
  mode << "_t";
  return mode.str ();
}

std::string
floattype (unsigned sew, int lmul_log2)
{
  if (!valid_type (sew, lmul_log2, /*float_t*/ true))
    return "INVALID";

  std::stringstream mode;
  mode << "vfloat" << sew << to_lmul (lmul_log2) << "_t";
  return mode.str ();
}

std::string
floattype (unsigned sew, int lmul_log2, unsigned nf)
{
  if (!valid_type (sew, lmul_log2, nf, /*float_t*/ true))
    return "INVALID";

  std::stringstream mode;
  mode << "vfloat" << sew << to_lmul (lmul_log2);
  if (nf > 1)
    mode << "x" << nf;
  mode << "_t";
  return mode.str ();
}

std::string
maskmode (unsigned sew, int lmul_log2)
{
  if (!valid_type (sew, lmul_log2, /*float_t*/ false))
    return "INVALID";

  std::stringstream mode;

  int mlen;
  if (lmul_log2 >= 0)
    mlen = sew / (1 << lmul_log2);
  else
    mlen = sew * (1 << -lmul_log2);

  mode << "vbool" << mlen << "_t";
  return mode.str ();
}

std::string
same_ratio_eew_type (unsigned sew, int lmul_log2, unsigned eew, bool unsigned_p,
		     bool float_p)
{
  if (!valid_type (sew, lmul_log2, float_p))
    return "INVALID";

  int elmul_log2;

  if (sew == eew)
    elmul_log2 = lmul_log2;
  else if (sew > eew)
    elmul_log2 = lmul_log2 - log2 (sew / eew);
  else /* sew < eew */
    elmul_log2 = lmul_log2 + log2 (eew / sew);

  if (float_p)
    return floattype (eew, elmul_log2);
  else
    return inttype (eew, elmul_log2, unsigned_p);
}

int
main (int argc, const char **argv)
{
  // Require at least one argument.
  if (argc < 2)
    return 1;

  FILE *fp = fopen (argv[1], "w");

  if (!fp)
    return 1;

  fprintf (fp, "/* Generated by genrvv-type-indexer */\n");

  for (unsigned vbool : {64, 32, 16, 8, 4, 2, 1})
    {
      std::stringstream mode;
      mode << "vbool" << vbool << "_t";
      fprintf (fp, "DEF_RVV_TYPE_INDEX (\n");
      fprintf (fp, "  /*VECTOR*/ %s,\n", mode.str ().c_str ());
      fprintf (fp, "  /*MASK*/ %s,\n", mode.str ().c_str ());
      fprintf (fp, "  /*SIGNED*/ INVALID,\n");
      fprintf (fp, "  /*UNSIGNED*/ INVALID,\n");
      for (unsigned eew : {8, 16, 32, 64})
	fprintf (fp, "  /*EEW%d_INDEX*/ INVALID,\n", eew);
      fprintf (fp, "  /*SHIFT*/ INVALID,\n");
      fprintf (fp, "  /*DOUBLE_TRUNC*/ INVALID,\n");
      fprintf (fp, "  /*QUAD_TRUNC*/ INVALID,\n");
      fprintf (fp, "  /*OCT_TRUNC*/ INVALID,\n");
      fprintf (fp, "  /*DOUBLE_TRUNC_SCALAR*/ INVALID,\n");
      fprintf (fp, "  /*DOUBLE_TRUNC_SIGNED*/ INVALID,\n");
      fprintf (fp, "  /*DOUBLE_TRUNC_UNSIGNED*/ INVALID,\n");
      fprintf (fp, "  /*DOUBLE_TRUNC_UNSIGNED_SCALAR*/ INVALID,\n");
      fprintf (fp, "  /*DOUBLE_TRUNC_FLOAT*/ INVALID,\n");
      fprintf (fp, "  /*FLOAT*/ INVALID,\n");
      fprintf (fp, "  /*LMUL1*/ INVALID,\n");
      fprintf (fp, "  /*WLMUL1*/ INVALID,\n");
      for (unsigned eew : {8, 16, 32, 64})
	fprintf (fp, "  /*EEW%d_INTERPRET*/ INVALID,\n", eew);

      for (unsigned boolsize : BOOL_SIZE_LIST)
	fprintf (fp, "  /*BOOL%d_INTERPRET*/ INVALID,\n", boolsize);

      for (unsigned lmul_log2_offset : {1, 2, 3, 4, 5, 6})
	{
	  unsigned multiple_of_lmul = 1 << lmul_log2_offset;
	  fprintf (fp, "  /*X%d_INTERPRET*/ INVALID,\n", multiple_of_lmul);
	}
      fprintf (fp, "  /*TUPLE_SUBPART*/ INVALID\n");
      fprintf (fp, ")\n");
    }

  // Build for vint and vuint
  for (unsigned sew : {8, 16, 32, 64})
    for (int lmul_log2 : {-3, -2, -1, 0, 1, 2, 3})
      for (unsigned nf : {1, 2, 3, 4, 5, 6, 7, 8})
	for (bool unsigned_p : {false, true})
	  {
	    if (!valid_type (sew, lmul_log2, nf, /*float_t*/ false))
	      continue;

	    fprintf (fp, "DEF_RVV_TYPE_INDEX (\n");
	    fprintf (fp, "  /*VECTOR*/ %s,\n",
		     inttype (sew, lmul_log2, nf, unsigned_p).c_str ());
	    fprintf (fp, "  /*MASK*/ %s,\n",
		     maskmode (sew, lmul_log2).c_str ());
	    fprintf (fp, "  /*SIGNED*/ %s,\n",
		     inttype (sew, lmul_log2, /*unsigned_p*/ false).c_str ());
	    fprintf (fp, "  /*UNSIGNED*/ %s,\n",
		     inttype (sew, lmul_log2, /*unsigned_p*/ true).c_str ());
	    for (unsigned eew : {8, 16, 32, 64})
	      fprintf (fp, "  /*EEW%d_INDEX*/ %s,\n", eew,
		       same_ratio_eew_type (sew, lmul_log2, eew,
					    /*unsigned_p*/ true, false)
			 .c_str ());
	    fprintf (fp, "  /*SHIFT*/ %s,\n",
		     inttype (sew, lmul_log2, /*unsigned_p*/ true).c_str ());
	    fprintf (fp, "  /*DOUBLE_TRUNC*/ %s,\n",
		     same_ratio_eew_type (sew, lmul_log2, sew / 2, unsigned_p,
					  false)
		       .c_str ());
	    fprintf (fp, "  /*QUAD_TRUNC*/ %s,\n",
		     same_ratio_eew_type (sew, lmul_log2, sew / 4, unsigned_p,
					  false)
		       .c_str ());
	    fprintf (fp, "  /*OCT_TRUNC*/ %s,\n",
		     same_ratio_eew_type (sew, lmul_log2, sew / 8, unsigned_p,
					  false)
		       .c_str ());
	    fprintf (fp, "  /*DOUBLE_TRUNC_SCALAR*/ %s,\n",
		     same_ratio_eew_type (sew, lmul_log2, sew / 2, unsigned_p,
					  false)
		       .c_str ());
	    fprintf (fp, "  /*DOUBLE_TRUNC_SIGNED*/ INVALID,\n");
	    fprintf (fp, "  /*DOUBLE_TRUNC_UNSIGNED*/ %s,\n",
		     same_ratio_eew_type (sew, lmul_log2, sew / 2, true, false)
		       .c_str ());
	    if (unsigned_p)
	      fprintf (fp, "  /*DOUBLE_TRUNC_UNSIGNED_SCALAR*/ INVALID,\n");
	    else
	      fprintf (fp, "  /*DOUBLE_TRUNC_UNSIGNED_SCALAR*/ %s,\n",
		       same_ratio_eew_type (sew, lmul_log2, sew / 2, true,
					    false)
			 .c_str ());
	    fprintf (fp, "  /*DOUBLE_TRUNC_FLOAT*/ %s,\n",
		     same_ratio_eew_type (sew, lmul_log2, sew / 2, false, true)
		       .c_str ());
	    fprintf (fp, "  /*FLOAT*/ %s,\n",
		     floattype (sew, lmul_log2).c_str ());
	    fprintf (fp, "  /*LMUL1*/ %s,\n",
		     inttype (sew, /*lmul_log2*/ 0, unsigned_p).c_str ());
	    fprintf (fp, "  /*WLMUL1*/ %s,\n",
		     inttype (sew * 2, /*lmul_log2*/ 0, unsigned_p).c_str ());
	    for (unsigned eew : {8, 16, 32, 64})
	      {
		if (eew == sew)
		  fprintf (fp, "  /*EEW%d_INTERPRET*/ INVALID,\n", eew);
		else
		  fprintf (fp, "  /*EEW%d_INTERPRET*/ %s,\n", eew,
			   inttype (eew, lmul_log2, unsigned_p).c_str ());
	      }

	    for (unsigned boolsize : BOOL_SIZE_LIST)
	      {
		std::stringstream mode;
		mode << "vbool" << boolsize << "_t";

		fprintf (fp, "  /*BOOL%d_INTERPRET*/ %s,\n", boolsize,
			 nf == 1 && lmul_log2 == 0 ? mode.str ().c_str ()
						   : "INVALID");
	      }

	    for (unsigned lmul_log2_offset : {1, 2, 3, 4, 5, 6})
	      {
		unsigned multiple_of_lmul = 1 << lmul_log2_offset;
		fprintf (fp, "  /*X%d_VLMUL_EXT*/ %s,\n", multiple_of_lmul,
			 inttype (sew, lmul_log2 + lmul_log2_offset, unsigned_p)
			   .c_str ());
	      }
	    fprintf (fp, "  /*TUPLE_SUBPART*/ %s\n",
		     inttype (sew, lmul_log2, 1, unsigned_p).c_str ());
	    fprintf (fp, ")\n");
	  }
  // Build for vfloat
  for (unsigned sew : {32, 64})
    for (int lmul_log2 : {-3, -2, -1, 0, 1, 2, 3})
      for (unsigned nf : {1, 2, 3, 4, 5, 6, 7, 8})
	{
	  if (!valid_type (sew, lmul_log2, nf, /*float_t*/ true))
	    continue;

	  fprintf (fp, "DEF_RVV_TYPE_INDEX (\n");
	  fprintf (fp, "  /*VECTOR*/ %s,\n",
		   floattype (sew, lmul_log2, nf).c_str ());
	  fprintf (fp, "  /*MASK*/ %s,\n", maskmode (sew, lmul_log2).c_str ());
	  fprintf (fp, "  /*SIGNED*/ %s,\n",
		   inttype (sew, lmul_log2, /*unsigned_p*/ false).c_str ());
	  fprintf (fp, "  /*UNSIGNED*/ %s,\n",
		   inttype (sew, lmul_log2, /*unsigned_p*/ true).c_str ());
	  for (unsigned eew : {8, 16, 32, 64})
	    fprintf (fp, "  /*EEW%d_INDEX*/ %s,\n", eew,
		     same_ratio_eew_type (sew, lmul_log2, eew,
					  /*unsigned_p*/ true, false)
		       .c_str ());
	  fprintf (fp, "  /*SHIFT*/ INVALID,\n");
	  fprintf (fp, "  /*DOUBLE_TRUNC*/ %s,\n",
		   same_ratio_eew_type (sew, lmul_log2, sew / 2, false, true)
		     .c_str ());
	  fprintf (fp, "  /*QUAD_TRUNC*/ INVALID,\n");
	  fprintf (fp, "  /*OCT_TRUNC*/ INVALID,\n");
	  fprintf (fp, "  /*DOUBLE_TRUNC_SCALAR*/ %s,\n",
		   same_ratio_eew_type (sew, lmul_log2, sew / 2, false, true)
		     .c_str ());
	  fprintf (fp, "  /*DOUBLE_TRUNC_SIGNED*/ %s,\n",
		   same_ratio_eew_type (sew, lmul_log2, sew / 2, false, false)
		     .c_str ());
	  fprintf (fp, "  /*DOUBLE_TRUNC_UNSIGNED*/ %s,\n",
		   same_ratio_eew_type (sew, lmul_log2, sew / 2, true, false)
		     .c_str ());
	  fprintf (fp, "  /*DOUBLE_TRUNC_UNSIGNED_SCALAR*/ INVALID,\n");
	  fprintf (fp, "  /*DOUBLE_TRUNC_FLOAT*/ %s,\n",
		   same_ratio_eew_type (sew, lmul_log2, sew / 2, false, true)
		     .c_str ());
	  fprintf (fp, "  /*FLOAT*/ INVALID,\n");
	  fprintf (fp, "  /*LMUL1*/ %s,\n",
		   floattype (sew, /*lmul_log2*/ 0).c_str ());
	  fprintf (fp, "  /*WLMUL1*/ %s,\n",
		   floattype (sew * 2, /*lmul_log2*/ 0).c_str ());
	  for (unsigned eew : {8, 16, 32, 64})
	    fprintf (fp, "  /*EEW%d_INTERPRET*/ INVALID,\n", eew);

	  for (unsigned boolsize : BOOL_SIZE_LIST)
	    fprintf (fp, "  /*BOOL%d_INTERPRET*/ INVALID,\n", boolsize);

	  for (unsigned lmul_log2_offset : {1, 2, 3, 4, 5, 6})
	    {
	      unsigned multiple_of_lmul = 1 << lmul_log2_offset;
	      fprintf (fp, "  /*X%d_VLMUL_EXT*/ %s,\n", multiple_of_lmul,
		       floattype (sew, lmul_log2 + lmul_log2_offset).c_str ());
	    }
	  fprintf (fp, "  /*TUPLE_SUBPART*/ %s\n",
		   floattype (sew, lmul_log2, 1).c_str ());
	  fprintf (fp, ")\n");
	}

  return 0;
}