3

I have some very optimized code (signal processing) running perfectly in C# using Mono, cross platform. However, certain circumstances causes exponentially decaying numbers which by nature are destined to become denormal at some point.

Since this effectively reduces the performance of the application by orders of magnitude, I would like to get rid of them. However, checking for denormals everywhere they can occur incurs similar performance problems.

I know both the x87 FPU and the SSE FPU supports DAZ operations (denormals-are-zero/flush denormals to zero) which would solve all problems, however these control words are not accessible through C# it would seem.

I would need something that works crossplatform, and for both the x87 fpu and daz. What are my options? Can i p/invoke to control the fpu words? I know the runtime doesn't expect the change of the control word, however I'll reset the word back after the small timeframe i need it.

e: I wouldn't say this is a duplicate; it needs more research. The answer in the other says it isn't possible, and in the next line says it can be done though p/invoking. Obviously it will be reset on exceptions, but since i intend on setting it each time i need the feature (which you should always do, anyway) it would seem to be possible. Has anyone succeeded doing in this? I also can't seem to find a _control_fp in some module on OSX, does p/invoking work the same there?

Shaggi
  • 1,121
  • 1
  • 9
  • 31
  • The answer there does not say that you can p/invoke it, it says that even if you try to change it with p/invoke, you will not succeed in creating a usable environment. It's a duplicate because there is no answer to your question which would not equally well be an answer to the existing question (which also specifically asks about causing flush-to-zero). Not liking the answer is not justification for asking the same question again. – Ben Voigt Apr 15 '15 at 00:15
  • x87 FPU does not support DAZ or FTZ, rather it transforms anything to double extended (80-bit) numbers which have extended exponent ranges; thus any numbers which are denormals for your code are not handled specially by the FPU, and you do not see any performance impact. BTW, if you force FTZ/DAZ for SSE, you'll see more differences caused by double rounding on the x87 FPU: if you can live with it so far without noticing it, perhaps an useful solution to try is to use double instead of float for SSE; another track could be to go for IEEE-754 trap (not just quiet exceptions as we usually do.) – AntoineL Apr 27 '15 at 11:00
  • Letting you know Shaggi that this isn't a stupid question, or even a duplicate. I actually am interested in this as well, but I can understand why most people aren't interested in this sort of thing in C# and would prefer to discourage you based on "C# is high level duh". – Thomas May 22 '15 at 09:36

0 Answers0