Bug 60396

Summary: Add RAII-style DenormalDisabler class to disable denormals which hurt audio performance
Product: WebKit Reporter: Chris Rogers <crogers>
Component: New BugsAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: aroben, barraclough, darin, eric, kbr, oliver, sam
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch kbr: review+

Description Chris Rogers 2011-05-06 12:42:34 PDT
Add macros to disable denormals on x86 for audio-intensive algorithms
Comment 1 Chris Rogers 2011-05-06 12:45:55 PDT
Created attachment 92627 [details]
Patch
Comment 2 Alexey Proskuryakov 2011-05-06 17:35:09 PDT
Can this be a RIAA-style class instead of a pair of macros? This class could disable denormals in constructor, and enable them back in destructor, making sure that we don't accidentally leak the state by taking an unusual code path with an early return.
Comment 3 Chris Rogers 2011-05-06 18:09:28 PDT
Sure, I can probably whip something up like that.  Just curious, what is RIAA?
Comment 5 Chris Rogers 2011-05-09 12:55:11 PDT
Created attachment 92834 [details]
Patch
Comment 6 Chris Rogers 2011-05-09 12:57:43 PDT
Latest patch turns this into an RIAA-style class.
Comment 7 Eric Seidel (no email) 2011-05-09 13:23:06 PDT
Comment on attachment 92834 [details]
Patch

Looks nice.  Need someone who actually knows about this stuff to comment though.
Comment 8 Eric Seidel (no email) 2011-05-09 13:23:47 PDT
CCing folks who might know something about denormal disabiling.
Comment 9 Oliver Hunt 2011-05-09 13:33:35 PDT
In terms of actual data behaviour what does this do?  If I load a denormalised value into an xmm register what happens?
Comment 10 Chris Rogers 2011-05-09 14:24:29 PDT
Oliver, I don't think a simple load will be any different.  Setting the denormal mask tells the processor to ignore any exceptions which occur due to any floating-point math which is dealing with denormals.  Not handling the exception causes a tiny amount of precision loss, which is acceptable for audio processing.  The performance benefit is very large because once these denormals get into a stateful audio processing algorithms they tend to stick around (in filter state, decaying delay lines, parameter slewing/de-glitching, etc.).  They can cause a very large number of exceptions to occur, causing enormous slowdown.

For all AudioUnits we used to disable them -- here's an old copy of an audio SDK file with this type of disabling (near the top of the file):
http://www.google.com/codesearch/p?hl=en#9WNLlqRP2nE/trunk/PublicUtility/AUPublic/AUBase/AUBase.cpp&q=AUPublic&sa=N&cd=2&ct=rc
Comment 11 Kenneth Russell 2011-05-09 14:49:21 PDT
I think Alexey meant RAII, not RIAA. Very different concepts. :)
Comment 12 Kenneth Russell 2011-05-09 14:59:19 PDT
Comment on attachment 92834 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=92834&action=review

Couple of minor comments; feel free to fix upon landing.

> Source/WebCore/ChangeLog:5
> +        Add RIAA-style DenormalDisabler class to disable denormals which hurt audio performance.

RIAA -> RAII

> Source/WebCore/platform/audio/DenormalDisabler.h:50
> +        asm volatile("stmxcsr %0" : "=m" (*&result));

It looks like the *& isn't needed here or on the ldmxcsr; see http://gcc.gnu.org/bugzilla/attachment.cgi?id=11062 .
Comment 13 Chris Rogers 2011-05-09 16:02:16 PDT
Committed r86099: <http://trac.webkit.org/changeset/86099>
Comment 14 Sam Weinig 2011-05-09 16:14:10 PDT
(In reply to comment #10)
> Oliver, I don't think a simple load will be any different.  Setting the denormal mask tells the processor to ignore any exceptions which occur due to any floating-point math which is dealing with denormals.  Not handling the exception causes a tiny amount of precision loss, which is acceptable for audio processing.  The performance benefit is very large because once these denormals get into a stateful audio processing algorithms they tend to stick around (in filter state, decaying delay lines, parameter slewing/de-glitching, etc.).  They can cause a very large number of exceptions to occur, causing enormous slowdown.
> 
> For all AudioUnits we used to disable them -- here's an old copy of an audio SDK file with this type of disabling (near the top of the file):
> http://www.google.com/codesearch/p?hl=en#9WNLlqRP2nE/trunk/PublicUtility/AUPublic/AUBase/AUBase.cpp&q=AUPublic&sa=N&cd=2&ct=rc

Can JavaScript run in the scope this is activated for?  If so, can the change in precision show up there?
Comment 15 Chris Rogers 2011-05-09 16:41:48 PDT
Hi Sam, no this will only run in audio-specific threads.  It's per-thread state and will not affect the main JS thread or any worker threads.