[SOLVED] C++: which type of cast is best for audio callbacks?

Programming applications for making music on Linux.

Moderators: MattKingUSA, khz

CrocoDuck
Established Member
Posts: 1133
Joined: Sat May 05, 2012 6:12 pm
Been thanked: 17 times

[SOLVED] C++: which type of cast is best for audio callbacks?

Post by CrocoDuck »

Hi there, I am new to C++ programming, but thanks to this website I made it to the point I can understand how this example works. I see that Harry used C-style casts, for example:

Code: Select all

float* inputBuffer = (float*)jack_port_get_buffer ( inputPort , nframes);
However, after this section, I got used to static_cast and I would write that line as follows:

Code: Select all

float *inputBuffer = static_cast<float*>(jack_port_get_buffer(inputPort, nframes));
Seems to me that static_cast does not make run time checks, so I have the feeling it should be faster?

What do you think guys? Is there any reason to prefer a particular kind of cast in a callback?
Last edited by CrocoDuck on Sun Dec 04, 2016 12:43 pm, edited 1 time in total.
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: C++: which type of cast is best for audio callbacks?

Post by sadko4u »

I would like to say, for C++ best cast is reinterpret_cast in this case.
LSP (Linux Studio Plugins) Developer and Maintainer.
folderol
Established Member
Posts: 2069
Joined: Mon Sep 28, 2015 8:06 pm
Location: Here, of course!
Has thanked: 224 times
Been thanked: 400 times
Contact:

Re: C++: which type of cast is best for audio callbacks?

Post by folderol »

Static cast seems reasonable, but I'll watch to see who comes along with more explanation.
The Yoshimi guy {apparently now an 'elderly'}
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: C++: which type of cast is best for audio callbacks?

Post by sadko4u »

folderol wrote:Static cast seems reasonable, but I'll watch to see who comes along with more explanation.
Here's some explanation:
Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.
So by using reinterpret_cast, you say to compiler: "This pointer really points to beginning of array of float".

http://en.cppreference.com/w/cpp/langua ... rpret_cast
LSP (Linux Studio Plugins) Developer and Maintainer.
CrocoDuck
Established Member
Posts: 1133
Joined: Sat May 05, 2012 6:12 pm
Been thanked: 17 times

Re: C++: which type of cast is best for audio callbacks?

Post by CrocoDuck »

Very interesting. Thanks!
tramp
Established Member
Posts: 2335
Joined: Mon Jul 01, 2013 8:13 am
Has thanked: 9 times
Been thanked: 454 times

Re: C++: which type of cast is best for audio callbacks?

Post by tramp »

sadko4u wrote:So by using reinterpret_cast, you say to compiler: "This pointer really points to beginning of array of float".
A reinterpret_cast is a cast that represents an unsafe conversion that might reinterpret the bits of one value as the bits of another value. For example, casting an int* to a double* is legal with a reinterpret_cast, though the result is unspecified. Similarly, casting an int to a void* is perfectly legal with reinterpret_cast, though it's unsafe. Only use reinterpret_cast if what you're doing really is changing the interpretation of some bits in the machine.
In general, you should always prefer static_cast for casts that should be safe. If you accidentally try doing a cast that isn't well-defined, then the compiler will report an error.
On the road again.
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: C++: which type of cast is best for audio callbacks?

Post by sadko4u »

tramp wrote:Only use reinterpret_cast if what you're doing really is changing the interpretation of some bits in the machine.
Sure, that's why I think that reinterpret_cast is more usable to assume chunk of memory to be array of floats rather than static_cast.
In general, you should always prefer static_cast for casts that should be safe. If you accidentally try doing a cast that isn't well-defined, then the compiler will report an error.
That's right and true for some objects with some hierarchy but not for low-level programming where each chunk of memory should be straightforward interpreted as some collection of objects of some data type.
LSP (Linux Studio Plugins) Developer and Maintainer.
folderol
Established Member
Posts: 2069
Joined: Mon Sep 28, 2015 8:06 pm
Location: Here, of course!
Has thanked: 224 times
Been thanked: 400 times
Contact:

Re: C++: which type of cast is best for audio callbacks?

Post by folderol »

Sounds potentially rather dangerous.

Analogy
Yes it's a gun. Yes I know it's loaded. Yes I know the safety is off, but I know what I'm doi{bang}.
The Yoshimi guy {apparently now an 'elderly'}
tramp
Established Member
Posts: 2335
Joined: Mon Jul 01, 2013 8:13 am
Has thanked: 9 times
Been thanked: 454 times

Re: C++: which type of cast is best for audio callbacks?

Post by tramp »

sadko4u wrote:
tramp wrote:Only use reinterpret_cast if what you're doing really is changing the interpretation of some bits in the machine.
Sure, that's why I think that reinterpret_cast is more usable to assume chunk of memory to be array of floats rather than static_cast.
In general, you should always prefer static_cast for casts that should be safe. If you accidentally try doing a cast that isn't well-defined, then the compiler will report an error.
That's right and true for some objects with some hierarchy but not for low-level programming where each chunk of memory should be straightforward interpreted as some collection of objects of some data type.
sadko4u wrote:Here's some explanation:
Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.
Well, let's check this explanation:

Let's take a little, basic test program:

Code: Select all

int main()
{
    float buf[10] = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
    float f = 0;
   
    float *ptr = static_cast<float*>(buf);
   
    //float *ptr = reinterpret_cast<float*>(buf);
   
    for (int i = 0; i<10; ++i) {
         f = ptr[i] ;
    }
   
   return 0;
}
and let's build verbose assembler code from it:

Code: Select all

 c++ -S -fverbose-asm -g -O2 cast_test.cpp -o static_cast_test.s
now comment out the static_cast, UN-commend the reinterpret_cast and build the assembly:

Code: Select all

c++ -S -fverbose-asm -g -O2 cast_test.cpp -o reinterpret_cast_test.s
Now, let's check the diff:

Code: Select all

 diff -Nur static_cast_test.s reinterpret_cast_test.s
turns out:

Code: Select all

--- static_cast_test.s	2016-12-04 04:33:06.850996404 +0100
+++ reinterpret_cast_test.s	2016-12-04 04:33:34.378996375 +0100
@@ -3,8 +3,8 @@
 #	compiled by GNU C version 4.9.2, GMP version 6.0.0, MPFR version 3.1.2-p3, MPC version 1.0.2
 # GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 # options passed:  -imultiarch x86_64-linux-gnu -D_GNU_SOURCE cast_test.cpp
-# -mtune=generic -march=x86-64 -auxbase-strip static_cast_test.s -g -O2
-# -fverbose-asm
+# -mtune=generic -march=x86-64 -auxbase-strip reinterpret_cast_test.s -g
+# -O2 -fverbose-asm
 # options enabled:  -faggressive-loop-optimizations
 # -fasynchronous-unwind-tables -fauto-inc-dec -fbranch-count-reg
 # -fcaller-saves -fcombine-stack-adjustments -fcommon -fcompare-elim
As you can see, the only difference here is the name of the file produced, as we produce verbose assembler code. Lets check the same again with plain assembler.

Code: Select all

 c++ -S  cast_test.cpp -o static_cast_test.s
switch comment and

Code: Select all

c++ -S  cast_test.cpp -o reinterpret_cast_test.s
do the diff

Code: Select all

diff -Nur static_cast_test.s reinterpret_cast_test.s
outputs: Conclusion , the only "advantage" you've when using reinterpret_cast in this case, is, ta, ta,tata, right, you've turn of the compiler type check, there is no difference in runtime behave. As the static_cast is, same as the reinterpret_cast, a pure compiler directive.
The using of a reinterpret_cast on a foreign function call, is in general no good idea, as you cant grant the result for the future.
On the road again.
CrocoDuck
Established Member
Posts: 1133
Joined: Sat May 05, 2012 6:12 pm
Been thanked: 17 times

Re: C++: which type of cast is best for audio callbacks?

Post by CrocoDuck »

Thank you for your detailed explanation guys, it is really helping me understanding more about casting. I think I will perhaps stick with static_cast for the time being.
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: C++: which type of cast is best for audio callbacks?

Post by sadko4u »

tramp wrote:Conclusion , the only "advantage" you've when using reinterpret_cast in this case, is, ta, ta,tata, right, you've turn of the compiler type check, there is no difference in runtime behave. As the static_cast is, same as the reinterpret_cast, a pure compiler directive.
The using of a reinterpret_cast on a foreign function call, is in general no good idea, as you cant grant the result for the future.
I don't want to make holy wars around C++ because I consider C++ a very dangerous language with complicated design. You're right that static_cast does nothing. But I prefer to use reinterpret_cast in these cases just to tell to the compiler "even don't try to do something with this". Also, reinterpret_cast is the most similar form of the C-style cast (type *)x.
LSP (Linux Studio Plugins) Developer and Maintainer.
tramp
Established Member
Posts: 2335
Joined: Mon Jul 01, 2013 8:13 am
Has thanked: 9 times
Been thanked: 454 times

Re: C++: which type of cast is best for audio callbacks?

Post by tramp »

sadko4u wrote:I don't want to make holy wars around C++ because I consider C++ a very dangerous language with complicated design. You're right that static_cast does nothing. But I prefer to use reinterpret_cast in these cases just to tell to the compiler "even don't try to do something with this". Also, reinterpret_cast is the most similar form of the C-style cast (type *)x.
sadko4u, you can use what ever you wont, you can prefer what ever you wont, in your project. I prefer compiler warnings/errors during compile time,when something is wrong, instead receive a crash during run. Therefor I switch on in principle any compiler checks I can get…
Also, keep in mind that the use of reinterpret_cast is restricted in any bigger C++ project I'm aware of, for the reasons I've given here. Patches or commits, which makes use of it unfounded, will be refused. It is considered as "bad codding style",
CrocoDuck wrote:Hi there, I am new to C++ programming,
and I don't think it is a good idea to advice it here for no reason.
On the road again.
CrocoDuck
Established Member
Posts: 1133
Joined: Sat May 05, 2012 6:12 pm
Been thanked: 17 times

Re: C++: which type of cast is best for audio callbacks?

Post by CrocoDuck »

tramp wrote: and I don't think it is a good idea to advice it here for no reason.
The very first sticky thread here is called Developers Introduction & Tutorials. I apologize if I misused this communication channel, but that made me think that this was a good place as any to ask for advice.
sadko4u wrote: I consider C++ a very dangerous language with complicated design.
That's exactly the reason why people in my position need to ask questions, as the language itself has many pitfalls and, especially in the audio realm, the documentation is sparse and hard to read.

Anyway, if there is a more appropriate location, on this forum or somewhere else, to ask for advice let me know. I guess I will retain from posting ever again in this section.

Thank you very much for your time and help :wink: , you answered my question well and I am gonna mark this as Solved.
tramp
Established Member
Posts: 2335
Joined: Mon Jul 01, 2013 8:13 am
Has thanked: 9 times
Been thanked: 454 times

Re: [SOLVED] C++: which type of cast is best for audio callbacks?

Post by tramp »

CrocoDuck wrote:The very first sticky thread here is called Developers Introduction & Tutorials. I apologize if I misused this communication channel, but that made me think that it was a good place as any to ask for advice.
Don't get me wrong, CrocoDuck.
You are in the right place here.
I just pointed out, that the advice sadko4u gives, is wrong by assumptions and dangerous by doing.
My commend goes to sadko4u, not to you.
CrocoDuck wrote: That's exactly the reason why people in my position need to ask questions, as the language itself has many pitfalls and, especially in the audio realm, the documentation is sparse and hard to read.
:) , you are welcome

EDIT:// CrocoDuck, I've just re-read my post before, and now I see why you thing my comment goes to your stance, no, it didn't, it is just the follow-up from the previous sentence. Sorry for the confusion about that.
On the road again.
CrocoDuck
Established Member
Posts: 1133
Joined: Sat May 05, 2012 6:12 pm
Been thanked: 17 times

Re: [SOLVED] C++: which type of cast is best for audio callbacks?

Post by CrocoDuck »

tramp wrote: You are in the right place here.
Uh oh, sorry, I misinterpreted your comment... oops :oops:
tramp wrote: and I don't think it is a good idea to advice it here for no reason.
For some whatever reason I read it as "and I don't think it as a good idea to ask for advice here for no reason". I still have to wake up properly...
Post Reply