Home | History | Annotate | Download | only in using_double_on_avr
      1 #include "double_conversion.h"
      2 #include <math.h>
      3 #include <stdio.h>
      4 
      5 static const double testvalues[] = {
      6            0.0,        -0.0,         0.1,         -0.1,
      7           M_PI,       -M_PI,  123456.789,  -123456.789,
      8       INFINITY,   -INFINITY,         NAN, INFINITY - INFINITY,
      9           1e38,       -1e38,        1e39,        -1e39,
     10          1e-38,      -1e-38,       1e-39,       -1e-39,
     11    3.14159e-37,-3.14159e-37, 3.14159e-43, -3.14159e-43,
     12          1e-60,      -1e-60,       1e-45,       -1e-45,
     13     0.99999999999999, -0.99999999999999, 127.999999999999, -127.999999999999
     14 };
     15 
     16 #define TESTVALUES_COUNT (sizeof(testvalues)/sizeof(testvalues[0]))
     17 
     18 int main()
     19 {
     20     int status = 0;
     21     int i;
     22     for (i = 0; i < TESTVALUES_COUNT; i++)
     23     {
     24         double orig = testvalues[i];
     25         float expected_float = (float)orig;
     26         double expected_double = (double)expected_float;
     27 
     28         float got_float = double_to_float(*(uint64_t*)&orig);
     29         uint64_t got_double = float_to_double(got_float);
     30 
     31         uint32_t e1 = *(uint32_t*)&expected_float;
     32         uint32_t g1 = *(uint32_t*)&got_float;
     33         uint64_t e2 = *(uint64_t*)&expected_double;
     34         uint64_t g2 = got_double;
     35 
     36         if (g1 != e1)
     37         {
     38             printf("%3d double_to_float fail: %08x != %08x\n", i, g1, e1);
     39             status = 1;
     40         }
     41 
     42         if (g2 != e2)
     43         {
     44             printf("%3d float_to_double fail: %016llx != %016llx\n", i,
     45                 (unsigned long long)g2,
     46                 (unsigned long long)e2);
     47             status = 1;
     48         }
     49     }
     50 
     51     return status;
     52 }
     53 
     54 
     55 
     56 
     57