R
robert bristow-johnson
(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
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) )*(x[n-i]);
}
long y[n++] = (long)(accumulator>>(8*sizeof(long)-1);
}
(sorry for what google does to the = sign.)
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 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?
r b-j
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
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) )*(x[n-i]);
}
long y[n++] = (long)(accumulator>>(8*sizeof(long)-1);
}
(sorry for what google does to the = sign.)
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 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?
r b-j