more pcf50606 patches

Andrew Paulsen andrew.paulsen at gmail.com
Fri Jan 25 18:38:04 CET 2008


On Jan 25, 2008 11:06 AM, Andy Green <andy at openmoko.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Somebody in the thread at some point said:
>
> >> adc_adcin1 and adc_batvolt are both u_int16_t.  Multiplying them by a
> >> large number before implicitly casting them to a u_int32_t is what
> >> could cause the overflow.  That is why I made an explicit caste before
> >> the multiply.  Again, I changed it to a signed type.
> >
> > Wouldn't be the first time gcc surprised me but doesn't stuff get
> > promoted to int before any arithmetic happens?
>
> Yeah
>
> #include <stdio.h>
>
> int main(void)
> {
>         short s1 = 30000, s2 = 30000;
>         int n = s1 * s2;
>
>         printf("%d\n", n);
>         return 0;
> }
> $ gcc test.c -o test
> $ ./test
> 900000000
>

While compilers sometimes automatically promote data types, you really
should not count on it.  Try this:

#include <stdio.h>
#include <sys/types.h>

int main(void) {
  int32_t s1 = 1<<30;
  int32_t s2 = 8;

  int64_t n = s1 * s2;
  int64_t m = (int64_t)s1 * s2;


  printf("%x, %x, %llx, %llx\n", s1, s2, n, m);
  return 0;
}
$ gcc test.c -O0 -o test
$ ./test
40000000, 8, 0, 200000000


> - -Andy
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.7 (GNU/Linux)
> Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
>
> iD8DBQFHmhcUOjLpvpq7dMoRAnBfAJwMJtaBIox8K27PTAV2XNO6nf/HXgCgk3H8
> mTQbM17LTx1gerP3OXn4JcY=
> =LX4p
> -----END PGP SIGNATURE-----
>




More information about the openmoko-kernel mailing list