The Tiny HTM Library
include/tinyhtm/common.h
Go to the documentation of this file.
00001 
00007 #ifndef HTM_COMMON_H
00008 #define HTM_COMMON_H
00009 
00010 #include <stddef.h>
00011 #include <stdint.h>
00012 #include <math.h>
00013 
00014 #include "config.h"
00015 
00016 
00020 #if HAVE_ATTRIBUTE_UNUSED
00021 #   define HTM_UNUSED __attribute__ ((unused))
00022 #else
00023 #   define HTM_UNUSED
00024 #endif
00025 
00029 #if __STDC_VERSION__ >= 199901L || __GNUC__
00030 /* "inline" is a keyword */
00031 #   define HTM_INLINE static inline
00032 #else
00033 #   define HTM_INLINE static HTM_UNUSED
00034 #endif
00035 
00039 #if HAVE_ATTRIBUTE_ALIGNED
00040 #   define HTM_ALIGNED(x) __attribute__ ((aligned(x)))
00041 #else
00042 #   define HTM_ALIGNED(x)
00043 #endif
00044 
00050 #if __STDC_VERSION__ >= 199901L
00051 #   define HTM_ISNAN(x) isnan(x)
00052 #   define HTM_ISSPECIAL(x) (!isfinite(x))
00053 #else
00054 #   define HTM_ISNAN(x) ((x) != (x))
00055 #   define HTM_ISSPECIAL(x) ((x) != (x) || ((x) != 0.0 && (x) == 2*(x)))
00056 #endif
00057 
00058 /* M_PI may not be available in <math.h> */
00059 #ifndef M_PI
00060 #   define M_PI 3.141592653589793238462643
00061 #endif
00062 
00063 
00064 #ifdef __cplusplus
00065 extern "C" {
00066 #endif
00067 
00074 struct htm_range {
00075     int64_t min;  
00076     int64_t max;  
00077 };
00078 
00081 HTM_INLINE int htm_popcount(uint64_t x)
00082 {
00083     const uint64_t c1 = UINT64_C(0x5555555555555555); /* -1/3 */
00084     const uint64_t c2 = UINT64_C(0x3333333333333333); /* -1/5 */
00085     const uint64_t c4 = UINT64_C(0x0f0f0f0f0f0f0f0f); /* -1/17 */
00086     const uint64_t f  = UINT64_C(0x0101010101010101); /* -1/255 */
00087     x = x - ((x >> 1)  & c1);
00088     x = (x & c2) + ((x >> 2)  & c2);
00089     x = (x + (x >> 4)) & c4;
00090     return (int) ((x * f) >> 56);
00091 }
00092 
00095 enum htm_errcode {
00096     HTM_OK = 0,     
00097     HTM_ENOMEM,     
00098     HTM_ENULLPTR,   
00099     HTM_ENANINF,    
00100     HTM_EZERONORM,  
00101     HTM_ELAT,       
00102     HTM_EANG,       
00103     HTM_EHEMIS,     
00104     HTM_ELEN,       
00105     HTM_EDEGEN,     
00106     HTM_EID,        
00107     HTM_ELEVEL,     
00108     HTM_EIO,        
00109     HTM_EMMAN,      
00110     HTM_EINV,       
00111     HTM_ETREE,      
00112     HTM_NUM_CODES
00113 };
00114 
00115 
00119 const char * htm_errmsg(enum htm_errcode err);
00120 
00121 
00123 #define HTM_DEG_PER_RAD    57.2957795130823208767981548141
00124 
00126 #define HTM_RAD_PER_DEG    0.0174532925199432957692369076849
00127 
00129 #define HTM_ARCSEC_PER_DEG 3600.0
00130 
00131 
00134 HTM_INLINE double htm_angred(double angle_deg)
00135 {
00136     double angle = fmod(angle_deg, 360.0);
00137     if (angle < 0.0) {
00138         angle += 360.0;
00139         if (angle == 360.0) {
00140             angle = 0.0;
00141         }
00142     }
00143     return angle;
00144 }
00145 
00148 HTM_INLINE double htm_clamp(double x, double min, double max)
00149 {
00150     return (x < min) ? min : ((x > max) ? max : x);
00151 }
00152 
00156 #ifdef __cplusplus
00157 }
00158 #endif
00159 
00160 #endif /* HTM_COMMON_H */
00161