core/vil/vil_na.cxx
Go to the documentation of this file.
00001 // This is core/vil/vil_na.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // This file is a cut-and-paste of vnl_na.cxx
00008 
00009 #include "vil_na.h"
00010 #include <vxl_config.h>
00011 #include <vcl_istream.h>
00012 #include <vcl_ios.h>
00013 
00014 //: A particular qNaN to indicate not available.
00015 // This returns the bit pattern 0x7ff00000000007a2, as used by Octave and R
00016 // Don't assume that any VXL functions will treat the value as NA rather than NaN, unless
00017 // explicitly documented.
00018 double vil_na(double)
00019 {
00020   double a;
00021 
00022 #if VXL_HAS_INT_64
00023   *reinterpret_cast<vxl_uint_64*>(&a) = 0x7ff00000000007a2LL;
00024 #else
00025 # if VXL_BIG_ENDIAN
00026 #  define hw 0
00027 #  define lw 1
00028 # else  // VXL_LITTLE_ENDIAN
00029 #  define hw 1
00030 #  define lw 0
00031 # endif
00032   reinterpret_cast<vxl_uint_32*>(&a)[hw]=0x7ff00000;
00033   reinterpret_cast<vxl_uint_32*>(&a)[lw]=0x000007a2;
00034 #endif
00035 
00036   return a;
00037 }
00038 
00039 
00040 
00041 //: A particular qNaN to indicate not available.
00042 // This returns the bit pattern 0x7f8007a2
00043 // Don't assume that any VXL functions will treat the value as NA rather than NaN, unless
00044 // explicitly documented.
00045 float vil_na(float)
00046 {
00047   float a;
00048 
00049   *reinterpret_cast<vxl_uint_32*>(&a) = 0x7f8007a2L;
00050 
00051   return a;
00052 }
00053 
00054 
00055 //: True if parameter is specific NA qNaN.
00056 // Tests for bit pattern 0x7ff00000000007a2, as used by Octave and R
00057 bool vil_na_isna(double x)
00058 {
00059 #if VXL_HAS_INT_64
00060   return ((*reinterpret_cast<vxl_uint_64*>(&x))&0xfff7ffffffffffffLL) // ignore signalling bit
00061     == 0x7ff00000000007a2LL;
00062 #else
00063   return ((reinterpret_cast<vxl_int_32*>(&x)[hw]) & 0xfff7ffff) == 0x7ff00000 &&
00064          reinterpret_cast<vxl_int_32*>(&x)[lw] == 0x000007a2;
00065 #endif
00066 }
00067 
00068 //: True if parameter is specific NA qNaN.
00069 // Tests for bit pattern 0x7F8007a2
00070 bool vil_na_isna(float x)
00071 {
00072   return ((*reinterpret_cast<vxl_uint_32*>(&x))&0xffbfffffL) // ignore signalling bit
00073     == 0x7f8007a2L;
00074 }
00075 //----------------------------------------------------------------------