Browse Source

Deal explicitly with -32768 in atan2(). Fix #3.

Also perf tweak for random range.
jamesbowman 9 years ago
parent
commit
dfd1a20ed6
1 changed files with 8 additions and 1 deletions
  1. 8 1
      GD2.cpp

+ 8 - 1
GD2.cpp

@@ -178,7 +178,8 @@ uint16_t GDClass::random() {
 }
 
 uint16_t GDClass::random(uint16_t n) {
-  return GDClass::random() % n;
+  uint32_t p = random();
+  return (p * n) >> 16;
 }
 
 
@@ -219,6 +220,12 @@ uint16_t GDClass::atan2(int16_t y, int16_t x)
   uint16_t a;
   uint16_t xx = 0;
 
+  /* These values are tricky. So pretend they are not */
+  if (x == -32768)
+    x++;
+  if (y == -32768)
+    y++;
+
   if ((x <= 0) ^ (y > 0)) {
     int16_t t; t = x; x = y; y = t;
     xx ^= 0x4000;