Compilation warning on chprintf.c

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Compilation warning on chprintf.c

Post by Korken »

Hi,

I just noticed that I currently get an error when compiling chprintf.c:
Compiling main.c
../ChibiOS-RT/os/various/chprintf.c:77:24: warning: overflow in implicit constant conversion [-Woverflow]
100000000, 1000000000, 10000000000, 100000000000,
^
../ChibiOS-RT/os/various/chprintf.c:77:24: warning: overflow in implicit constant conversion [-Woverflow]
../ChibiOS-RT/os/various/chprintf.c:78:24: warning: overflow in implicit constant conversion [-Woverflow]
1000000000000, 10000000000000};

This is this line:

Code: Select all

static long pow10[] = {10, 100, 1000, 10000, 100000, 1000000, 10000000,
                       100000000, 1000000000, 10000000000, 100000000000,
                       1000000000000, 10000000000000};
Which also will give rise to errors in the ftoa() function.

Should it be defined as an 64-bit integer? As long will default to a 32-bit number.


Untested fix (no compilation errors):

Code: Select all

static int64_t pow10[] = {10, 100, 1000, 10000, 100000, 1000000, 10000000,
                          100000000, 1000000000, 10000000000, 100000000000,
                          1000000000000, 10000000000000};

static char *ftoa(char *p, double num, unsigned long precision) {
  int64_t l, temp;

  if (precision == 0)
    precision = FLOAT_PRECISION;
  temp = pow10[precision - 1];

  l = (int64_t)num;
  p = long_to_string_with_divisor(p, l, 10, 0);
  *p++ = '.';
  l = (int64_t)((num - l) * temp);
  return long_to_string_with_divisor(p, l, 10, temp / 10);
}


Best Regards
Emil
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Compilation warning on chprintf.c

Post by Giovanni »

I begin to think that adding float support to chprintf() has been an error. I don't want to bring in 64bits math so probably the best change is to limit the precision to what is achievable with 32bits.

Giovanni
colin
Posts: 149
Joined: Thu Dec 22, 2011 7:44 pm

Re: Compilation warning on chprintf.c

Post by colin »

Well floating point support is nice as an option that you can enable at compile-time at least via #ifdef. But it might make sense to disable it by default.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Compilation warning on chprintf.c

Post by Giovanni »

I fixed the issue by limiting the precision, not sure if the fix is the best possible however.

Giovanni
Post Reply