Rounding discussion on KVR: worth investigating?


#1

this thread is discussing rounding float to int in various ways. i thought to myself “oh, i just let juce do that for me, it’s already got a nice function in it”. but then someone says this:

… which someone else acknowledges as happening to them.

thought i might bring that to your attention in case it’s something that the Juce codebase might have caused.


#2

No, I don’t do anything involving FPU settings in juce, and in tracktion all I did was an occasional reset because plugins would sometimes mess around with it and not put things back.

If you’re using the roundDoubleToInt function, it’s unaffected by this anyway, as it uses a cunning trick rather than the FPU.


#3

that’s cool, just thought i’d raise it here in case there was anything fishy going on.


#4

Related question: Regarding the cunning union method, could it be used for a truncating floatToInt aswell?

Reason I’m asking is that I currently use an asm function from Music DSP for that purpose, but apparently it’s not a 100%.
(The discussion on KvR shows that finding an optimized truncating xplat floatToInt function is not trivial.)


#5

Not really, unless you maybe subtracted 0.5 before calling it.

TBH I don’t fully understand why it works, it’s just something I picked up somewhere.


#6

I shall have to test and see if that’s faster than a regular cast.

Edit:
For what it’s worth, in clock ticks:

Plain cast: 1421
Asm Truncation: 797
Juce Rounding (-0.5f): 953


#7