RESOLVED FIXED Bug 34538
audio engine: add Complex number class
https://bugs.webkit.org/show_bug.cgi?id=34538
Summary audio engine: add Complex number class
Chris Rogers
Reported 2010-02-03 12:18:18 PST
The audio engine needs a class representing a complex number. It's used in various DSP / math algorithms.
Attachments
patch for Complex number class (4.78 KB, patch)
2010-02-03 13:12 PST, Chris Rogers
no flags
addresses Darin Adler's initial comments (5.99 KB, patch)
2010-02-03 13:36 PST, Chris Rogers
no flags
Patch (2.71 KB, patch)
2010-02-10 14:37 PST, Chris Rogers
no flags
Patch (2.63 KB, patch)
2010-02-10 17:32 PST, Chris Rogers
no flags
Sam Weinig
Comment 1 2010-02-03 13:09:00 PST
Are there reasons we can't use std::complex other than the normal WebCore rule of not using stdlib types. If it is usable for your purposes, I think we should consider augmenting the rule for this, like we do for std::pair.
Chris Rogers
Comment 2 2010-02-03 13:12:34 PST
Created attachment 48062 [details] patch for Complex number class
Chris Rogers
Comment 3 2010-02-03 13:15:30 PST
Hi Sam, I anticipate extending this class in the future for various DSP algorithms, so I'd prefer to use our own implementation.
Darin Adler
Comment 4 2010-02-03 13:21:52 PST
Comment on attachment 48062 [details] patch for Complex number class > + Complex() : m_real(0.0), m_imag(0.0) { } We try to keep abbreviations to a minimum in WebKit and use words instead. So this would normally be called "imaginary", not "imag". > + double abs() const { return sqrt(m_real*m_real + m_imag*m_imag); } We put spaces around operators like "*". > + double magnitude() const { return abs(); } Do we really need both the names "abs()" and "magnitude()"? How about choosing just one? I suggest "magnitude". > + void setMagnitudePhase(double mag, double phase) > + { > + m_real = mag * cos(phase); > + m_imag = mag * sin(phase); > + } It seems like we'd want this to be a function that returns a Complex, rather than a setter. > +protected: > + double m_real; > + double m_imag; private, not protected.
Chris Rogers
Comment 5 2010-02-03 13:36:42 PST
Created attachment 48065 [details] addresses Darin Adler's initial comments Hi Darin, thanks for having such a quick look!
Darin Adler
Comment 6 2010-02-03 13:38:58 PST
(In reply to comment #3) > Hi Sam, I anticipate extending this class in the future for various DSP > algorithms, so I'd prefer to use our own implementation. I think we can add lots of functions that work with std::complex too, though, so I’m not sure a new class is the right way to go.
Chris Rogers
Comment 7 2010-02-03 13:47:01 PST
subclassing std::complex to add additional methods looks like a problem since the ivars are private. I'd prefer, if at all possible, to have something lightweight and more easily modifiable to our needs.
Darin Adler
Comment 8 2010-02-03 13:51:00 PST
(In reply to comment #7) > subclassing std::complex to add additional methods looks like a problem since > the ivars are private. I'd prefer, if at all possible, to have something > lightweight and more easily modifiable to our needs. The technique is not subclassing. It's functions that take and return complex objects. I hear you expressing your preference. But I don’t see a good reason to consider std::complex heavyweight. And it comes with many of the functions you’d want to use already implemented. We should really consider it seriously before adding our own. Maybe you could show some coding examples to clarify why the code ends up looking so much cleaner with your class than if you used std::complex?
Chris Rogers
Comment 9 2010-02-03 13:58:44 PST
OK - don't want to rock the boat too much :) I'm sure it will be possible to adapt to std::complex. This will probably be fine for the current engine code. If it starts to get too messy, as I've been fearing, then we can revisit it later. Thanks for having a look - I guess we can close out this bug...
Eric Seidel (no email)
Comment 10 2010-02-03 16:13:27 PST
Closing as wontfix per Chris' comment above.
Eric Seidel (no email)
Comment 11 2010-02-03 16:14:56 PST
Comment on attachment 48065 [details] addresses Darin Adler's initial comments Clearing review flag per Chris's above comment.
Chris Rogers
Comment 12 2010-02-03 16:24:07 PST
Sorry, I'm re-opening this. I posted to webkit-dev with the problems with std::complex - re-pasting here: In the process of switching my code over to use std::complex I noticed a conflict with isinf(), isnan(), etc. The problem is that simply including: #include <complex> breaks the isinf(), isnan() functions (and some others I think). So now I'm getting compile errors in any header files which use these functions, such as WebGLFloatArray.h (which I need to include for music visualizer stuff). I'm a bit queasy about all the side-effects of simply including <complex> and am not even sure how to address the current situation, short of switching all of webkit over to using std::isinf, std::isnan, etc. Now I remember having similar problems with this in other codebases I've worked on, as the effects of <complex> seem to be viral... Unless somebody has a reasonable fix for the sinister side-effects of including <complex> I'd like my patch to be re-considered. Thanks, Chris
Darin Adler
Comment 13 2010-02-04 11:59:10 PST
(In reply to comment #12) > So now I'm > getting compile errors in any header files which use these functions, such as > WebGLFloatArray.h Lets start by fixing those -- if it really ends up being too much of a pain we can fall back. I don’t want to give up at the first sign of trouble. It’s great to not have to reinvent the C++ library.
Chris Rogers
Comment 14 2010-02-04 13:41:52 PST
Hi Darin, I've been trying to move forward by changing all the uses of isnan() in the WebGL header files to std::isnan(). This also required including <cmath> in all of these header files so that std::isnan() is defined. But now this appears to be having a cascading effect, and this creates a compile error in JSImmediate.h (a use of the signbit() function) in JavaScriptCore. After including <cmath> here and using std::signbit(), I'm now getting a compile error in CachedResource ( function isfinite() ). I've found that the isfinite() function is used many places in the codebase, as well as isinf(), isnan(), etc. etc. So, I'm not sure what to do. I'm very uncomfortable trying to do a massive munge to convert the whole codebase to be <cmath> friendly since I'm very new here, especially considering possible differences across platforms. I think it would be a very large number of changes. What do you think we should do?
Darin Adler
Comment 15 2010-02-04 13:50:00 PST
(In reply to comment #14) > Hi Darin, I've been trying to move forward by changing all the uses of isnan() > in the WebGL header files to std::isnan(). This also required including > <cmath> in all of these header files so that std::isnan() is defined. Could you post your patch in progress somewhere so I can spend a little time on this?
Darin Adler
Comment 16 2010-02-04 13:59:30 PST
One of the first things I noticed is that many of those isnan checks are unneeded, because converting NaN to an integer already yields a zero. Not directly relevant, but something I spotted while looking at this.
Chris Rogers
Comment 17 2010-02-10 14:37:19 PST
Chris Rogers
Comment 18 2010-02-10 14:40:56 PST
Comment on attachment 48516 [details] Patch I've removed my original patch where I created a new complex number class which was defined in Complex.h. Instead, I've added a utilities header for adding new methods on std::complex as recommended by Darin Adler.
Darin Adler
Comment 19 2010-02-10 16:13:18 PST
Comment on attachment 48516 [details] Patch I like everything about this except for the suffix Utilities. I think this file would be just fine named Complex.h -- named after the typedef that's in it. > +#include <stdio.h> Why include this? We should not. I'm going to say review- because I think we should not choose this filename.
Chris Rogers
Comment 20 2010-02-10 17:32:41 PST
Chris Rogers
Comment 21 2010-02-10 17:34:30 PST
Comment on attachment 48533 [details] Patch changed filename from ComplexUtilities.h to Complex.h also removed #include <stdio.h>
WebKit Commit Bot
Comment 22 2010-02-11 16:20:08 PST
Comment on attachment 48533 [details] Patch Clearing flags on attachment: 48533 Committed r54684: <http://trac.webkit.org/changeset/54684>
WebKit Commit Bot
Comment 23 2010-02-11 16:20:20 PST
All reviewed patches have been landed. Closing bug.
Note You need to log in before you can comment on or make changes to this bug.