int main() { //This algorithm zeroes the products i*j which means not prime //In conclusion bits took more time , but less space when compared to the char //representation //Array of bits std::array<int,N_bits> a_bits; for(std::size_t i = 2; i != N; i++) setBit(a_bits, i); auto start = std::chrono::steady_clock::now(); //array indexes are accessed using helper functions above. for(std::size_t i = 2; i != N; i++){ if(getBitValue(a_bits,i)){ for(std::size_t j = i; j * i < N; i++){ clearBit(a_bits, i * j); } } } auto end = std::chrono::steady_clock::now(); auto diff1 = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); //Array of chars std::array<char,N> a_char; for(std::size_t i = 2; i != N; i++) a_char[i] = '1'; start = std::chrono::steady_clock::now(); for(std::size_t i = 2; i != N; i++){ if(a_char[i] == '1'){ for(std::size_t j = i; j * i < N; i++){ a_char[i * j] = '0'; } } } end = std::chrono::steady_clock::now(); auto diff2 = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); for(std::size_t i = 2; i < N; i++){ if(getBitValue(a_bits,i)) std::cout << "bits :" << i << " "; if(a_char[i] == '1') std::cout << "chars : " << i << std::endl; } std::cout<< "Execution time for array of bits : " << diff1 << " microseconds" << std::endl; std::cout<< "Execution time for array of chars : " << diff2 << " microseconds" << std::endl; return 0; }
static string createDefineString( List< ShaderFeature >& features, uint32 bitMask ) { string defineString = ""; for (uint i = 0u; i < features.getCount(); ++i) { const ShaderFeature& feature = features[ i ]; const uint value = getBitValue( bitMask, feature.startBit, feature.bitCount ); defineString += formatString( "#define %s %u\n", feature.name.cStr(), value ); } return defineString; }
int32_t CBC_PDF417CodewordDecoder::getDecodedCodewordValue( CFX_Int32Array& moduleBitCount) { int32_t decodedValue = getBitValue(moduleBitCount); return CBC_PDF417Common::getCodeword(decodedValue) == -1 ? -1 : decodedValue; }
SArrayIndex::SArrayIndex(const char* seq, quint32 seqSize, quint32 _len, TaskStateInfo& ti, char unknownChar, const quint32* _bitTable, int _bitCharLen, int _gap, int _gapOffset) : w(_len), w4(_len/4), wRest(_len%4), skipGap(_gap), gapOffset(_gapOffset), bitTable(_bitTable), bitCharLen(_bitCharLen), l1Step(0), L1_SIZE(0), l1bitMask(NULL) { quint64 t1 = GTimer::currentTimeMicros(); seqLen = seqSize; arrLen = seqLen - w + 1; if (skipGap > 0) { arrLen = (arrLen / skipGap) + 1; } sArray = new quint32[arrLen]; if (bitTable!=NULL && bitCharLen>0 && bitCharLen<=5) { wCharsInMask = qMin(30 / bitCharLen, w);//30 to avoid +- overflow wAfterBits = qMax(0, w - wCharsInMask); if (wCharsInMask * bitCharLen == 32) { bitFilter = 0xFFFFFFFF; } else { bitFilter = (1<<(bitCharLen * wCharsInMask))-1; } } else { bitMask = NULL; bitFilter = wAfterBits = wCharsInMask = 0; } quint32* arunner = sArray; seqStart = seq; const char* seqEnd= seqStart+seqSize - w + 1; if (unknownChar == 0) { quint32 step = 1 + skipGap; for (const char* crunner = seqStart+gapOffset; crunner < seqEnd; arunner++, crunner+=step) { *arunner=seq2val(crunner); } } else { //filter suffixes with unknown char from result int oldLen = arrLen; const char* crunner = seqStart; int lastErrDist = 0; for (; crunner < seqEnd && lastErrDist < w - 1; crunner++) { if (*crunner != unknownChar) { lastErrDist++; continue; } lastErrDist = 0; } const char* cpos = crunner - w; int gapLeft = _gapOffset; quint32 w1 = w - 1; if (arrLen != 0) { while ( ++cpos < seqEnd ) { if (*(cpos + w1) != unknownChar) { lastErrDist++; if (lastErrDist >= w && gapLeft-- == 0) { *arunner = seq2val(cpos); arunner++; gapLeft = skipGap; } continue; } lastErrDist = 0; gapLeft = _gapOffset; } } arrLen = arunner - sArray; algoLog.trace(QString("filtered len %1, percent %2\n").arg(oldLen - arrLen).arg((arrLen/(float)(oldLen!=0?oldLen:1)))); } // here sArray is initialized with default values and is not sorted arrLen = arunner - sArray; if (bitTable != NULL) { //mask all prefixes in sArray with 32-bit values bitMask = new quint32[arrLen]; quint32 bitValue = 0; quint32* arunner = sArray; quint32* mrunner = bitMask; // Used for optimization - do not recompute whole bit mask if only 1 symbol changes // Note 1: at this moment arrays is not sorted and points to sequential regions // Note 2: expectedNext is not matched if some region was excluded from sarray quint32 expectedNext = 0; quint32 wCharsInMask1 = wCharsInMask - 1; for (quint32* end = mrunner + arrLen; mrunner < end; arunner++, mrunner++) { const char* seq = sarr2seq(arunner); if (*arunner == expectedNext && expectedNext != 0) { //pop first bit, push wCharsInMask1 char to the mask bitValue = ((bitValue << bitCharLen) | bitTable[uchar(*(seq + wCharsInMask1))]) & bitFilter; #ifdef _DEBUG // double check that optimization doesn't break anything quint32 bitValue2 = getBitValue(seq); assert(bitValue == bitValue2); #endif } else { //recompute the mask if we have some symbols skipped bitValue = getBitValue(seq); } expectedNext = seq2val(seq + 1); *mrunner = bitValue; } } if (ti.cancelFlag) { return; } //now sort sArray. Use bit-mask if available if (bitMask!=NULL) { sortBit(bitMask, 0, arrLen); //sortBitClassic(bitMask, 0, arrLen-1); //create L1 cache for bitMask if (arrLen < 200*1000) { L1_SIZE = arrLen; l1Step = 1; l1bitMask = bitMask; } else { L1_SIZE = 8192; l1bitMask = new quint32[L1_SIZE]; l1Step = arrLen / L1_SIZE; for (int i=0; i < L1_SIZE; i++) { l1bitMask[i] = bitMask[i*l1Step]; } l1bitMask[L1_SIZE-1] = bitMask[arrLen-1]; } } else { sort(sArray, 0, arrLen); } quint64 t2 = GTimer::currentTimeMicros(); perfLog.details(QString("SArray index creation time: %1").arg(double(t2-t1)/(1000*1000))); #ifdef _DEBUG debugCheck(unknownChar); #endif }
void ConvertMgr::convert (BIT8 *src, Value *pValue, int _iFuncID, int _iLen, int _iBitPos) { static int iAdjustLen; int iLen = 0; switch (_iFuncID) { case 0: // 不做转换, 内存直接赋值 //memcpy (pValue->m_sValue, src, _iLen); //pValue->m_sValue[_iLen]=0; iLen= _iLen>(sizeof(pValue->m_sValue)-1)?(sizeof(pValue->m_sValue)-1):_iLen; memcpy (pValue->m_sValue, src, iLen); pValue->m_sValue[iLen]=0; break; case 1: // 将 BIN 格式字段转换成为 ASC, 即数字转换为对应格式化字符串输出 iAdjustLen = MAX_STRING_VALUE_LEN-1 < _iLen ? MAX_STRING_VALUE_LEN-1 : _iLen ; binToAscii (src, pValue->m_sValue, iAdjustLen); break; case 2: // 将 BCD 格式转换成 long (BCD源格式不允许非数字bcd码,若有,则截断,如 123e56-->123) pValue->m_lValue = bcdToLong (src, _iLen); break; case 3: // 将 BIN 格式转换为 long pValue->m_lValue = binToLong (src, _iLen); if (_iBitPos>=1 && _iBitPos<=8 && _iLen==1) //## 需要取bit位的值 pValue->m_lValue = getBitValue ((unsigned char)pValue->m_lValue, _iBitPos); break; case 4: // 将 BCD 格式转换成 Ascii 码 (不允许非数字字符) iAdjustLen = (MAX_STRING_VALUE_LEN-1)/2 < _iLen ? (MAX_STRING_VALUE_LEN-1)/2 : _iLen ; bcdToAscii_Left_D (src, (BIT8 *)pValue->m_sValue, iAdjustLen); break; case 5: // 将 BCD 格式转换成 Ascii 码 (允许非数字字符A~F) iAdjustLen = (MAX_STRING_VALUE_LEN-1)/2 < _iLen ? (MAX_STRING_VALUE_LEN-1)/2 : _iLen ; bcdToAscii_Left_X (src, (BIT8 *)pValue->m_sValue, iAdjustLen); break; case 6: // Ascii码格式转换成Long pValue->m_lValue = asciiToLong (src, _iLen); break; case 7: // 将 BIN 格式转换为 long pValue->m_lValue = binToLong2 (src, _iLen); if (_iBitPos>=1 && _iBitPos<=8 && _iLen==1) //## 需要取bit位的值 pValue->m_lValue = getBitValue ((unsigned char)pValue->m_lValue, _iBitPos); break; case 8: // 将ASC码的 费用(单位元) 转换成数值型的费用(单位分) iAdjustLen = MAX_STRING_VALUE_LEN-1 < _iLen ? MAX_STRING_VALUE_LEN-1 : _iLen ; memcpy (pValue->m_sValue, src, iAdjustLen); pValue->m_sValue[iAdjustLen]=0; //pValue->m_lValue = (long)(atof(pValue->m_sValue)*100); strcat(pValue->m_sValue,"e+2"); pValue->m_lValue = (long)(atof(pValue->m_sValue)); break; case 9: longToString (src, (BIT8 *)pValue->m_sValue, _iLen); break; default : break; } }