460,018 Members | 1,292 Online Need help? Post your question and get tips & solutions from a community of 460,018 IT Pros & Developers. It's quick & easy.

# Fast Division/Modulo Operation

 P: n/a Hi, Is there a possibiliy to improve division or Modulo operations in the following, tmp1 = 123; tmp2 = 123; frame = ((char)((tmp1/100)+48)); // Division tmp1 = (tmp2 % 100); // Mod frame = ((char)((tmp1/10)+48)); tmp1 = (tmp2 % 10); tmp 1& 2 are unsigned int and frame is a char array. This is in a 16bit environment. I cant use any math libraries. Is there a possibility of shifting or subtration methods..? Thanks in advance. Densil Nov 14 '05 #1
8 Replies

 P: n/a silentlights wrote: Is there a possibiliy to improve division or Modulo operations in the following, tmp1 = 123; tmp2 = 123; frame = ((char)((tmp1/100)+48)); // Division tmp1 = (tmp2 % 100); // Mod frame = ((char)((tmp1/10)+48)); tmp1 = (tmp2 % 10); tmp 1& 2 are unsigned int and frame is a char array. This is in a 16bit environment. I cant use any math libraries. Is there a possibility of shifting or subtration methods..? Search google for an article I wrote in roughly the past month or two. The subject included "double dabble", and was either here or in comp.arch.embedded or comp.programming. -- A: Because it fouls the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Nov 14 '05 #2

 P: n/a Hi, For a beginer, your article http://www.talkaboutprogramming.com/...es/189873.html is not so clear to be implemted on this particular problem. Could you please explain me a bit more relevant to this question. Thanks Densil Nov 14 '05 #3

 P: n/a In article "silentlights" writes: Hi, Is there a possibiliy to improve division or Modulo operations in the following, tmp1 = 123; tmp2 = 123; frame = ((char)((tmp1/100)+48)); // Division tmp1 = (tmp2 % 100); // Mod frame = ((char)((tmp1/10)+48)); tmp1 = (tmp2 % 10); tmp 1& 2 are unsigned int and frame is a char array. tmp1 *= 41; f8 = (tmp1 >> 12) + 48; tmp1 = (tmp1 & 0x0fff) * 10; f9 = (tmp1 >> 12) + 48; tmp1 = ((tmp1 & 0x0fff) * 10) >> 12; works as long as tmp1 is smaller than 1000 initially. -- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131 home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/ Nov 14 '05 #4

 P: n/a In article "Dik T. Winter" writes: In article "silentlights" writes: > Hi, > Is there a possibiliy to improve division or Modulo operations in the > following, > > tmp1 = 123; > tmp2 = 123; > frame = ((char)((tmp1/100)+48)); // Division > tmp1 = (tmp2 % 100); // Mod > frame = ((char)((tmp1/10)+48)); > tmp1 = (tmp2 % 10); > > tmp 1& 2 are unsigned int and frame is a char array. tmp1 *= 41; f8 = (tmp1 >> 12) + 48; tmp1 = (tmp1 & 0x0fff) * 10; f9 = (tmp1 >> 12) + 48; tmp1 = ((tmp1 & 0x0fff) * 10) >> 12; Ok, if you also want to eliminate all multiplications: tmp1 += ((tmp1 << 2) + tmp1) << 3; f8 = (tmp1 >> 12) + 48; tmp1 &= 0x0fff; tmp1 += tmp1 << 2; f9 = (tmp1 >> 11) + 48; tmp1 &= 0x0fff; tmp1 += tmp1 << 2; tmp1 >>= 10; -- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131 home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/ Nov 14 '05 #5

 P: n/a silentlights wrote: Is there a possibility to improve division or Modulo operations in the following, tmp1 = 123; tmp2 = 123; frame = ((char)((tmp1/100)+48)); // Division tmp1 = (tmp2 % 100); // Mod frame = ((char)((tmp1/10)+48)); tmp1 = (tmp2 % 10); tmp 1& 2 are unsigned int and frame is a char array. This is in a 16bit environment. I can't use any math libraries. Is there a possibility of shifting or subtraction methods..? DIV(3) Linux Programmers Manual DIV(3) NAME div - computes the quotient and remainder of integer division SYNOPSIS #include div_t div(int numer, int denom); DESCRIPTION The div() function computes the value numer/denom and returns the quotient and remainder in a structure named div_t that contains two integer members named quot and rem. RETURN VALUE The div_t structure. CONFORMING TO SVID 3, BSD 4.3, ISO 9899 Nov 14 '05 #6 