Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)

Adding two uint4

Altera_Forum
Honored Contributor II
1,384 Views

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.
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
702 Views

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); 

}
0 Kudos
Altera_Forum
Honored Contributor II
702 Views

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!

0 Kudos
Reply