Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-ecwcompress / include / NCSMisc.h @ 1937

History | View | Annotate | Download (6.8 KB)

1
/********************************************************
2
** Copyright 1999 Earth Resource Mapping Pty Ltd.
3
** This document contains unpublished source code of
4
** Earth Resource Mapping Pty Ltd. This notice does
5
** not indicate any intention to publish the source
6
** code contained herein.
7
**
8
** FILE:           NCSMisc.h
9
** CREATED:        Wed Oct 13 09:19:00 WST 1999
10
** AUTHOR:         David Hayward
11
** PURPOSE:        Miscellaneous prototypes of useful functions
12
**                        for the public SDKs.
13
**
14
**                        NOTE: Must be kept in sync with the private
15
**                                  includes.
16
**
17
** EDITS:
18
 *******************************************************/
19

    
20
#ifndef NCSMISC_H
21
#define NCSMISC_H
22

    
23
#include <math.h>
24
#ifdef __cplusplus
25
extern "C" {
26
#endif
27

    
28
#ifndef NCSDEFS_H
29
#include "NCSDefs.h"
30
#endif
31

    
32
#ifdef _WIN32_WCE
33
#define NCSMin MIN
34
#define NCSMax MAX
35
#else
36
#define NCSMin min
37
#define NCSMax max
38
#endif // _WIN32_WCE
39

    
40

    
41
typedef enum {
42
        NCS_UNKNOWN        = 0,        /* Don't know what this platform is         */
43
        NCS_WINDOWS_9X        = 1,        /* Windows 9x (95, 98)                        */
44
        NCS_WINDOWS_NT        = 2,        /* Windows NT (NT4, 2000)                */
45
        NCS_WINDOWS_CE        = 3,        /* Windows CE (CE, PocketPC)                */
46
        NCS_MACINTOSH        = 4,        /* Macintosh (Sys 8/9)                        */
47
        NCS_MACINTOSH_OSX= 5,        /* Macintosh OSX                        */
48
        NCS_LINUX        = 6,        /* Linux                                */
49
        NCS_PALM        = 7,        /* PalmOS (v2+)                                */
50
        NCS_SOLARIS        = 8,        /* Solaris 2.5+                                */
51
        NCS_HPUX        = 9,        /* HP-UX 11.0(64bit)                        */
52
} NCSPlatform;
53

    
54
void NCSFormatSizeText(INT64 nSizeBytes, char *buf);
55
NCSPlatform NCSGetPlatform(void);
56

    
57
/*
58
**        [06] Fast Float to UINT8 conversion logic
59
*/
60
#if (defined(WIN32) && defined(_X86_))
61

    
62
#define FLT_TO_INT_INIT() { unsigned int old_controlfp_val = _controlfp(_RC_NEAR, _MCW_RC)
63
#define FLT_TO_INT_INIT_FLR() { unsigned int old_controlfp_val = _controlfp(_RC_DOWN, _MCW_RC)
64
#define FLT_TO_INT_FINI() _controlfp(old_controlfp_val, _MCW_RC); }
65

    
66
#define FLT_TO_UINT8(a, b)                                                                \
67
        {                                                   \
68
                INT32 FLT_TO_INT_rval;                                                        \
69
                __asm                                           \
70
                {                                               \
71
                        __asm fld dword ptr [b]                     \
72
                        __asm fistp dword ptr [FLT_TO_INT_rval]     \
73
                }                                               \
74
                a = FLT_TO_INT_rval;                                                        \
75
        }
76
#define FLT_TO_INT32(a, b)                                                \
77
                __asm                                   \
78
                {                                       \
79
                        __asm fld dword ptr [b]             \
80
                        __asm fistp dword ptr [a]           \
81
                }
82

    
83
#else        /* WIN32 && _X86_ */
84

    
85
#define FLT_TO_INT_INIT() { 
86
#define FLT_TO_INT_FINI()  }
87

    
88
#ifdef MACINTOSH        /**[16]**/
89

    
90
#define FLT_TO_INT32(a,b) a = rint(b)
91
//#define FLT_TO_UINT8(a, b) a = (UINT8) b
92
// rar (24-1-01): added from ECW mac port
93
#define FLT_TO_UINT8(a,b)                                                         \
94
        {                                                                                                 \
95
        UINT32        _x;                                                                                \
96
        FLT_TO_INT32(_x,b);                                                                \
97
        a = (UINT8)_x;                                                                        \
98
        }
99
                                   
100
#else        /* MACINTOSH */
101

    
102
#define FLT_TO_UINT8(a, b)                                                                \
103
           { a = (UINT8) b; }
104
#define FLT_TO_INT32(a,b)                                                                \
105
           { a = (INT32) b; }
106

    
107
#endif        /* MACINTOSH */
108
#endif        /* WIN32 && _X86_ */
109

    
110
#ifndef MACOSX
111

    
112
#define NCS_INLINE_FUNCS
113
#ifdef NCS_INLINE_FUNCS
114

    
115
#if (defined(WIN32) && defined(_X86_))
116

    
117
static NCS_INLINE INT32 NCSfloatToInt32_RM(IEEE4 f) {
118
        INT32 i32;
119
        FLT_TO_INT32(i32, f);
120
        return(i32);
121
}
122

    
123
// Convert a float between 0.0 and 1.0 to an INT32
124
static NCS_INLINE INT32 NCSfloatToInt32_0_1(IEEE4 x)
125
{
126
    IEEE4 y = x + 1.f;
127
    return((*(INT32 *)&y) & 0x7FFFFF);        // last 23 bits
128
}
129

    
130
// Convert a float to an INT32
131
static NCS_INLINE INT32 NCSfloatToInt32(IEEE4 x)
132
{
133
        INT32 FltInt = *(INT32 *)&x;
134
        INT32 MyInt;
135
        INT32 mantissa = (FltInt & 0x07fffff) | 0x800000;
136
        INT32 exponent = 150 - ((FltInt >> 23) & 0xff);
137

    
138
        if (exponent < -(8*(int)sizeof(mantissa)-1)) {
139
                MyInt = (mantissa << (8*(int)sizeof(mantissa)-1));                      
140
        } else if(exponent < 0) {
141
                MyInt = (mantissa << -exponent);                      
142
        } else if(exponent > (8*(int)sizeof(mantissa)-1)) {
143
                MyInt = (mantissa >> (8*(int)sizeof(mantissa)-1));
144
        } else {
145
                MyInt = (mantissa >> exponent);
146
        }
147

    
148
        if (FltInt & 0x80000000)
149
                MyInt = -MyInt;
150
        return(MyInt);
151
}
152

    
153
// Convert a double to an INT32
154
static NCS_INLINE INT32 NCSdoubleToInt32(IEEE8 x)
155
{
156
        INT64 DblInt = *(INT64 *)&x;
157
        INT32 MyInt;
158
        INT64 mantissa = (DblInt & 0xfffffffffffff) | 0x10000000000000;
159
        INT32 exponent = (INT32)(1075 - ((DblInt >> 52) & 0x7ff));
160

    
161
        if (exponent < -(8*(int)sizeof(mantissa)-1)) {
162
                MyInt = (INT32)(mantissa << (8*(int)sizeof(mantissa)-1));                      
163
        } else if(exponent < 0) {
164
                MyInt = (INT32)(mantissa << -exponent);                      
165
        } else if(exponent > (8*(int)sizeof(mantissa)-1)) {
166
                MyInt = (INT32)(mantissa >> (8*(int)sizeof(mantissa)-1));
167
        } else {
168
                MyInt = (INT32)(mantissa >> exponent);
169
        }
170

    
171
        if (DblInt & 0x8000000000000000)
172
                MyInt = -MyInt;
173
        return(MyInt);
174
}
175

    
176
#else // WIN32 & X86
177

    
178
#define NCSfloatToInt32_RM(f) ((INT32)(f))
179
#define NCSfloatToInt32_0_1(x) ((INT32)(x))
180
#define NCSfloatToInt32(x) ((INT32)(x))
181
#define NCSdoubleToInt32(x) ((INT32)(x))
182

    
183
#endif // WIN32 && X86
184

    
185
static NCS_INLINE INT32 NCSCeil(double a)
186
{
187
        if(a >= 0.0) {
188
                INT32 v = NCSdoubleToInt32(a);
189
                return(v + ((a != v) ? 1 : 0));
190
        } else {
191
                return(NCSdoubleToInt32(a));
192
        }
193
}
194

    
195
static NCS_INLINE INT32 NCSFloor(double a)
196
{
197
        if(a >= 0.0) {
198
                return(NCSdoubleToInt32(a));
199
        } else {
200
                INT32 v = NCSdoubleToInt32(a);
201
                return(v - ((a != v) ? 1 : 0));                
202
        }
203
}
204

    
205
static NCS_INLINE INT32 NCSCeilDiv(INT32 n, INT32 d)
206
{
207
        if(d == 0) {
208
                return(0x7fffffff);
209
        } else if(n >= 0 && d > 0) {
210
                return((n / d + ((n % d) ? 1 : 0)));
211
        } else {
212
                return(n / d);
213
        }
214
//        if(n < 0 || d < 0) {
215
//                return((INT32)ceil(n / (double)d));
216
//        } else {
217
//                return((n / d + ((n % d) ? 1 : 0)));
218
//        }
219
}
220

    
221
static NCS_INLINE INT32 NCSFloorDiv(INT32 n, INT32 d)
222
{
223
        switch(d) {
224
                case 1: return(n); break;
225
                case 2: return(n >> 1); break;
226
                case 4: return(n >> 2); break;
227
                default:
228
                                if(n < 0 || d < 0) {
229
                                        return((INT32)NCSFloor(n / (double)d));
230
                                } else {
231
                                        return(n / d);
232
                                }
233
                        break;
234
        }
235
}
236

    
237
static NCS_INLINE UINT32 NCS2Pow(UINT32 n)
238
{
239
//        return(pow(2, n));
240
        return(1 << n);
241
}
242

    
243
static NCS_INLINE IEEE8 NCS2PowS(INT32 n)
244
{
245
//        return(pow(2, n));
246
        if(n >= 0) {
247
                return((IEEE8)(1 << n));
248
        } else {
249
                return(1.0 / (1 << -n));
250
        }
251
}
252

    
253
static NCS_INLINE INT32 NCSLog2(INT32 n)
254
{
255
    INT32 nLog;
256
    for(nLog = 0; n > 1; nLog++) {
257
        n >>= 1;
258
    }
259
    return nLog;
260
}
261

    
262
static NCS_INLINE UINT64 NCSAbs(INT64 a)
263
{
264
//        return(abs(a));
265
        return((a < 0) ? -a : a);
266
}
267

    
268
static NCS_INLINE BOOLEAN NCSIsPow2(UINT32 nValue) 
269
{
270
        if(NCS2Pow(NCSLog2(nValue)) == nValue) {
271
                return(TRUE);
272
        }
273
        return(FALSE);
274
}
275

    
276
#else
277

    
278
#ifdef __cplusplus
279
extern void *NCSNew(INT32 nSize, bool bClear = false);
280
extern void NCSDelete(void *p);
281
#endif // __cplusplus
282

    
283
extern INT32 NCSCeil(double a);
284
extern INT32 NCSFloor(double a);
285
extern INT32 NCSCeilDiv(INT32 n, INT32 d);
286
extern INT32 NCSFloorDiv(INT32 n, INT32 d);
287
extern UINT32 NCS2Pow(UINT32 n);
288
extern IEEE8 NCS2PowS(INT32 n);
289
extern UINT64 NCSAbs(INT64 a);
290

    
291
#endif /* NCS_INLINE_FUNCS */
292
#endif // !MACOSX
293

    
294
#ifdef __cplusplus
295
}
296
#endif
297
#endif /* NCSMISC_H */