00001
00002 #include "support.h"
00003
00004
00051 int support::roundVal_( const double val )
00052 {
00053 int intPart = static_cast<int>(val);
00054 double fracPart = val - intPart;
00055 int threeDigits = fracPart * 1000.0;
00056 int fourDigits = fracPart * 10000.0;
00057 int forthDigit = fourDigits % 10;
00058 int thirdDigit = threeDigits % 10;
00059
00060 double roundVal = 0.001;
00061 if (forthDigit < 5) {
00062 roundVal = 0.0;
00063 }
00064 else if (forthDigit == 5) {
00065 if ((thirdDigit & 0x1) == 0) {
00066 roundVal = 0.0;
00067 }
00068 }
00069 double newVal = val + roundVal;
00070 double intRslt = newVal * 1000.0;
00071 return intRslt;
00072 }
00073
00074
00093 void support::roundToInt( int *intVec,
00094 const double *realVec,
00095 const size_t len )
00096 {
00097 if (intVec != 0 && realVec != 0) {
00098 for (size_t i = 0; i < len; i++) {
00099 intVec[i] = roundVal_( realVec[i] );
00100 }
00101 }
00102 }
00103
00104
00113 void support::decimalToInt( int *intVec,
00114 const double *realVec,
00115 const size_t len )
00116 {
00117 if (intVec != 0 && realVec != 0) {
00118 for (size_t i = 0; i < len; i++) {
00119 intVec[i] = (int)(realVec[i] * 100.0);
00120 }
00121 }
00122 }
00123
00124
00125
00126
00127 size_t support::nearestPower2Width_( size_t val )
00128 {
00129 size_t width = 0;
00130 if (val > 0) {
00131 width = 1;
00132 size_t power = 1;
00133 while (power < val && width < 32) {
00134 power = power << 1;
00135 width++;
00136 }
00137 }
00138
00139 return width;
00140 }
00141
00142
00150 size_t support::valWidth( const int val )
00151 {
00152 size_t wholeNum = (val < 0) ? -val : val;
00153 size_t width = 1 + nearestPower2Width_( wholeNum );
00154
00155 return width;
00156 }
00157
00158
00165 size_t support::UnsignedValWidth( const size_t val )
00166 {
00167 size_t width = nearestPower2Width_( val );
00168
00169 return width;
00170 }
00171
00172
00177 size_t support::vecWidth( const int *vec, const size_t N )
00178 {
00179 size_t totalWidth = 0;
00180 if (vec != 0) {
00181 for (size_t i = 0; i < N; i++) {
00182 totalWidth += valWidth( vec[i] );
00183 }
00184 }
00185 return totalWidth;
00186 }