robert bristow-johnson <
r...@audioimagination.com> writes:
> (cross posted to comp.dsp and comp.lang.c.)
>
> are compilers now-a-days smart enough to multiply two N-bit numbers to
> a 2N-bit result without wasting instructions in the generated machine
> code?
>
> like, we know for C, a long times a long is a long. but if you cast
> one of the multiplicands to (long long), and then multiply, you get a
> long long without any loss of bits. but are the compilers smart
> enough to avoid that? let
Keep in mind that long long is not *necessarily* twice as wide as long.
For example, on x86_64 Linux systems, both are typically 64 bits.
Your example would work better with int32_t and int64_t.
> long *h, *x, *y; // coefs, input, output ptrs
> int L; // FIR length
> int n=0;
>
> ...
>
> while(TRUE)
> {
>
> long long accumulator=0LL;
>
> for(int i=0; i<L; i++)
> {
> accumulator += ( (long long)(h[i]) )*(x[n-i]);
> }
>
> long y[n++] = (long)(accumulator>>(8*sizeof(long)-1);
>
> }
>
> (sorry for what google does to the = sign.)
The "=" characters look fine.
> it's not complete code, but i hope you can get the drift. can anyone
> comment on whether or not some particular compiler (gcc?) codes that
> to an efficient mac instruction without actually sign extending the
> value or h[i] and doing a long long multiply?
>
> this had been an old irritant with C, i think the language definition
> gets that wrong, but really smart compilers should know what to do,
> no?
Certainly a conforming compiler *can* perform this optimization. If it
can confirm that both operands of "*" are small enough (which should be
easy in this case), then it can determine that a 32-bit by 32-bit to
64-bit multiplication will yield the same result as a 64x64->64
multiplication.
I don't know which actual compilers do this. It would be easy enough to
find out by examining the generated assembl code (and being more
familiar with the instruction set than I am).
--
Keith Thompson (The_Other_Keith)
ks...@mib.org <
http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"