3 MiscUtilities.h Miscellaneous Utilities
5 This module contains miscellaneous utility functions.
8 Copyright (C) 2004, Wong Chi Kwong.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #ifndef __MISC_UTILITIES_H__
27 #define __MISC_UTILITIES_H__
29 #include "TypeNLimit.h"
32 #define init(variable) variable = 0; // this is for avoiding compiler warning
33 // disable it if compiler becomes smarter!
35 #define truncateRight(value, offset) ( (value) >> (offset) << (offset) )
36 #define truncateLeft(value, offset) ( (value) << (offset) >> (offset) )
37 // alignBoundary must be power of 2
38 #define nextAlignedBoundary(offset, alignBoundary) ( ((offset) + (alignBoundary) - 1) & (- (alignBoundary)) )
39 #define lastAlignedBoundary(offset, alignBoundary) ( (offset) & (- (alignBoundary)) )
40 #define average(value1, value2) ( ((value1) & (value2)) + ((value1) ^ (value2)) / 2 )
41 #define min(value1, value2) ( ((value1) < (value2)) ? (value1) : (value2) )
42 #define max(value1, value2) ( ((value1) > (value2)) ? (value1) : (value2) )
43 #define med3(a, b, c) ( a<b ? (b<c ? b : a<c ? c : a) : (b>c ? b : a>c ? c : a))
44 #define med3Index(key, ia, ib, ic) ( key[ia]<key[ib] ? (key[ib]<key[ic] ? ib : key[ia]<key[ic] ? ic : ia) : (key[ib]>key[ic] ? ib : key[ia]>key[ic] ? ic : ia))
45 #define swap(a, b, t); t = a; a = b; b = t;
47 void Dust(const unsigned int len
, unsigned char *pattern
, const unsigned int level
, const unsigned int window
, const unsigned int word
);
49 void LimitCodeGenerateCodeTable(const unsigned int limit
, unsigned int** codeValue
, unsigned int** codeLength
);
51 int QSortUnsignedIntOrder(const void *data
, const int index1
, const int index2
);
52 void QSort(void* __restrict data
, const int numData
, const int dataWidth
, int (*QSortComp
)(const void*, const int, const int) );
54 unsigned int checkDuplicate(int *input
, const unsigned int numItem
, const int minValue
, const int maxValue
, char* text
);
55 unsigned int leadingZero(const unsigned int input
);
56 unsigned int ceilLog2(const unsigned int input
);
57 unsigned int floorLog2(const unsigned int input
);
58 unsigned int power(const unsigned int base
, const unsigned int power
);
59 void formatVALAsBinary(const unsigned int input
, char* output
, unsigned int bitGroup
);
60 unsigned int getRandomSeed();
62 void ConvertBytePackedDNAToWordPacked(const unsigned char *input
, unsigned int *output
, const unsigned int textLength
);
65 unsigned int reverseBit(unsigned int x
);
66 void initializeVAL(unsigned int *startAddr
, const unsigned int length
, const unsigned int initValue
);
67 void initializeCHAR(unsigned char *startAddr
, const unsigned int length
, const unsigned char initValue
);
68 unsigned int numberOfMatchInVAL(unsigned int *startAddr
, const unsigned int length
, const unsigned int searchValue
);
69 unsigned int numberOfMatchInCHAR(unsigned char *startAddr
, const unsigned int length
, const unsigned char searchValue
);
71 void bitCopyNoDestOffset(unsigned int *destinationAddress
, const unsigned int *sourceAddress
,
72 int sourceBitOffset
, int copyLengthInBit
);
73 void bitCopyNoDestBitOffset(unsigned int *destinationAddress
, int destinationWordOffset
,
74 const unsigned int *sourceAddress
, int sourceWordOffset
,
75 int sourceBitOffset
, int copyLengthInBit
);
76 unsigned int bitCopy(unsigned int *destinationAddress
, int destinationWordOffset
, int destinationBitOffset
,
77 const unsigned int *sourceAddress
, int sourceBitOffset
, int copyLengthInBit
);
79 unsigned int nextPrime(const unsigned int number
);
80 unsigned int popCount(const unsigned int bitVector
);