3

I want to play with interval arithmetic in Rust, and to do so I need to set the rounding mode upward or downward. As I understand from some searching, setting the rounding mode will affect the performance, so I'd want to set it at compile time.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Houss_gc
  • 727
  • 5
  • 27
  • if you know how, share your knowledge with us please. – Houss_gc Apr 19 '16 at 16:54
  • http://www.gnu.org/software/libc/manual/html_node/Rounding.html might help. It probably will apply to gcc's implementation of C and C++. No clue about Rust or how Clang implements C/C++. Note the comments at the very end, as well as comments under "round to nearest". – thurizas Apr 19 '16 at 17:01
  • 2
    Relevant: http://stackoverflow.com/questions/28121957/how-do-i-specify-the-rounding-mode-for-floating-point-numbers – Veedrac Apr 19 '16 at 23:03
  • 2
    Relevant: https://users.rust-lang.org/t/request-for-some-numerics-related-features/3530/5 – Veedrac Apr 19 '16 at 23:05

1 Answers1

3

Not reliably, no. The problem is that the LLVM backend doesn't provide any support for modifying the rounding mode, though recently there have been some proposals to fix this, it's not likely to be resolved in the near future.

You might be able to call out to the C fesetround function (in fenv.h) at the start of your program, but the problem is that certain optimisations (such as constant folding) would have already been performed using the default rounding mode.

Simon Byrne
  • 7,694
  • 1
  • 26
  • 50
  • ok. I see, maybe if I can desible such optimisation and change rounding using some inline assembly I can get the bahavior that I want. – Houss_gc Apr 22 '16 at 10:49
  • I would call `fesetround` rather than use assembly: on x86 there are actually 2 different registers you need to change: the MXCSR for SSE operations, and FPSCR for x87 operations. – Simon Byrne Apr 22 '16 at 12:01
  • Yes, I have to change the two registers, but now I am facing another problem which I address in this [post](http://stackoverflow.com/questions/36789681/which-llvm-passes-are-responsible-for-floating-point-optimizations) – Houss_gc Apr 22 '16 at 16:27