aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/math/rounding.c
blob: c2ce85e3f946ad34fb72bca208c2444a43d8f61c (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
#include <stdio.h>
#include <fenv.h>

#ifdef __K1C__
#include <mppa_bare_runtime/k1c/registers.h>
int fesetround(int rounding_mode) {
  if (rounding_mode < 0 || rounding_mode > 3) return 1;
  unsigned long long cs = __builtin_k1_get(K1_SFR_CS);
  cs = (cs & ~(3 << 16)) | (rounding_mode << 16);
  __builtin_k1_set(K1_SFR_CS, cs);
  return 0;
}

int fegetround(void) {
  unsigned long long cs = __builtin_k1_get(K1_SFR_CS);
  return (cs >> 16) & 3;
}
#endif

#pragma STDC FENV_ACCESS ON

double add(double x) {
  return x+0.1;
}

int main() {
  if (fesetround(FE_DOWNWARD)) {
    printf("fesetround(FE_DOWNWARD) unsuccessful\n");
  }
  double low = add(3.0);
  if (fesetround(FE_UPWARD)) {
    printf("fesetround(FE_UPWARD) unsuccessful\n");
  }
  double high = add(3.0);
  printf("%d %a %d\n", low==high, high-low, fegetround());
}