Hi Dmitry
Does this modification of your algorithm still actually work? I just
tweeked your code by removing the 'first' variable and use the index j
to perform the same check, this should still make sure that the first
number is non-zero before filling with zeros, just have to make sure
that pgm_read_byte(letters+k)='0' when k=0 which is your algorithm I
don't think uses due to the  if (check > val) statement.

for (i = j = 0; i < max; i++) {
  unsigned long check = pgm_read_dword(d + i);
  k = 0;
  while(check <= __val) {
    __val -= check;
    k++;
  }
  if (k||j) __s[j++] = pgm_read_byte(letters + k);
}

if (!j) __s[j++] = '0';
__s[j] = 0;

return __s;

Depending on what i is the following variant may also help speed/size

where max=d+max;
for (i = d, j = 0; i < max; i++) {
  unsigned long check = pgm_read_dword(d);


I have compiled the two algorithms and the variant is quite a bit
smaller, I have not tested that it gives correct output.

Regards Brad

On 06/17/11 02:00, avr-libc-dev-requ...@nongnu.org wrote:
>         for (i = j = 0, first = 1; i < max; i++) {
>                 unsigned long check = pgm_read_dword(d + i);
>                 if (check > __val) {
>                         if (first)
>                                 continue;
>                         __s[j++] = '0';
>                         continue;
>                 }
>                 first = k = 0;
>                 while(check <= __val) {
>                         __val -= check;
>                         k++;
>                 }
>
>                 __s[j++] = pgm_read_byte(letters + k);
>         }
>
>         if (first)
>                 __s[j++] = '0';
>         __s[j] = 0;
>
>         return __s;


-- 
Bradley Jarvis

Pocket Innovations
Phone: (03) 5622-1773
Mobile: 0439 988-337



_______________________________________________
AVR-libc-dev mailing list
AVR-libc-dev@nongnu.org
https://lists.nongnu.org/mailman/listinfo/avr-libc-dev

Reply via email to