2
2
#
3
3
# Copyright (c) 2013 Victor Oliveira <[email protected] >
4
4
# Copyright (c) 2013 Jesse Towner <[email protected] >
5
+ # Copyright (c) 2024 Romaric Jodin <[email protected] >
5
6
#
6
7
# Permission is hereby granted, free of charge, to any person obtaining a copy
7
8
# of this software and associated documentation files (the "Software"), to deal
26
27
#
27
28
# convert_<destTypen><_sat><_roundingMode>(<sourceTypen>)
28
29
30
+ import argparse
31
+
32
+ parser = argparse .ArgumentParser ()
33
+ parser .add_argument (
34
+ "--clspv" , action = "store_true" , help = "Generate the clspv variant of the code"
35
+ )
36
+ args = parser .parse_args ()
37
+
38
+ clspv = args .clspv
39
+
29
40
types = [
30
41
"char" ,
31
42
"uchar" ,
@@ -251,13 +262,19 @@ def generate_default_conversion(src, dst, mode):
251
262
print ("#endif" )
252
263
253
264
254
- for src in types :
255
- for dst in types :
256
- generate_default_conversion (src , dst , "" )
265
+ # Do not generate default conversion for clspv as they are handled natively
266
+ if not clspv :
267
+ for src in types :
268
+ for dst in types :
269
+ generate_default_conversion (src , dst , "" )
257
270
258
271
for src in int_types :
259
272
for dst in int_types :
260
273
for mode in rounding_modes :
274
+ # Do not generate "_rte" conversion for clspv as they are handled
275
+ # natively
276
+ if clspv and mode == "_rte" :
277
+ continue
261
278
generate_default_conversion (src , dst , mode )
262
279
263
280
#
@@ -304,21 +321,38 @@ def generate_saturated_conversion(src, dst, size):
304
321
305
322
elif src in float_types :
306
323
307
- # Conversion from float to int
308
- print (
309
- """ {DST}{N} y = convert_{DST}{N}(x);
310
- y = select(y, ({DST}{N}){DST_MIN}, {BP}(x < ({SRC}{N}){DST_MIN}){BS});
311
- y = select(y, ({DST}{N}){DST_MAX}, {BP}(x > ({SRC}{N}){DST_MAX}){BS});
312
- return y;""" .format (
313
- SRC = src ,
314
- DST = dst ,
315
- N = size ,
316
- DST_MIN = limit_min [dst ],
317
- DST_MAX = limit_max [dst ],
318
- BP = bool_prefix ,
319
- BS = bool_suffix ,
324
+ if clspv :
325
+ # Conversion from float to int
326
+ print (
327
+ """ {DST}{N} y = convert_{DST}{N}(x);
328
+ y = select(y, ({DST}{N}){DST_MIN}, {BP}(x <= ({SRC}{N}){DST_MIN}){BS});
329
+ y = select(y, ({DST}{N}){DST_MAX}, {BP}(x >= ({SRC}{N}){DST_MAX}){BS});
330
+ return y;""" .format (
331
+ SRC = src ,
332
+ DST = dst ,
333
+ N = size ,
334
+ DST_MIN = limit_min [dst ],
335
+ DST_MAX = limit_max [dst ],
336
+ BP = bool_prefix ,
337
+ BS = bool_suffix ,
338
+ )
339
+ )
340
+ else :
341
+ # Conversion from float to int
342
+ print (
343
+ """ {DST}{N} y = convert_{DST}{N}(x);
344
+ y = select(y, ({DST}{N}){DST_MIN}, {BP}(x < ({SRC}{N}){DST_MIN}){BS});
345
+ y = select(y, ({DST}{N}){DST_MAX}, {BP}(x > ({SRC}{N}){DST_MAX}){BS});
346
+ return y;""" .format (
347
+ SRC = src ,
348
+ DST = dst ,
349
+ N = size ,
350
+ DST_MIN = limit_min [dst ],
351
+ DST_MAX = limit_max [dst ],
352
+ BP = bool_prefix ,
353
+ BS = bool_suffix ,
354
+ )
320
355
)
321
- )
322
356
323
357
else :
324
358
@@ -432,7 +466,10 @@ def generate_float_conversion(src, dst, size, mode, sat):
432
466
print (" return convert_{DST}{N}(x);" .format (DST = dst , N = size ))
433
467
else :
434
468
print (" {DST}{N} r = convert_{DST}{N}(x);" .format (DST = dst , N = size ))
435
- print (" {SRC}{N} y = convert_{SRC}{N}(r);" .format (SRC = src , N = size ))
469
+ if clspv :
470
+ print (" {SRC}{N} y = convert_{SRC}{N}_sat(r);" .format (SRC = src , N = size ))
471
+ else :
472
+ print (" {SRC}{N} y = convert_{SRC}{N}(r);" .format (SRC = src , N = size ))
436
473
if mode == "_rtz" :
437
474
if src in int_types :
438
475
print (
@@ -448,23 +485,59 @@ def generate_float_conversion(src, dst, size, mode, sat):
448
485
else :
449
486
print (" {SRC}{N} abs_x = fabs(x);" .format (SRC = src , N = size ))
450
487
print (" {SRC}{N} abs_y = fabs(y);" .format (SRC = src , N = size ))
451
- print (
452
- " return select(r, nextafter(r, sign(r) * ({DST}{N})-INFINITY), convert_{BOOL}{N}(abs_y > abs_x));" .format (
453
- DST = dst , N = size , BOOL = bool_type [dst ]
488
+ if clspv :
489
+ print (
490
+ " {BOOL}{N} c = convert_{BOOL}{N}(abs_y > abs_x);" .format (
491
+ BOOL = bool_type [dst ], N = size
492
+ )
493
+ )
494
+ if sizeof_type [src ] >= 4 and src in int_types :
495
+ print (
496
+ " c = c || convert_{BOOL}{N}(({SRC}{N}){SRC_MAX} == x);" .format (
497
+ BOOL = bool_type [dst ], N = size , SRC = src , SRC_MAX = limit_max [src ]
498
+ )
499
+ )
500
+ print (
501
+ " return select(r, nextafter(r, sign(r) * ({DST}{N})-INFINITY), c);" .format (
502
+ DST = dst , N = size , BOOL = bool_type [dst ], SRC = src
503
+ )
504
+ )
505
+ else :
506
+ print (
507
+ " return select(r, nextafter(r, sign(r) * ({DST}{N})-INFINITY), convert_{BOOL}{N}(abs_y > abs_x));" .format (
508
+ DST = dst , N = size , BOOL = bool_type [dst ]
509
+ )
454
510
)
455
- )
456
511
if mode == "_rtp" :
457
512
print (
458
513
" return select(r, nextafter(r, ({DST}{N})INFINITY), convert_{BOOL}{N}(y < x));" .format (
459
514
DST = dst , N = size , BOOL = bool_type [dst ]
460
515
)
461
516
)
462
517
if mode == "_rtn" :
463
- print (
464
- " return select(r, nextafter(r, ({DST}{N})-INFINITY), convert_{BOOL}{N}(y > x));" .format (
465
- DST = dst , N = size , BOOL = bool_type [dst ]
518
+ if clspv :
519
+ print (
520
+ " {BOOL}{N} c = convert_{BOOL}{N}(y > x);" .format (
521
+ BOOL = bool_type [dst ], N = size
522
+ )
523
+ )
524
+ if sizeof_type [src ] >= 4 and src in int_types :
525
+ print (
526
+ " c = c || convert_{BOOL}{N}(({SRC}{N}){SRC_MAX} == x);" .format (
527
+ BOOL = bool_type [dst ], N = size , SRC = src , SRC_MAX = limit_max [src ]
528
+ )
529
+ )
530
+ print (
531
+ " return select(r, nextafter(r, ({DST}{N})-INFINITY), c);" .format (
532
+ DST = dst , N = size , BOOL = bool_type [dst ], SRC = src
533
+ )
534
+ )
535
+ else :
536
+ print (
537
+ " return select(r, nextafter(r, ({DST}{N})-INFINITY), convert_{BOOL}{N}(y > x));" .format (
538
+ DST = dst , N = size , BOOL = bool_type [dst ]
539
+ )
466
540
)
467
- )
468
541
469
542
# Footer
470
543
print ("}" )
@@ -484,4 +557,8 @@ def generate_float_conversion(src, dst, size, mode, sat):
484
557
for dst in float_types :
485
558
for size in vector_sizes :
486
559
for mode in rounding_modes :
560
+ # Do not generate "_rte" conversion for clspv as they are
561
+ # handled natively
562
+ if clspv and mode == "_rte" :
563
+ continue
487
564
generate_float_conversion (src , dst , size , mode , "" )
0 commit comments