1d14abf1Robert Mustacchi/*******************************************************************************
2d14abf1Robert Mustacchi * CDDL HEADER START
3d14abf1Robert Mustacchi *
4d14abf1Robert Mustacchi * The contents of this file are subject to the terms of the
5d14abf1Robert Mustacchi * Common Development and Distribution License (the "License").
6d14abf1Robert Mustacchi * You may not use this file except in compliance with the License.
7d14abf1Robert Mustacchi *
8d14abf1Robert Mustacchi * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9d14abf1Robert Mustacchi * or http://www.opensolaris.org/os/licensing.
10d14abf1Robert Mustacchi * See the License for the specific language governing permissions
11d14abf1Robert Mustacchi * and limitations under the License.
12d14abf1Robert Mustacchi *
13d14abf1Robert Mustacchi * When distributing Covered Code, include this CDDL HEADER in each
14d14abf1Robert Mustacchi * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15d14abf1Robert Mustacchi * If applicable, add the following below this CDDL HEADER, with the
16d14abf1Robert Mustacchi * fields enclosed by brackets "[]" replaced with your own identifying
17d14abf1Robert Mustacchi * information: Portions Copyright [yyyy] [name of copyright owner]
18d14abf1Robert Mustacchi *
19d14abf1Robert Mustacchi * CDDL HEADER END
20d14abf1Robert Mustacchi *
21d14abf1Robert Mustacchi * Copyright 2014 QLogic Corporation
22d14abf1Robert Mustacchi * The contents of this file are subject to the terms of the
23d14abf1Robert Mustacchi * QLogic End User License (the "License").
24d14abf1Robert Mustacchi * You may not use this file except in compliance with the License.
25d14abf1Robert Mustacchi *
26d14abf1Robert Mustacchi * You can obtain a copy of the License at
27d14abf1Robert Mustacchi * http://www.qlogic.com/Resources/Documents/DriverDownloadHelp/
28d14abf1Robert Mustacchi * QLogic_End_User_Software_License.txt
29d14abf1Robert Mustacchi * See the License for the specific language governing permissions
30d14abf1Robert Mustacchi * and limitations under the License.
31d14abf1Robert Mustacchi *
32d14abf1Robert Mustacchi *
33d14abf1Robert Mustacchi * Module Description:
34d14abf1Robert Mustacchi *  This file should include pure ANSI C defines
35d14abf1Robert Mustacchi *
36d14abf1Robert Mustacchi * History:
37d14abf1Robert Mustacchi *    04/03/07 Alon Elhanani        Inception.
38d14abf1Robert Mustacchi ******************************************************************************/
39d14abf1Robert Mustacchi
40d14abf1Robert Mustacchi#ifndef __utils_h__
41d14abf1Robert Mustacchi#define __utils_h__
42d14abf1Robert Mustacchi
43d14abf1Robert Mustacchi#ifndef BITS_PER_BYTE
44d14abf1Robert Mustacchi#define BITS_PER_BYTE                        (8)
45d14abf1Robert Mustacchi#endif //BITS_PER_BYTE
46d14abf1Robert Mustacchi/*
47d14abf1Robert MustacchiXXX_FLAGS
48d14abf1Robert Mustacchibitwise flags operations, for readability of the code
49d14abf1Robert Mustacchi*/
50d14abf1Robert Mustacchi
51d14abf1Robert Mustacchi// get specific flags
52d14abf1Robert Mustacchi#define GET_FLAGS(flags,bits)                   ((flags) & (bits))
53d14abf1Robert Mustacchi#define GET_FLAGS_WITH_OFFSET(flags,bits,offset)    (((flags) & (bits)) >> (offset))
54d14abf1Robert Mustacchi// set specific flags
55d14abf1Robert Mustacchi#define SET_FLAGS(flags,bits)                   ((flags) |= (bits))
56d14abf1Robert Mustacchi// reset specific flags
57d14abf1Robert Mustacchi#define RESET_FLAGS(flags,bits)                 ((flags) &= ~(bits))
58d14abf1Robert Mustacchi// clear flags
59d14abf1Robert Mustacchi#define CLEAR_FLAGS(flags)                      ((flags)=0)
60d14abf1Robert Mustacchi
61d14abf1Robert Mustacchi// macros for a single bit
62d14abf1Robert Mustacchi#define SET_BIT( _bits, _val )   SET_FLAGS  ( _bits,   (0x1ULL << _val) )
63d14abf1Robert Mustacchi#define RESET_BIT( _bits, _val ) RESET_FLAGS( _bits,   (0x1ULL << _val) )
64d14abf1Robert Mustacchi#define GET_BIT( _bits, _val )   GET_FLAGS  ( _bits,   (0x1ULL << _val) )
65d14abf1Robert Mustacchi
66d14abf1Robert Mustacchi/**
67d14abf1Robert Mustacchi * \brief offset of byte next to specified struct member
68d14abf1Robert Mustacchi *
69d14abf1Robert Mustacchi * Find the size of the structure members, up-to and including
70d14abf1Robert Mustacchi * the specified meber (_m).
71d14abf1Robert Mustacchi *
72d14abf1Robert Mustacchi * \param _s            structure type
73d14abf1Robert Mustacchi * \param _m            struct member
74d14abf1Robert Mustacchi */
75d14abf1Robert Mustacchi
76d14abf1Robert Mustacchi#define LAST_BYTE_OF(_s,_m)   (OFFSETOF(_s,_m)+sizeof( ((_s *)0)->_m))
77d14abf1Robert Mustacchi
78d14abf1Robert Mustacchi/*
79d14abf1Robert MustacchiARRSIZE:
80d14abf1Robert Mustacchiused to calcualte item count of an array
81d14abf1Robert Mustacchithis macro is used to prevent compile warning for unreferenced parametes
82d14abf1Robert Mustacchi*/
83d14abf1Robert Mustacchi#ifndef ARRSIZE
84d14abf1Robert Mustacchi#define ARRSIZE(a)                   (sizeof(a)/sizeof((a)[0]))
85d14abf1Robert Mustacchi#endif // ARRSIZE
86d14abf1Robert Mustacchi
87d14abf1Robert Mustacchi/*
88d14abf1Robert MustacchiUNREFERENCED_PARAMETER
89d14abf1Robert Mustacchithis macro is used to prevent compile warning for unreferenced parametes
90d14abf1Robert Mustacchi*/
91d14abf1Robert Mustacchi#ifndef UNREFERENCED_PARAMETER_
92d14abf1Robert Mustacchi#define UNREFERENCED_PARAMETER_(P)\
93d14abf1Robert Mustacchi    /*lint -save -e527 -e530 */  \
94d14abf1Robert Mustacchi    { \
95d14abf1Robert Mustacchi        (P) = (P); \
96d14abf1Robert Mustacchi    }
97d14abf1Robert Mustacchi#endif // UNREFERENCED_PARAMETER_
98d14abf1Robert Mustacchi
99d14abf1Robert Mustacchi
100d14abf1Robert Mustacchi/*
101d14abf1Robert MustacchiASSERT_STATIC
102d14abf1Robert Mustacchithis macro is used to raise COMPILE time assertions
103d14abf1Robert Mustacchie.g: ASSERT_STATIC( sizeof(S08) == 1 )
104d14abf1Robert Mustacchirelevant errors that compilers gives in such case:
105d14abf1Robert Mustacchibuild.exe (MS)     - "error  C2196: case value '0' already used"
106d14abf1Robert MustacchiWMAKE.exe (Watcom) - "Error! E1039: Duplicate case value '0' found"
107d14abf1Robert Mustacchi*/
108d14abf1Robert Mustacchi#ifndef ASSERT_STATIC
109d14abf1Robert Mustacchi#define ASSERT_STATIC(cond) \
110d14abf1Robert Mustacchi    {   \
111d14abf1Robert Mustacchi        const unsigned char dummy_zero = 0 ; \
112d14abf1Robert Mustacchi        switch(dummy_zero){case 0:case (cond):;} \
113d14abf1Robert Mustacchi    }
114d14abf1Robert Mustacchi#ifdef __SUNPRO_C /* Sun's cc can't deal with this clever hack */
115d14abf1Robert Mustacchi#undef ASSERT_STATIC
116d14abf1Robert Mustacchi#define ASSERT_STATIC(cond)
117d14abf1Robert Mustacchi#endif
118d14abf1Robert Mustacchi#endif // ASSERT_STATIC(cond)
119d14abf1Robert Mustacchi
120d14abf1Robert Mustacchi/*
121d14abf1Robert MustacchiRANGE
122d14abf1Robert Mustacchithis macro is used to check that a certain variable is within a given range
123d14abf1Robert Mustacchie.g: RANGE_INCLUDE(a, 10, 100)    - is the following true : 10<=a<=100
124d14abf1Robert Mustacchi*/
125d14abf1Robert Mustacchi#define IN_RANGE(_var, _min, _max)  ( ((_var) >= (_min)) && ((_var) <= (_max)) )
126d14abf1Robert Mustacchi
127d14abf1Robert Mustacchi/*
128d14abf1Robert MustacchiIS_DIGIT
129d14abf1Robert Mustacchithis macro is used to check that a char is a digit
130d14abf1Robert Mustacchiexample IS_DIGIT('4') - is '4' a digit will return TRUE.
131d14abf1Robert Mustacchi*/
132d14abf1Robert Mustacchi#ifndef IS_DIGIT
133d14abf1Robert Mustacchi#define IS_DIGIT(c) ( ((c) >= '0') && ((c) <= '9') )
134d14abf1Robert Mustacchi#endif
135d14abf1Robert Mustacchi
136d14abf1Robert Mustacchi/*
137d14abf1Robert MustacchiDefine standard min and max macros
138d14abf1Robert Mustacchi*/
139d14abf1Robert Mustacchi#ifndef min
140d14abf1Robert Mustacchi#define min(a,b) (((a) < (b)) ? (a) : (b))
141d14abf1Robert Mustacchi#endif
142d14abf1Robert Mustacchi
143d14abf1Robert Mustacchi#if defined(__LINUX) || defined(USER_LINUX)
144d14abf1Robert Mustacchi#undef max
145d14abf1Robert Mustacchi#endif // LINUX
146d14abf1Robert Mustacchi
147d14abf1Robert Mustacchi#ifndef max
148d14abf1Robert Mustacchi#define max(a,b) (((a) > (b)) ? (a) : (b))
149d14abf1Robert Mustacchi#endif // !max
150d14abf1Robert Mustacchi
151d14abf1Robert Mustacchi#if defined(__LINUX) || defined(USER_LINUX)
152d14abf1Robert Mustacchi#undef DIV_ROUND_UP_BITS
153d14abf1Robert Mustacchi#endif // LINUX
154d14abf1Robert Mustacchi// Round up a divied operation more optimal way base on bits.
155d14abf1Robert Mustacchi// It is the same opration as d == (1<<bits).
156d14abf1Robert Mustacchi// DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
157d14abf1Robert Mustacchi#ifndef BITS_ROUND_UP
158d14abf1Robert Mustacchi#define DIV_ROUND_UP_BITS(n,bits) (((n) + (1 << (bits)) - 1) >> (bits))
159d14abf1Robert Mustacchi#endif
160d14abf1Robert Mustacchi/*
161d14abf1Robert MustacchiDefine for pragma message output with file name and line number
162d14abf1Robert Mustacchiusage: #pragma message (MSGSTR("blah blah blah"))
163d14abf1Robert Mustacchi*/
164d14abf1Robert Mustacchi#define _STR(x) #x
165d14abf1Robert Mustacchi#define _STR2(x) _STR(x)
166d14abf1Robert Mustacchi#define MSGSTR(msg) __FILE__ "(" _STR2(__LINE__)"): message - " ##msg
167d14abf1Robert Mustacchi
168d14abf1Robert Mustacchi
169d14abf1Robert Mustacchi#ifndef POWER_OF_2
170d14abf1Robert Mustacchi// known algorithm
171d14abf1Robert Mustacchi#define POWER_OF_2(x)       ((0 != x) && (0 == (x &(x-1))))
172d14abf1Robert Mustacchi#endif // !POWER_OF_2
173d14abf1Robert Mustacchi
174d14abf1Robert Mustacchi
175d14abf1Robert Mustacchi
176d14abf1Robert Mustacchi#ifndef FAST_PATH_MODULO
177d14abf1Robert Mustacchi// a = b (mod n)
178d14abf1Robert Mustacchi// If a==b the compiler will omit the last line.
179d14abf1Robert Mustacchi#define FAST_PATH_MODULO(a,b,n)     \
180d14abf1Robert Mustacchi    do                              \
181d14abf1Robert Mustacchi    {                               \
182d14abf1Robert Mustacchi        while ((b) > ((n) -1))      \
183d14abf1Robert Mustacchi            (b) = (b) - (n);        \
184d14abf1Robert Mustacchi        (a)=(b);                    \
185d14abf1Robert Mustacchi    }                               \
186d14abf1Robert Mustacchi    while(0)
187d14abf1Robert Mustacchi#endif // !FAST_PATH_MODULO
188d14abf1Robert Mustacchi
189d14abf1Robert Mustacchi#ifndef MINCHAR
190d14abf1Robert Mustacchi#define MINCHAR     0x80
191d14abf1Robert Mustacchi#endif
192d14abf1Robert Mustacchi#ifndef MAXCHAR
193d14abf1Robert Mustacchi#define MAXCHAR     0x7f
194d14abf1Robert Mustacchi#endif
195d14abf1Robert Mustacchi#ifndef MINSHORT
196d14abf1Robert Mustacchi#define MINSHORT    0x8000
197d14abf1Robert Mustacchi#endif
198d14abf1Robert Mustacchi#ifndef MAXSHORT
199d14abf1Robert Mustacchi#define MAXSHORT    0x7fff
200d14abf1Robert Mustacchi#endif
201d14abf1Robert Mustacchi#ifndef MINLONG
202d14abf1Robert Mustacchi#define MINLONG     0x80000000
203d14abf1Robert Mustacchi#endif
204d14abf1Robert Mustacchi#ifndef MAXLONG
205d14abf1Robert Mustacchi#define MAXLONG     0x7fffffff
206d14abf1Robert Mustacchi#endif
207d14abf1Robert Mustacchi#ifndef MAXBYTE
208d14abf1Robert Mustacchi#define MAXBYTE     0xff
209d14abf1Robert Mustacchi#endif
210d14abf1Robert Mustacchi#ifndef MAXWORD
211d14abf1Robert Mustacchi#define MAXWORD     0xffff
212d14abf1Robert Mustacchi#endif
213d14abf1Robert Mustacchi#ifndef MAXDWORD
214d14abf1Robert Mustacchi#define MAXDWORD    0xffffffff
215d14abf1Robert Mustacchi#endif
216d14abf1Robert Mustacchi
217d14abf1Robert Mustacchi#define CLEAR_MSB32(_val32) (_val32 & MAXLONG)
218d14abf1Robert Mustacchi
219d14abf1Robert Mustacchi// Calculate the size of a field in a structure of type type, without
220d14abf1Robert Mustacchi// knowing or stating the type of the field.
221d14abf1Robert Mustacchi// based on NTDDK RTL_FIELD_SIZE
222d14abf1Robert Mustacchi#ifndef FIELD_SIZE
223d14abf1Robert Mustacchi#define FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
224d14abf1Robert Mustacchi#endif
225d14abf1Robert Mustacchi
226d14abf1Robert Mustacchi#endif /* __utils_h__ */
227