aboutsummaryrefslogtreecommitdiff
path: root/none/tests/s390x/fixbr.c
blob: ba99210b5e11e5b9425293cb7449283e6bd75457 (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
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include "opcodes.h"
#include "rounding.h"

/* Test "fixbr"  with rounding mode given in insn (m3 field)
   Covers all generally available rounding modes that can be mapped to
   IRRoundingMode. As a consequence m3=1 which is "round to nearest with
   ties away from 0" is not tested here.
*/

const char *
rtext(unsigned m3_round)
{
   switch (m3_round) {
   case 0: return "[-> per fpc]";
   case 1: return "[-> nearest away]";
   case 3: return "[-> prepare short]";   // floating point extension fac needed
   case 4: return "[-> nearest even]";
   case 5: return "[-> 0]";
   case 6: return "[-> +inf]";
   case 7: return "[-> -inf]";
   }
   assert(0);
}

#define round_to_int(value,round)                             \
do {                                                          \
   long double src = value;                                   \
   long double dst;                                           \
                                                              \
   __asm__ volatile ("fixbr %[dst]," #round ",%[src]\n\t"     \
                     : [dst] "=f"(dst)                        \
                     : [src] "f"(src));                       \
                                                              \
   printf("fixbr %.5Lf\t-> %Lg  %s\n",                        \
          src, dst, rtext(round));                            \
} while (0)

#define fixbr(value,round) round_to_int(value,round)

void
set_rounding_mode(unsigned mode)
{
   register unsigned r asm("1") = mode;
   __asm__ volatile ( SFPC(1) : : "d"(r) );
}


int main(void)
{
   int j;
   static const long double dval[] = {
      1.25, 1.5, 2.5, 1.75, -1.25, -1.5, -2.5, -1.75, 0.0,
   };

   assert(sizeof(long double) == 16);
   
   /* f128 -> f128, round to int */
   for (j = 0; j < sizeof dval / sizeof dval[0]; ++j) {
      set_rounding_mode(FPC_BFP_ROUND_ZERO);
      fixbr(dval[j], M3_BFP_ROUND_NEAREST_EVEN);
      set_rounding_mode(FPC_BFP_ROUND_NEAREST_EVEN);
      fixbr(dval[j], M3_BFP_ROUND_ZERO);
      fixbr(dval[j], M3_BFP_ROUND_POSINF);
      fixbr(dval[j], M3_BFP_ROUND_NEGINF);
   }

   return 0;
}