DSPRelated.com
Forums

32 bit * 32 bit mutliplication in C

Started by Ashish Gupta November 4, 2008
Hi,
I am trying to do 32 bit * 32 bit multiplication on C55 x processor in C . I
am unable to get the appropriate instruction or intrinsic for that. Can u
help in this regard. Or we have to do this operation in assembly only.

Ashish
Hi,

Like any other compiler, I think the normal ' * ' operator in c55x will
implicitly do it for you.

Karthik Balaguru

Larsen & Toubro Infotech Ltd.
www.Lntinfotech.com

This Document is classified as:

L&T Infotech Proprietary L&T Infotech Confidential L&T Infotech
Internal Use Only L&T Infotech General Business

This Email may contain confidential or privileged information for the
intended recipient (s) If you are not the intended recipient, please do
not use or disseminate the information, notify the sender and delete it
from your system.

"Ashish Gupta"
Sent by: c...
11/04/2008 09:39 AM
Please respond to
c...
To
c...
cc

Subject
[c55x] 32 bit * 32 bit mutliplication in C

Hi,
I am trying to do 32 bit * 32 bit multiplication on C55 x processor in C .
I
am unable to get the appropriate instruction or intrinsic for that. Can u
help in this regard. Or we have to do this operation in assembly only.

Ashish

______________________________________________________________________
______________________________________________________________________
SSB0aGluayB5b3UgY2FuIHVzZSBkc3BsaWIgZnVuY3Rpb25zLHRoZXJlIGlzIG11bDMyIGZ1
bmN0aW9uIGluIGl0CgoyMDA4LTExLTA1IAoKCgpjaGVua2Fpc2hpeWkKCgoKt6K8/sjLo7og
QXNoaXNoIEd1cHRhIAq3osvNyrG85KO6IDIwMDgtMTEtMDQgIDIwOjU0OjI0IArK1bz+yMuj
uiBjNTV4IAqzrcvNo7ogCtb3zOKjuiBbYzU1eF0gMzIgYml0ICogMzIgYml0IG11dGxpcGxp
Y2F0aW9uIGluIEMgCiAKSGksCkkgYW0gdHJ5aW5nIHRvIGRvIDMyIGJpdCAqIDMyIGJpdCBt
dWx0aXBsaWNhdGlvbiBvbiBDNTUgeCBwcm9jZXNzb3IgaW4gQyAuIEkKYW0gdW5hYmxlIHRv
IGdldCB0aGUgYXBwcm9wcmlhdGUgaW5zdHJ1Y3Rpb24gb3IgaW50cmluc2ljIGZv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BMaW5rcwoKPCo+IFRvIHZpc2l0IHlvdXIgZ3Jv
dXAgb24gdGhlIHdlYiwgZ28gdG86CiAgICBodHRwOi8vZ3JvdXBzLnlhaG9vLmNvbS9ncm91
cC9jNTV4LwoKPCo+IFlvdXIgZW1haWwgc2V0dGluZ3M6CiAgICBJbmRpdmlkdWFsIEVtYWls
IHwgVHJhZGl0aW9uYWwKCjwqPiBUbyBjaGFuZ2Ugc2V0dGluZ3Mgb25saW5lIGdvIHRvOgog
ICAgaHR0cDovL2dyb3Vwcy55YWhvby5jb20vZ3JvdXAvYzU1eC9qb2luCiAgICAoWWFob28h
IElEIHJlcXVpcmVkKQoKPCo+IFRvIGNoYW5nZSBzZXR0aW5ncyB2aWEgZW1haWw6CiAgICBt
YWlsdG86YzU1eC1kaWdlc3RAeWFob29ncm91cHMuY29tIAogICAgbWFpbHRvOmM1NXgtZnVs
bGZlYXR1cmVkQHlhaG9vZ3JvdXBzLmNvbQoKPCo+IFRvIHVuc3Vic2NyaWJlIGZyb20gdGhp
cyBncm91cCwgc2VuZCBhbiBlbWFpbCB0bzoKICAgIGM1NXgtdW5zdWJzY3JpYmVAeWFob29n
cm91cHMuY29tCgo8Kj4gWW91ciB1c2Ugb2YgWWFob28hIEdyb3VwcyBpcyBzdWJqZWN0IHRv
OgogICAgaHR0cDovL2RvY3MueWFob28uY29tL2luZm8vdGVybXMvCgo
Ashish--
Check this out:

How to Write *Multiplies Correctly in C*
Code

http://focus.ti.com.cn/cn/lit/an/spra683/spra683.pdf
--Bhooshan

On Tue, Nov 4, 2008 at 9:39 AM, Ashish Gupta wrote:

> Hi,
> I am trying to do 32 bit * 32 bit multiplication on C55 x processor in C .
> I
> am unable to get the appropriate instruction or intrinsic for that. Can u
> help in this regard. Or we have to do this operation in assembly only.
>
> Ashish
>
We performing 32x32 or 32x16 multiplication two questions should be answered:

1. Is this a fractional multiply or an integer multiply?
2. Is the 64 bit result required for the intended application?

-Aaron

Hi,
>I am trying to do 32 bit * 32 bit multiplication on C55 x processor in C . I
>am unable to get the appropriate instruction or intrinsic for that. Can u
>help in this regard. Or we have to do this operation in assembly only.
>
>Ashish
Hi Aaron ,
This is a fractional multiply and 64 bit result is required.
Ashish

On Thu, Nov 6, 2008 at 10:58 PM, wrote:

> We performing 32x32 or 32x16 multiplication two questions should be
> answered:
>
> 1. Is this a fractional multiply or an integer multiply?
> 2. Is the 64 bit result required for the intended application?
>
> -Aaron
>
> Hi,
> >I am trying to do 32 bit * 32 bit multiplication on C55 x processor in C .
> I
> >am unable to get the appropriate instruction or intrinsic for that. Can u
> >help in this regard. Or we have to do this operation in assembly only.
> >
> >Ashish
>
Hai Ashish,
64 bit result directly is not possible on c55x as the max data type available on c55x platform is 40bit(long long). But you can capture it in two 32bit variables with some kind of work around. If you are truncating/rounding 64bit to 32bit at some point in your code then this method will work.

Usually in algorithms at some point these 64bit variable are truncated/rounded to 32bit. You have to do 32x32 in terms 16x16 and should take care of carry during additions.

It may take at least 12cycles to do mac of kind 64bit + = 32bit x 32bit.

-Santhosh

--- On Fri, 11/7/08, Ashish Gupta wrote:
From: Ashish Gupta
Subject: Re: [c55x] Re: 32 bit * 32 bit mutliplication in C
To: c...
Date: Friday, November 7, 2008, 9:59 PM

Hi Aaron ,
This is a fractional multiply and 64 bit result is required.
Ashish
On Thu, Nov 6, 2008 at 10:58 PM, wrote:

We performing 32x32 or 32x16 multiplication two questions should be answered:

1. Is this a fractional multiply or an integer multiply?
2. Is the 64 bit result required for the intended application?

-Aaron
Hi,
>I am trying to do 32 bit * 32 bit multiplication on C55 x processor in C . I
>am unable to get the appropriate instruction or intrinsic for that. Can u
>help in this regard. Or we have to do this operation in assembly only.

>
>Ashish
Hi Ashish,

Here is what I use presently for C-code 32x32 multiplies on the C55,
but you may want to run a few values considering a carry as mention by
Santhosh as this was not taken into account when testing this code.

/********************************************************************/
/* INT32 x INT32 (fractional) that produces a INT64 result */
/********************************************************************/
static inline void D_mult_ll_p(
INT32 * v1,
INT32 * v2,
INT32 * HI,
UINT32 * LO
)
{
INT16 a, c;
UINT16 b, d;
INT32 temp_HI;
UINT32 temp_LO;
register UINT32 temp_A1;
register INT32 temp_A2;
register INT32 temp_A3;

a = extract_h(*v1);
b = extract_l(*v1);
c = extract_h(*v2);
d = extract_l(*v2);

/* L1*L2 */
temp_A1 = (UINT32)(UINT16)b * (UINT32)(UINT16)d;
/* H1*L2 + HI(A1) */
temp_A2 = (INT32) (INT16)a * (UINT32)(UINT16)d +
extract_h(temp_A1);
/* L1*H2 + A2 */
temp_A2 = (UINT32)(UINT16)b * (INT32) (INT16)c +
(INT32)temp_A2;
/* H1*H2 + HI(AC2) */
temp_A3 = (INT32) (INT16)a * (INT32) (INT16)c +
extract_h(temp_A2);

temp_LO = L_deposit_l(extract_l(temp_A1)) |
L_deposit_h(extract_l(temp_A2));
temp_HI = temp_A3;

/* Fractional conversion */
temp_HI = temp_HI << 1;
if(((UINT32)1<<31) & temp_LO) {
temp_HI = temp_HI | (1<<0);
}
*HI = temp_HI;
*LO = temp_LO << 1;
}

static inline void D_mult_ll(
INT32 v1,
INT32 v2,
INT32 * HI,
UINT32 * LO
)
{
D_mult_ll_p(&v1, &v2, HI, LO);
}

-Aaron

--- In c..., santhosh kumar wrote:
>
> Hai Ashish,
> 64 bit result directly is not possible on c55x as the max data type
available on c55x platform is 40bit(long long). But you can capture
it in two 32bit variables with some kind of work around. If you are
truncating/rounding 64bit to 32bit at some point in your code then
this method will work.
>
> Usually in algorithms at some point these 64bit variable are
truncated/rounded to 32bit. You have to do 32x32 in terms 16x16 and
should take care of carry during additions.
>
> It may take at least 12cycles to do mac of kind 64bit + = 32bit x
32bit.
>
> -Santhosh
>
>
>
>
>
> --- On Fri, 11/7/08, Ashish Gupta wrote:
> From: Ashish Gupta
> Subject: Re: [c55x] Re: 32 bit * 32 bit mutliplication in C
> To: c...
> Date: Friday, November 7, 2008, 9:59 PM
>
>
>
>
>
>
>
>
>
>
>
> Hi Aaron ,
> This is a fractional multiply and 64 bit result is required.
> Ashish
>
>
> On Thu, Nov 6, 2008 at 10:58 PM, wrote:
>
>
>
>
>
>
> We performing 32x32 or 32x16 multiplication two questions should be
answered:
>
> 1. Is this a fractional multiply or an integer multiply?
> 2. Is the 64 bit result required for the intended application?
>
> -Aaron
>
>
> Hi,
> >I am trying to do 32 bit * 32 bit multiplication on C55 x processor
in C . I
> >am unable to get the appropriate instruction or intrinsic for that.
Can u
> >help in this regard. Or we have to do this operation in assembly
only.
>
> >
> >Ashish
>