1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| int bitcopy(void* to, unsigned int tOfs, int tCnt, const void* from, unsigned int fOfs, int fCnt) { int BitsOfHalfWord = sizeof(HALF_WORD) * 8; to = (void*)((intptr_t)to + (tOfs / BitsOfHalfWord) * sizeof(HALF_WORD)); from = (void*)((intptr_t)from + (fOfs / BitsOfHalfWord) * sizeof(HALF_WORD)); fOfs %= BitsOfHalfWord; tOfs %= BitsOfHalfWord;
int NbrOfCopiedBits = 0, bCnt = (fCnt < tCnt) ? fCnt : tCnt; WORD rMask, wMask; WORD temp, * _to = (WORD*)to, * _from = (WORD*)from; while (bCnt > 0) { int BitsToCopy = (bCnt < BitsOfHalfWord) ? bCnt : BitsOfHalfWord; bCnt -= BitsToCopy; NbrOfCopiedBits += BitsToCopy; rMask = (((WORD)-1) << fOfs) ^ (((WORD)-1) << (fOfs + BitsToCopy)); wMask = (((WORD)-1) << tOfs) ^ (((WORD)-1) << (tOfs + BitsToCopy)); showBits(&rMask, 32); showBits(&wMask, 32); temp = (*_from & rMask) >> fOfs << tOfs; *_to &= ~wMask; *_to |= temp; printf("%d bit(s) copied\n", BitsToCopy); if (BitsToCopy == BitsOfHalfWord) { _to = (WORD*)((intptr_t)_to + sizeof(HALF_WORD)); _from = (WORD*)((intptr_t)_from + sizeof(HALF_WORD)); } showBits(to, 64); } return NbrOfCopiedBits; }
|