Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_904 / libraries / libjni-ecw / include / NCSMisc.h @ 43469

History | View | Annotate | Download (7.3 KB)

1 3538 nacho
/********************************************************
2
** Copyright 1999 Earth Resource Mapping Ltd.
3
** This document contains proprietary source code of
4
** Earth Resource Mapping Ltd, and can only be used under
5
** one of the three licenses as described in the
6
** license.txt file supplied with this distribution.
7
** See separate license.txt file for license details
8
** and conditions.
9
**
10
** This software is covered by US patent #6,442,298,
11
** #6,102,897 and #6,633,688.  Rights to use these patents
12
** is included in the license agreements.
13
**
14
** FILE:           NCSMisc.h
15
** CREATED:        Wed Oct 13 09:19:00 WST 1999
16
** AUTHOR:         David Hayward
17
** PURPOSE:        Miscellaneous prototypes of useful functions
18
**                        for the public SDKs.
19
**
20
**                        NOTE: Must be kept in sync with the private
21
**                                  includes.
22
**
23
** EDITS:
24
 *******************************************************/
25
26
#ifndef NCSMISC_H
27
#define NCSMISC_H
28
29
#include <math.h>
30
31
#ifdef __cplusplus
32
extern "C" {
33
#endif
34
35
#ifndef NCSDEFS_H
36
#include "NCSDefs.h"
37
#endif
38
39
#ifdef _WIN32_WCE
40
#define NCSMin MIN
41
#define NCSMax MAX
42
#elif defined POSIX
43
#define NCSMin(a, b)   ((a) > (b) ? (b) : (a))
44
#define NCSMax(a, b)   ((a) < (b) ? (b) : (a))
45
#else
46
#define NCSMin min
47
#define NCSMax max
48
#endif // _WIN32_WCE
49
50
51
typedef enum {
52
        NCS_UNKNOWN        = 0,        /* Don't know what this platform is         */
53
        NCS_WINDOWS_9X        = 1,        /* Windows 9x (95, 98)                        */
54
        NCS_WINDOWS_NT        = 2,        /* Windows NT (NT4, 2000)                */
55
        NCS_WINDOWS_CE        = 3,        /* Windows CE (CE, PocketPC)                */
56
        NCS_MACINTOSH        = 4,        /* Macintosh (Sys 8/9)                        */
57
        NCS_MACINTOSH_OSX= 5,        /* Macintosh OSX                        */
58
        NCS_LINUX        = 6,        /* Linux                                */
59
        NCS_PALM        = 7,        /* PalmOS (v2+)                                */
60
        NCS_SOLARIS        = 8,        /* Solaris 2.5+                                */
61
        NCS_HPUX        = 9,        /* HP-UX 11.0(64bit)                        */
62
} NCSPlatform;
63
64
void NCSFormatSizeText(INT64 nSizeBytes, char *buf);
65
NCSPlatform NCSGetPlatform(void);
66
67
/*
68
**        [06] Fast Float to UINT8 conversion logic
69
*/
70
#if (defined(WIN32) && defined(_X86_))
71
72
#define FLT_TO_INT_INIT() { unsigned int old_controlfp_val = _controlfp(_RC_NEAR, _MCW_RC)
73
#define FLT_TO_INT_INIT_FLR() { unsigned int old_controlfp_val = _controlfp(_RC_DOWN, _MCW_RC)
74
#define FLT_TO_INT_FINI() _controlfp(old_controlfp_val, _MCW_RC); }
75
76
#define FLT_TO_UINT8(a, b)                                                                \
77
        {                                                   \
78
                INT32 FLT_TO_INT_rval;                                                        \
79
                __asm                                           \
80
                {                                               \
81
                        __asm fld dword ptr [b]                     \
82
                        __asm fistp dword ptr [FLT_TO_INT_rval]     \
83
                }                                               \
84
                a = FLT_TO_INT_rval;                                                        \
85
        }
86
#define FLT_TO_INT32(a, b)                                                \
87
                __asm                                   \
88
                {                                       \
89
                        __asm fld dword ptr [b]             \
90
                        __asm fistp dword ptr [a]           \
91
                }
92
93
#else        /* WIN32 && _X86_ */
94
95
#define FLT_TO_INT_INIT() {
96
#define FLT_TO_INT_FINI()  }
97
98
#ifdef MACINTOSH        /**[16]**/
99
100
#define FLT_TO_INT32(a,b) a = rint(b)
101
//#define FLT_TO_UINT8(a, b) a = (UINT8) b
102
// rar (24-1-01): added from ECW mac port
103
#define FLT_TO_UINT8(a,b)                                                         \
104
        {                                                                                                 \
105
        UINT32        _x;                                                                                \
106
        FLT_TO_INT32(_x,b);                                                                \
107
        a = (UINT8)_x;                                                                        \
108
        }
109
110
#else        /* MACINTOSH */
111
112
#define FLT_TO_UINT8(a, b)                                                                \
113
           { a = (UINT8) b; }
114
#define FLT_TO_INT32(a,b)                                                                \
115
           { a = (INT32) b; }
116
117
#endif        /* MACINTOSH */
118
#endif        /* WIN32 && _X86_ */
119
120
#ifndef MACOSX
121
122
#define NCS_INLINE_FUNCS
123
#ifdef NCS_INLINE_FUNCS
124
125
#if (defined(WIN32) && defined(_X86_))
126
127
static NCS_INLINE INT32 NCSfloatToInt32_RM(IEEE4 f) {
128
        INT32 i32;
129
        FLT_TO_INT32(i32, f);
130
        return(i32);
131
}
132
133
static NCS_INLINE INT32 NCSdoubleToInt32_RM(IEEE8 x) {
134
        return(NCSfloatToInt32_RM((IEEE4)x));
135
}
136
137
// Convert a float between 0.0 and 1.0 to an INT32
138
static NCS_INLINE INT32 NCSfloatToInt32_0_1(IEEE4 x)
139
{
140
    IEEE4 y = x + 1.f;
141
    return((*(INT32 *)&y) & 0x7FFFFF);        // last 23 bits
142
}
143
144
// Convert a float to an INT32
145
static NCS_INLINE INT32 NCSfloatToInt32(IEEE4 x)
146
{
147
        INT32 FltInt = *(INT32 *)&x;
148
        INT32 MyInt;
149
        INT32 mantissa = (FltInt & 0x07fffff) | 0x800000;
150
        INT32 exponent = 150 - ((FltInt >> 23) & 0xff);
151
152
        if (exponent < -(8*(int)sizeof(mantissa)-1)) {
153
                MyInt = (mantissa << (8*(int)sizeof(mantissa)-1));
154
        } else if(exponent < 0) {
155
                MyInt = (mantissa << -exponent);
156
        } else if(exponent > (8*(int)sizeof(mantissa)-1)) {
157
                MyInt = (mantissa >> (8*(int)sizeof(mantissa)-1));
158
        } else {
159
                MyInt = (mantissa >> exponent);
160
        }
161
162
        if (FltInt & 0x80000000)
163
                MyInt = -MyInt;
164
        return(MyInt);
165
}
166
167
// Convert a double to an INT32
168
static NCS_INLINE INT32 NCSdoubleToInt32(IEEE8 x)
169
{
170
        INT64 DblInt = *(INT64 *)&x;
171
        INT32 MyInt;
172
        INT64 mantissa = (DblInt & 0xfffffffffffff) | 0x10000000000000;
173
        INT32 exponent = (INT32)(1075 - ((DblInt >> 52) & 0x7ff));
174
175
        if (exponent < -(8*(int)sizeof(mantissa)-1)) {
176
                MyInt = (INT32)(mantissa << (8*(int)sizeof(mantissa)-1));
177
        } else if(exponent < 0) {
178
                MyInt = (INT32)(mantissa << -exponent);
179
        } else if(exponent > (8*(int)sizeof(mantissa)-1)) {
180
                MyInt = (INT32)(mantissa >> (8*(int)sizeof(mantissa)-1));
181
        } else {
182
                MyInt = (INT32)(mantissa >> exponent);
183
        }
184
185
        if (DblInt & 0x8000000000000000)
186
                MyInt = -MyInt;
187
        return(MyInt);
188
}
189
190
#else // WIN32 & X86
191
192
#define NCSfloatToInt32_RM(f) ((INT32)(f))
193
#define NCSfloatToInt32_0_1(x) ((INT32)(x))
194
#define NCSfloatToInt32(x) ((INT32)(x))
195
#define NCSdoubleToInt32(x) ((INT32)(x))
196
#define NCSdoubleToInt32_RM(x) ((INT32)(x))
197
198
#endif // WIN32 && X86
199
200
static NCS_INLINE INT32 NCSCeil(double a)
201
{
202
        if(a >= 0.0) {
203
                INT32 v = NCSdoubleToInt32(a);
204
                return(v + ((a != v) ? 1 : 0));
205
        } else {
206
                return(NCSdoubleToInt32(a));
207
        }
208
}
209
210
static NCS_INLINE INT32 NCSFloor(double a)
211
{
212
        if(a >= 0.0) {
213
                return(NCSdoubleToInt32(a));
214
        } else {
215
                INT32 v = NCSdoubleToInt32(a);
216
                return(v - ((a != v) ? 1 : 0));
217
        }
218
}
219
220
static NCS_INLINE INT32 NCSCeilDiv(INT32 n, INT32 d)
221
{
222
        if(d == 0) {
223
                return(0x7fffffff);
224
        } else if(n >= 0 && d > 0) {
225
                return((n / d + ((n % d) ? 1 : 0)));
226
        } else {
227
                return(n / d);
228
        }
229
//        if(n < 0 || d < 0) {
230
//                return((INT32)ceil(n / (double)d));
231
//        } else {
232
//                return((n / d + ((n % d) ? 1 : 0)));
233
//        }
234
}
235
236
static NCS_INLINE INT32 NCSFloorDiv(INT32 n, INT32 d)
237
{
238
        switch(d) {
239
                case 1: return(n); break;
240
                case 2: return(n >> 1); break;
241
                case 4: return(n >> 2); break;
242
                default:
243
                                if(n < 0 || d < 0) {
244
                                        return((INT32)NCSFloor(n / (double)d));
245
                                } else {
246
                                        return(n / d);
247
                                }
248
                        break;
249
        }
250
}
251
252
static NCS_INLINE UINT32 NCS2Pow(UINT32 n)
253
{
254
//        return(pow(2, n));
255
        return(1 << n);
256
}
257
258
static NCS_INLINE IEEE8 NCS2PowS(INT32 n)
259
{
260
//        return(pow(2, n));
261
        if(n >= 0) {
262
                return((IEEE8)(1 << n));
263
        } else {
264
                return(1.0 / (1 << -n));
265
        }
266
}
267
268
static NCS_INLINE INT32 NCSLog2(INT32 n)
269
{
270
    INT32 nLog;
271
    for(nLog = 0; n > 1; nLog++) {
272
        n >>= 1;
273
    }
274
    return nLog;
275
}
276
277
static NCS_INLINE UINT64 NCSAbs(INT64 a)
278
{
279
//        return(abs(a));
280
        return((a < 0) ? -a : a);
281
}
282
283
static NCS_INLINE BOOLEAN NCSIsPow2(UINT32 nValue)
284
{
285
        if(NCS2Pow(NCSLog2(nValue)) == nValue) {
286
                return(TRUE);
287
        }
288
        return(FALSE);
289
}
290
291
#else
292
293
#ifdef __cplusplus
294
extern void *NCSNew(INT32 nSize, bool bClear = false);
295
extern void NCSDelete(void *p);
296
#endif // __cplusplus
297
298
extern INT32 NCSCeil(double a);
299
extern INT32 NCSFloor(double a);
300
extern INT32 NCSCeilDiv(INT32 n, INT32 d);
301
extern INT32 NCSFloorDiv(INT32 n, INT32 d);
302
extern UINT32 NCS2Pow(UINT32 n);
303
extern IEEE8 NCS2PowS(INT32 n);
304
extern UINT64 NCSAbs(INT64 a);
305
306
#endif /* NCS_INLINE_FUNCS */
307
#endif // !MACOSX
308
309
#ifdef __cplusplus
310
}
311
#endif
312
#endif /* NCSMISC_H */