internal/debug.hh

00001 namespace TooN {
00002 
00003 namespace Internal
00004 {
00005 
00006     
00007 
00008     #if defined  TOON_CHECK_BOUNDS  || defined TOON_TEST_INTERNALS
00009         static inline void check_index(int s, int i)
00010         {
00011             if(i<0 || i >= s)
00012             {
00013                 #ifdef TOON_TEST_INTERNALS
00014                     throw Internal::BadIndex();
00015                 #else
00016                     std::cerr << "Toon index out of range" << std::endl;
00017                     std::abort();
00018                 #endif
00019             }
00020         }
00021     #else
00022         static inline void check_index(int, int){}
00023     #endif
00024 
00025     #if defined TOON_INITIALIZE_SNAN
00026         template<class P> static void debug_initialize(P* data, int n)
00027         {   
00028             using std::numeric_limits;
00029             for(int i=0; i < n; i++)
00030                 data[i] = numeric_limits<P>::signaling_NaN();
00031         }
00032     #elif defined TOON_INITIALIZE_QNAN
00033         template<class P> static void debug_initialize(P* data, int n)
00034         {   
00035             using std::numeric_limits;
00036             for(int i=0; i < n; i++)
00037                 data[i] = numeric_limits<P>::quiet_NaN();
00038         }
00039     #elif defined TOON_INITIALIZE_VAL
00040         template<class P> static void debug_initialize(P* data, int n)
00041         {   
00042             for(int i=0; i < n; i++)
00043                 data[i] = TOON_INITIALIZE_VAL;
00044         }
00045     #elif defined TOON_INITIALIZE_RANDOM
00046         union intbits
00047         {
00048             unsigned long i;
00049             char c[4];
00050         };
00051 
00052         template<class P> union datafail
00053         {
00054             int i;
00055             P p;
00056         };
00057 
00058 
00059         template<class P> static void debug_initialize(P* data, int n)
00060         {
00061             //Get a random seed. Precision limited to 1 second.
00062             static intbits random = { ((std::time(NULL) & 0xffffffff) *1664525L + 1013904223L)& 0xffffffff};
00063             unsigned char* cdata = reinterpret_cast<unsigned char*>(data);
00064 
00065             int bytes = sizeof(P)*n, i=0;
00066             
00067             //Do nothing except for noisy failure with non-POD types.
00068             datafail<P> d={0};
00069             bytes+=d.i;
00070             
00071             switch(bytes & 0x3 )
00072             {
00073                 for(i=0; i < bytes;)
00074                 {
00075                     //Really evil random number generator from NR.
00076                     //About 4x faster than Mersenne twister. Good quality
00077                     //is not needed, since it's only used to shake up the program
00078                     //state to test for uninitialized values. Also, it doesn't disturb
00079                     //the standard library random number generator.
00080                     
00081                     case 0:
00082                         cdata[i++] = random.c[0];   
00083                     case 3:
00084                         cdata[i++] = random.c[1];   
00085                     case 2:
00086                         cdata[i++] = random.c[2];   
00087                     case 1:
00088                         cdata[i++] = random.c[3];   
00089                     random.i = (1664525L * random.i + 1013904223L) & 0xffffffff;
00090                 }
00091             }
00092         }
00093     #else
00094         template<class P> static void debug_initialize(P*, int)
00095         {
00096         }
00097     #endif
00098     
00099 
00100 }
00101 
00102 }

Generated on Thu May 7 20:28:40 2009 for TooN by  doxygen 1.5.3