File: UtilInt.h

package info (click to toggle)
grcompiler 5.2.1-0.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,968 kB
  • sloc: cpp: 48,199; ansic: 7,670; sh: 4,427; makefile: 197; xml: 190; perl: 127; sed: 21
file content (170 lines) | stat: -rw-r--r-- 5,505 bytes parent folder | download | duplicates (8)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: UtilInt.h
Responsibility: Steve McConnel (was Shon Katzenberger)
Last reviewed:

	Integer utilities.
----------------------------------------------------------------------------------------------*/
#ifdef _MSC_VER
#pragma once
#endif
#ifndef UtilInt_H
#define UtilInt_H 1

#include "GrDebug.h"
namespace gr
{

const int knMax = 0x7FFFFFFF;


/***********************************************************************************************
	These arithmetic functions assert that the result doesn't overflow.
***********************************************************************************************/


/*----------------------------------------------------------------------------------------------
	Multiply two integers and assert on overflow.
----------------------------------------------------------------------------------------------*/
template<typename T>
	inline int Mul(T t1, T t2)
{
	Assert(!t1 || (t1 * t2) / t1 == t2);
	return t1 * t2;
}


/*----------------------------------------------------------------------------------------------
	Add two integers and assert on overflow.
----------------------------------------------------------------------------------------------*/
template<typename T>
	inline int Add(T t1, T t2)
{
	Assert((t1 + t2 < t2) == (t1 < 0));
	return t1 + t2;
}


/***********************************************************************************************
	Arithmetic functions.
***********************************************************************************************/


/*----------------------------------------------------------------------------------------------
	Return the floor(tNum / tDen) where floor(x) is defined as the the greatest integer
	that is less than or equal to the number. This only works for signed integer types.
----------------------------------------------------------------------------------------------*/
template<typename T>
	inline T FloorDiv(T tNum, T tDen)
{
	Assert(tDen != 0);
	return tNum / tDen - ((tNum ^ tDen) < 0 && (tNum % tDen));
}

/*----------------------------------------------------------------------------------------------
	Return the absolute value of the given integer.
----------------------------------------------------------------------------------------------*/
inline unsigned int Abs(int n)
{
	return n < 0 ? -n : n;
}

/***********************************************************************************************
	Hash functions.
***********************************************************************************************/
/*
unsigned int ComputeHashRgb(const byte * prgb, int cb, unsigned int uHash = 0);
unsigned int CaseSensitiveComputeHash(LPCOLESTR psz, unsigned int uHash = 0);
unsigned int CaseSensitiveComputeHashCch(const OLECHAR * prgch, int cch, unsigned int uHash = 0);
unsigned int CaseInsensitiveComputeHash(LPCOLESTR psz, unsigned int uHash = 0);
unsigned int CaseInsensitiveComputeHashCch(const OLECHAR * prgch, int cch, unsigned int uHash = 0);
*/

/***********************************************************************************************
	Getting primes.
***********************************************************************************************/

// Looks for a prime near u. The primes are gotten from a table in Util.cpp.
unsigned int GetPrimeNear(unsigned int u);

// Looks for a prime larger than u. If u is larger than the largest prime in the table, we
// just return that largest prime.
unsigned int GetLargerPrime(unsigned int u);

// Looks for a prime smaller than u. If u is smaller than the smallest prime in the table,
// we just return that smallest prime.
unsigned int GetSmallerPrime(unsigned int u);


/***********************************************************************************************
	Max and Min.
***********************************************************************************************/
template<typename T> T Max(T t1, T t2)
{
	return (t1 >= t2) ? t1 : t2;
}


template<typename T> T Min(T t1, T t2)
{
	return (t1 <= t2) ? t1 : t2;
}


inline int NMax(int n1, int n2)
{
	return (n1 >= n2) ? n1 : n2;
}


inline int NMin(int n1, int n2)
{
	return (n1 <= n2) ? n1 : n2;
}


/*----------------------------------------------------------------------------------------------
	If t < tMin, this returns tMin. Otherwise if t > tMax, it returns tMax. Otherwise it
	returns t.
----------------------------------------------------------------------------------------------*/
template<typename T> T Bound(T t, T tMin, T tMax)
{
	return t < tMin ? tMin : t > tMax ? tMax : t;
}


inline int NBound(int n, int nMin, int nMax)
{
	return n < nMin ? nMin : n > nMax ? nMax : n;
}


/*----------------------------------------------------------------------------------------------
	This returns true iff tMin <= t && t < tLim.
----------------------------------------------------------------------------------------------*/
template<typename T> bool InInterval(T t, T tMin, T tLim)
{
	return tMin <= t && t < tLim;
}


inline int SignedInt(wchar_t ch)
{
	if (ch & 0x00008000)
	{
		// Negative number.
		int nRet = (ch | 0xFFFF0000);
		return nRet;
	}
	else
		return (int)ch;
}

}// namespace gr

#endif // !UtilInt_H