Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
11 years ago

Adding two uint4

Hi, I'm struggling with adding int4 data. I want to be able to add two 96b numbers (what implies adding with carry) in my kernel and I came up with this code:


uint4 Add(uint4 a, uint4 b){
    ulong c = {0};
    
    c = (ulong)a+(ulong)b;
    
    for(int i=1; i<3; i++){
        c = (ulong)a+(ulong)b + (c>>32);
    }
    return (uint4)(c,c,c,0);
}

However, I noticed that typecasting significantly increases my resource usage. I'd appreciate if anyone could help me with finding a better solution.

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    In OpenCL C language you cannot dynamically index into vector types. That is a and b as used above is not allowed. You could do the following instead:

    uint4 Add(uint4 a, uint4 b){

    ulong4 c;

    c.s0 = (ulong) a.s0 + (ulong) b.s0;

    c.s1 = (ulong) a.s1 + (ulong) b.s1 + (c.s0 >> 32);

    c.s2 = (ulong) a.s2 + (ulong) b.s2 + (c.s1 >> 32);

    return (uint4)(c.s0, c.s1, c.s2, 0);

    }
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Wow, thank you so much! I didn't realize it before, as it worked (despite great resource usage) properly. I lowered my logic utilization tremendously thanks to your advice, thank you again!