cgribexlib.c 293 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1

Uwe Schulzweida's avatar
Uwe Schulzweida committed
2
/* Automatically generated by m214003 at 2014-03-28, do not edit */
3

Uwe Schulzweida's avatar
Uwe Schulzweida committed
4
/* CGRIBEXLIB_VERSION="1.6.4" */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
5

Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
#ifdef _ARCH_PWR6
#pragma options nostrict
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
10
11
12
13
14
15
16
17
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <sys/types.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
#include <inttypes.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
19

20

Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
#include "file.h"
#include "dmemory.h"
#include "dtypes.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24
#include "calendar.h"
25
#include "timebase.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
27
28
29

#ifndef _GRIB_INT_H
#define _GRIB_INT_H

30
31
32
33
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>


Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
41
#if ! defined   (_CGRIBEX_H)
#  include "cgribex.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#endif
#if ! defined   (_ERROR_H)
#  include "error.h"
#endif
#if ! defined   (_DTYPES_H)
#  include "dtypes.h"
#endif

#if ! defined   (FALSE)
#  define  FALSE  0
#endif

#if ! defined   (TRUE)
#  define  TRUE  1
#endif

#if ! defined   (UCHAR)
#  define  UCHAR  unsigned char
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
62

Uwe Schulzweida's avatar
Uwe Schulzweida committed
63
#if defined (CRAY) || defined (SX) || defined (__uxpch__)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
65
66
#  define VECTORCODE
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
67

Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
#if defined (VECTORCODE)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
#if  defined  (INT32)
70
#  define  GRIBPACK     unsigned INT32
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
72
73
#  define  PACK_GRIB    packInt32
#  define  UNPACK_GRIB  unpackInt32
#else
74
#  define  GRIBPACK     unsigned INT64
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
77
#  define  PACK_GRIB    packInt64
#  define  UNPACK_GRIB  unpackInt64
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
80
#else
#  define  GRIBPACK     unsigned char
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81

Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
83
#define  U_BYTEORDER     static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1}
#define  IS_BIGENDIAN()  (u_byteorder.c[sizeof(long) - 1])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84

Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
#if defined (__xlC__) /* performance problems on IBM */
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
86
87
88
89
#ifndef DBL_IS_NAN
#  define DBL_IS_NAN(x)     ((x) != (x))
#endif
#else
90
#ifndef DBL_IS_NAN
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
#if  defined  (HAVE_DECL_ISNAN)
92
#  define DBL_IS_NAN(x)     (isnan(x))
93
#elif  defined  (FP_NAN)
94
95
96
97
98
#  define DBL_IS_NAN(x)     (fpclassify(x) == FP_NAN)
#else
#  define DBL_IS_NAN(x)     ((x) != (x))
#endif
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
#endif
100

101
102
103
104
105
#ifndef DBL_IS_EQUAL
/*#define DBL_IS_EQUAL(x,y) (!(x < y || y < x)) */
#  define DBL_IS_EQUAL(x,y) (DBL_IS_NAN(x)||DBL_IS_NAN(y)?(DBL_IS_NAN(x)&&DBL_IS_NAN(y)?1:0):!(x < y || y < x))
#endif

106
107
108
#ifndef IS_EQUAL
#  define IS_NOT_EQUAL(x,y) (x < y || y < x)
#  define IS_EQUAL(x,y)     (!IS_NOT_EQUAL(x,y))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
111
#endif


112
113
114
#define  JP23SET    0x7FFFFF  /* 2**23 - 1 (---> 8388607)  */

#define  POW_2_M24  0.000000059604644775390625  /*  pow(2.0, -24.0) */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
117

double intpow2(int x);

118
119
120
int gribrec_len(int b1, int b2, int b3);
int correct_bdslen(int bdslen, long recsize, long gribpos);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* CDI converter routines */

/* param format:  DDDCCCNNN */

void    cdiDecodeParam(int param, int *dis, int *cat, int *num);
int     cdiEncodeParam(int dis, int cat, int num);

/* date format:  YYYYMMDD */
/* time format:  hhmmss   */

void    cdiDecodeDate(int date, int *year, int *month, int *day);
int     cdiEncodeDate(int year, int month, int day);

void    cdiDecodeTime(int time, int *hour, int *minute, int *second);
int     cdiEncodeTime(int hour, int minute, int second);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
140
141
142
143
144
/* CALENDAR types */

#define  CALENDAR_STANDARD        0  /* don't change this value (used also in cgribexlib)! */
#define  CALENDAR_PROLEPTIC       1
#define  CALENDAR_360DAYS         2
#define  CALENDAR_365DAYS         3
#define  CALENDAR_366DAYS         4
#define  CALENDAR_NONE            5
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
147

extern FILE *grprsm;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
extern int  CGRIBEX_Debug;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
151
152
153
154
155
156
157
158

void   gprintf(const char *caller, const char *fmt, ...);

void   grsdef(void);

void   prtbin(int kin, int knbit, int *kout, int *kerr);
void   confp3(double pval, int *kexp, int *kmant, int kbits, int kround);
double decfp2(int kexp, int kmant);
void   ref2ibm(double *pref, int kbits);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
160
161
162
void   scaleComplex(double *fpdata, int pcStart, int pcScale, int trunc, int inv);
void   scatterComplex(double *fpdata, int pcStart, int trunc, int nsp);
void   gatherComplex(double *fpdata, int pcStart, int trunc, int nsp);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
void   scm0(double *pdl, double *pdr, double *pfl, double *pfr, int klg);
int    rowina2(double *p, int ko, int ki, double *pw,
	       int kcode, double msval, int *kret);
166
int    rowina3(double *p, int ko, int ki, double *pw,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
	       int kcode, double msval, int *kret, int omisng, int operio, int oveggy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
169
int    qu2reg2(double *pfield, int *kpoint, int klat, int klon,
	       double *ztemp, double msval, int *kret);
170
int    qu2reg3(double *pfield, int *kpoint, int klat, int klon,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
	       double msval, int *kret, int omisng, int operio, int oveggy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
173

#if  defined  (INT32)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
long   packInt32(unsigned INT32 *up, unsigned char *cp, long bc, long tc);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
long   packInt64(unsigned INT64 *up, unsigned char *cp, long bc, long tc);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
#if  defined  (INT32)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
long   unpackInt32(unsigned char *cp, unsigned INT32 *up, long bc, long tc);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
long   unpackInt64(unsigned char *cp, unsigned INT64 *up, long bc, long tc);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

void  gribEncode(int *isec0, int *isec1, int *isec2, double *fsec2, int *isec3,
		 double *fsec3, int *isec4, double *fsec4, int klenp, int *kgrib,
		 int kleng, int *kword, int efunc, int *kret);

void  gribDecode(int *isec0, int *isec1, int *isec2, double *fsec2, int *isec3,
		 double *fsec3, int *isec4, double *fsec4, int klenp, int *kgrib,
		 int kleng, int *kword, int dfunc, int *kret);

#endif  /* _GRIB_INT_H */
#ifndef _GRIBDECODE_H
#define _GRIBDECODE_H

#define  UNDEFINED          9.999e20


#define  GET_INT3(a,b,c)    ((1-(int) ((unsigned) (a & 128) >> 6)) * (int) (((a & 127) << 16)+(b<<8)+c))
#define  GET_INT2(a,b)      ((1-(int) ((unsigned) (a & 128) >> 6)) * (int) (((a & 127) << 8) + b))
199
#define  GET_INT1(a)        ((1-(int) ((unsigned) (a & 128) >> 6)) * (int) (a&127))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
201
202
203
204
205
206

/* this requires a 32-bit default integer machine */
#define  GET_UINT4(a,b,c,d) ((int) ((a << 24) + (b << 16) + (c << 8) + (d)))
#define  GET_UINT3(a,b,c)   ((int) ((a << 16) + (b << 8)  + (c)))
#define  GET_UINT2(a,b)     ((int) ((a << 8)  + (b)))
#define  GET_UINT1(a)       ((int)  (a))

Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
209
#define  BUDG_START(s)      (s[0]=='B' && s[1]=='U' && s[2]=='D' && s[3]=='G')
#define  TIDE_START(s)      (s[0]=='T' && s[1]=='I' && s[2]=='D' && s[3]=='E')
#define  GRIB_START(s)      (s[0]=='G' && s[1]=='R' && s[2]=='I' && s[3]=='B')
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
#define  GRIB_FIN(s)        (s[0]=='7' && s[1]=='7' && s[2]=='7' && s[3]=='7')
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211

Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
/* GRIB1 Section 0: Indicator Section (IS) */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213

214
#define  GRIB1_SECLEN(s)     GET_INT3(s[ 4], s[ 5], s[ 6])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
#define  GRIB_EDITION(s)     GET_UINT1(s[ 7])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216

Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
/* GRIB1 Section 1: Product Definition Section (PDS) */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231

#define  PDS_Len             GET_UINT3(pds[ 0], pds[ 1], pds[ 2])
#define  PDS_CodeTable       GET_UINT1(pds[ 3])
#define  PDS_CenterID        GET_UINT1(pds[ 4])
#define  PDS_ModelID         GET_UINT1(pds[ 5])
#define  PDS_GridDefinition  GET_UINT1(pds[ 6])
#define  PDS_Sec2Or3Flag     GET_UINT1(pds[ 7])
#define  PDS_HAS_GDS         ((pds[7] & 128) != 0)
#define  PDS_HAS_BMS         ((pds[7] &  64) != 0)
#define  PDS_Parameter       GET_UINT1(pds[ 8])
#define  PDS_LevelType       GET_UINT1(pds[ 9])
#define  PDS_Level1          (pds[10])
#define  PDS_Level2	     (pds[11])
#define  PDS_Level	     GET_UINT2(pds[10], pds[11])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
#define  PDS_Year            GET_INT1(pds[12])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#define  PDS_Month           GET_UINT1(pds[13])
#define  PDS_Day             GET_UINT1(pds[14])
#define  PDS_Hour            GET_UINT1(pds[15])
#define  PDS_Minute          GET_UINT1(pds[16])
#define  PDS_Date            (PDS_Year*10000+PDS_Month*100+PDS_Day)
#define  PDS_Time            (PDS_Hour*100+PDS_Minute)
#define  PDS_TimeUnit        GET_UINT1(pds[17])
#define  PDS_TimePeriod1     GET_UINT1(pds[18])
#define  PDS_TimePeriod2     GET_UINT1(pds[19])
#define  PDS_TimeRange       GET_UINT1(pds[20])
#define  PDS_AvgNum          GET_UINT2(pds[21], pds[22])
#define  PDS_AvgMiss         GET_UINT1(pds[23])
#define  PDS_Century         GET_UINT1(pds[24])
#define  PDS_Subcenter       GET_UINT1(pds[25])
#define  PDS_DecimalScale    GET_INT2(pds[26],pds[27])


Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
/* GRIB1 Section 2: Grid Description Section (GDS) */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
252
253
254

#define  GDS_Len             ((gds) == NULL ? 0 : GET_UINT3(gds[ 0], gds[ 1], gds[ 2]))
#define  GDS_NV              GET_UINT1(gds[ 3])
#define  GDS_PVPL            GET_UINT1(gds[ 4])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256
#define  GDS_PV	             ((gds[3] ==    0) ? -1 : (int) gds[4] - 1)
#define  GDS_PL	             ((gds[4] == 0xFF) ? -1 : (int) gds[3] * 4 + (int) gds[4] - 1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
258
259
#define  GDS_GridType        GET_UINT1(gds[ 5])


Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
/* GRIB1 Triangular grid of DWD */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
262
263
264
265
266
267
268
269
270
#define  GDS_GME_NI2         GET_UINT2(gds[ 6], gds[ 7])
#define  GDS_GME_NI3         GET_UINT2(gds[ 8], gds[ 9])
#define  GDS_GME_ND          GET_UINT3(gds[10], gds[11], gds[12])
#define  GDS_GME_NI          GET_UINT3(gds[13], gds[14], gds[15])
#define  GDS_GME_AFlag       GET_UINT1(gds[16])
#define  GDS_GME_LatPP       GET_INT3(gds[17], gds[18], gds[19])
#define  GDS_GME_LonPP       GET_INT3(gds[20], gds[21], gds[22])
#define  GDS_GME_LonMPL      GET_INT3(gds[23], gds[24], gds[25])
#define  GDS_GME_BFlag       GET_UINT1(gds[27])

Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
/* GRIB1 Spectral */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
273
274
275
276
277
#define  GDS_PentaJ          GET_UINT2(gds[ 6], gds[ 7])
#define  GDS_PentaK          GET_UINT2(gds[ 8], gds[ 9])
#define  GDS_PentaM          GET_UINT2(gds[10], gds[11])
#define  GDS_RepType         GET_UINT1(gds[12])
#define  GDS_RepMode         GET_UINT1(gds[13])

Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
/* GRIB1 Regular grid */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
280
281
282
283
284
285
#define  GDS_NumLon          GET_UINT2(gds[ 6], gds[ 7])
#define  GDS_NumLat          GET_UINT2(gds[ 8], gds[ 9])
#define  GDS_FirstLat        GET_INT3(gds[10], gds[11], gds[12])
#define  GDS_FirstLon        GET_INT3(gds[13], gds[14], gds[15])
#define  GDS_ResFlag         GET_UINT1(gds[16])
#define  GDS_LastLat         GET_INT3(gds[17], gds[18], gds[19])
#define  GDS_LastLon         GET_INT3(gds[20], gds[21], gds[22])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
287
288
#define  GDS_LonIncr         GET_UINT2(gds[23], gds[24])
#define  GDS_LatIncr         GET_UINT2(gds[25], gds[26])
#define  GDS_NumPar          GET_UINT2(gds[25], gds[26])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290
291
292
293
#define  GDS_ScanFlag        GET_UINT1(gds[27])
#define  GDS_LatSP           GET_INT3(gds[32], gds[33], gds[34])
#define  GDS_LonSP           GET_INT3(gds[35], gds[36], gds[37])
#define  GDS_RotAngle        GET_Real(&(gds[38]))

Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
/* GRIB1 Lambert */
295
296
297
298
299
300
301
302
#define  GDS_Lambert_Lov     GET_INT3(gds[17], gds[18], gds[19])
#define  GDS_Lambert_dx	     GET_INT3(gds[20], gds[21], gds[22])
#define  GDS_Lambert_dy	     GET_INT3(gds[23], gds[24], gds[25])
#define  GDS_Lambert_ProjFlag GET_UINT1(gds[26])
#define  GDS_Lambert_LatS1   GET_INT3(gds[28], gds[29], gds[30])
#define  GDS_Lambert_LatS2   GET_INT3(gds[31], gds[32], gds[33])
#define  GDS_Lambert_LatSP   GET_INT3(gds[34], gds[35], gds[36])
#define  GDS_Lambert_LonSP   GET_INT3(gds[37], gds[37], gds[37])
303

Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
/* GRIB1 Section 3: Bit Map Section (BMS) */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
306
307
308
309
310
311

#define  BMS_Len	     ((bms) == NULL ? 0 : (int) (bms[0]<<16)+(bms[1]<<8)+bms[2])
#define  BMS_UnusedBits      (bms[3])
#define  BMS_Numeric         
#define  BMS_Bitmap	     ((bms) == NULL ? NULL : (bms)+6)
#define  BMS_BitmapSize      (((((bms[0]<<16)+(bms[1]<<8)+bms[2]) - 6)<<3) - bms[3])

Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
/* GRIB1 Section 4: Binary Data Section (BDS) */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314
315
316
317
318
319

#define  BDS_Len	    ((int) ((bds[0]<<16)+(bds[1]<<8)+bds[2]))
#define  BDS_Flag	    (bds[3])
#define  BDS_BinScale       GET_INT2(bds[ 4], bds[ 5])
#define  BDS_RefValue       decfp2((int)bds[ 6], GET_UINT3(bds[ 7], bds[ 8], bds[ 9]))
#define  BDS_NumBits        ((int) bds[10])
#define  BDS_RealCoef       decfp2((int)bds[zoff+11], GET_UINT3(bds[zoff+12], bds[zoff+13], bds[zoff+14]))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
320
321
#define  BDS_PackData       ((int) ((bds[zoff+11]<<8) + bds[zoff+12]))
#define  BDS_Power          GET_INT2(bds[zoff+13], bds[zoff+14])
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
323
#define  BDS_Z              (bds[13])

Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
325
326
327
328
329
/* GRIB1 Section 5: End Section (ES) */

/* GRIB2 */

#define  GRIB2_SECLEN(section)   (GET_UINT4(section[0], section[1], section[2], section[3]))
#define  GRIB2_SECNUM(section)   (GET_UINT1(section[4]))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

#endif  /* _GRIBDECODE_H */
#ifndef _GRIBENCODE_H
#define _GRIBENCODE_H


#define PutnZero(n) \
{ \
  int i; \
  for ( i = z; i < z+n; i++ ) lGrib[i] = 0; \
  z += n; \
}

#define Put1Byte(Value)  (lGrib[z++] = (Value))
#define Put2Byte(Value) ((lGrib[z++] = (Value) >>  8), \
                         (lGrib[z++] = (Value)))
#define Put3Byte(Value) ((lGrib[z++] = (Value) >> 16), \
                         (lGrib[z++] = (Value) >>  8), \
                         (lGrib[z++] = (Value)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
351
352
#define Put4Byte(Value) ((lGrib[z++] = (Value) >> 24), \
                         (lGrib[z++] = (Value) >> 16), \
                         (lGrib[z++] = (Value) >>  8), \
                         (lGrib[z++] = (Value)))
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353

Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
355
356
#define Put1Int(Value)  {ival = Value; if ( ival < 0 ) ival =     0x80 - ival; Put1Byte(ival);}
#define Put2Int(Value)  {ival = Value; if ( ival < 0 ) ival =   0x8000 - ival; Put2Byte(ival);}
#define Put3Int(Value)  {ival = Value; if ( ival < 0 ) ival = 0x800000 - ival; Put3Byte(ival);}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
358
359
360
361
362
363
364
365

#define Put1Real(Value)          \
{                                \
  confp3(Value, &exponent, &mantissa, BitsPerInt, 1); \
  Put1Byte(exponent);            \
  Put3Byte(mantissa);            \
}

#endif  /* _GRIBENCODE_H */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
#include <stdio.h>
#include <math.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
369


Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
const double _pow2tab[158] = {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
 /* pow(2.0,  0.0) */  1.0,
 /* pow(2.0,  1.0) */  2.0,
 /* pow(2.0,  2.0) */  4.0,
 /* pow(2.0,  3.0) */  8.0,
 /* pow(2.0,  4.0) */  16.0,
 /* pow(2.0,  5.0) */  32.0,
 /* pow(2.0,  6.0) */  64.0,
 /* pow(2.0,  7.0) */  128.0,
 /* pow(2.0,  8.0) */  256.0,
 /* pow(2.0,  9.0) */  512.0,
 /* pow(2.0, 10.0) */  1024.0,
 /* pow(2.0, 11.0) */  2048.0,
 /* pow(2.0, 12.0) */  4096.0,
 /* pow(2.0, 13.0) */  8192.0,
 /* pow(2.0, 14.0) */  16384.0,
 /* pow(2.0, 15.0) */  32768.0,
 /* pow(2.0, 16.0) */  65536.0,
 /* pow(2.0, 17.0) */  131072.0,
 /* pow(2.0, 18.0) */  262144.0,
 /* pow(2.0, 19.0) */  524288.0,
 /* pow(2.0, 20.0) */  1048576.0,
 /* pow(2.0, 21.0) */  2097152.0,
 /* pow(2.0, 22.0) */  4194304.0,
 /* pow(2.0, 23.0) */  8388608.0,
 /* pow(2.0, 24.0) */  16777216.0,
 /* pow(2.0, 25.0) */  33554432.0,
 /* pow(2.0, 26.0) */  67108864.0,
 /* pow(2.0, 27.0) */  134217728.0,
 /* pow(2.0, 28.0) */  268435456.0,
 /* pow(2.0, 29.0) */  536870912.0,
 /* pow(2.0, 30.0) */  1073741824.0,
 /* pow(2.0, 31.0) */  2147483648.0,
 /* pow(2.0, 32.0) */  4294967296.0,
 /* pow(2.0, 33.0) */  8589934592.0,
 /* pow(2.0, 34.0) */  17179869184.0,
 /* pow(2.0, 35.0) */  34359738368.0,
 /* pow(2.0, 36.0) */  68719476736.0,
 /* pow(2.0, 37.0) */  137438953472.0,
 /* pow(2.0, 38.0) */  274877906944.0,
 /* pow(2.0, 39.0) */  549755813888.0,
 /* pow(2.0, 40.0) */  1099511627776.0,
 /* pow(2.0, 41.0) */  2199023255552.0,
 /* pow(2.0, 42.0) */  4398046511104.0,
 /* pow(2.0, 43.0) */  8796093022208.0,
 /* pow(2.0, 44.0) */  17592186044416.0,
 /* pow(2.0, 45.0) */  35184372088832.0,
 /* pow(2.0, 46.0) */  70368744177664.0,
 /* pow(2.0, 47.0) */  140737488355328.0,
 /* pow(2.0, 48.0) */  281474976710656.0,
 /* pow(2.0, 49.0) */  562949953421312.0,
 /* pow(2.0, 50.0) */  1125899906842624.0,
 /* pow(2.0, 51.0) */  2251799813685248.0,
 /* pow(2.0, 52.0) */  4503599627370496.0,
 /* pow(2.0, 53.0) */  9007199254740992.0,
 /* pow(2.0, 54.0) */  18014398509481984.0,
 /* pow(2.0, 55.0) */  36028797018963968.0,
 /* pow(2.0, 56.0) */  72057594037927936.0,
 /* pow(2.0, 57.0) */  144115188075855872.0,
 /* pow(2.0, 58.0) */  288230376151711744.0,
 /* pow(2.0, 59.0) */  576460752303423488.0,
 /* pow(2.0, 60.0) */  1152921504606846976.0,
 /* pow(2.0, 61.0) */  2305843009213693952.0,
 /* pow(2.0, 62.0) */  4611686018427387904.0,
 /* pow(2.0, 63.0) */  9223372036854775808.0,
 /* pow(2.0, 64.0) */  18446744073709551616.0,
 /* pow(2.0, 65.0) */  36893488147419103232.0,
 /* pow(2.0, 66.0) */  73786976294838206464.0,
 /* pow(2.0, 67.0) */  147573952589676412928.0,
 /* pow(2.0, 68.0) */  295147905179352825856.0,
 /* pow(2.0, 69.0) */  590295810358705651712.0,
 /* pow(2.0, 70.0) */  1180591620717411303424.0,
 /* pow(2.0, 71.0) */  2361183241434822606848.0,
 /* pow(2.0, 72.0) */  4722366482869645213696.0,
 /* pow(2.0, 73.0) */  9444732965739290427392.0,
 /* pow(2.0, 74.0) */  18889465931478580854784.0,
 /* pow(2.0, 75.0) */  37778931862957161709568.0,
 /* pow(2.0, 76.0) */  75557863725914323419136.0,
 /* pow(2.0, 77.0) */  151115727451828646838272.0,
 /* pow(2.0, 78.0) */  302231454903657293676544.0,
 /* pow(2.0, 79.0) */  604462909807314587353088.0,
 /* pow(2.0, 80.0) */  1208925819614629174706176.0,
 /* pow(2.0, 81.0) */  2417851639229258349412352.0,
 /* pow(2.0, 82.0) */  4835703278458516698824704.0,
 /* pow(2.0, 83.0) */  9671406556917033397649408.0,
 /* pow(2.0, 84.0) */  19342813113834066795298816.0,
 /* pow(2.0, 85.0) */  38685626227668133590597632.0,
 /* pow(2.0, 86.0) */  77371252455336267181195264.0,
 /* pow(2.0, 87.0) */  154742504910672534362390528.0,
 /* pow(2.0, 88.0) */  309485009821345068724781056.0,
 /* pow(2.0, 89.0) */  618970019642690137449562112.0,
 /* pow(2.0, 90.0) */  1237940039285380274899124224.0,
 /* pow(2.0, 91.0) */  2475880078570760549798248448.0,
 /* pow(2.0, 92.0) */  4951760157141521099596496896.0,
 /* pow(2.0, 93.0) */  9903520314283042199192993792.0,
 /* pow(2.0, 94.0) */  19807040628566084398385987584.0,
 /* pow(2.0, 95.0) */  39614081257132168796771975168.0,
 /* pow(2.0, 96.0) */  79228162514264337593543950336.0,
 /* pow(2.0, 97.0) */  158456325028528675187087900672.0,
 /* pow(2.0, 98.0) */  316912650057057350374175801344.0,
 /* pow(2.0, 99.0) */  633825300114114700748351602688.0,
 /* pow(2.0, 100.0) */  1267650600228229401496703205376.0,
 /* pow(2.0, 101.0) */  2535301200456458802993406410752.0,
 /* pow(2.0, 102.0) */  5070602400912917605986812821504.0,
 /* pow(2.0, 103.0) */  10141204801825835211973625643008.0,
 /* pow(2.0, 104.0) */  20282409603651670423947251286016.0,
 /* pow(2.0, 105.0) */  40564819207303340847894502572032.0,
 /* pow(2.0, 106.0) */  81129638414606681695789005144064.0,
 /* pow(2.0, 107.0) */  162259276829213363391578010288128.0,
 /* pow(2.0, 108.0) */  324518553658426726783156020576256.0,
 /* pow(2.0, 109.0) */  649037107316853453566312041152512.0,
 /* pow(2.0, 110.0) */  1298074214633706907132624082305024.0,
 /* pow(2.0, 111.0) */  2596148429267413814265248164610048.0,
 /* pow(2.0, 112.0) */  5192296858534827628530496329220096.0,
 /* pow(2.0, 113.0) */  10384593717069655257060992658440192.0,
 /* pow(2.0, 114.0) */  20769187434139310514121985316880384.0,
 /* pow(2.0, 115.0) */  41538374868278621028243970633760768.0,
 /* pow(2.0, 116.0) */  83076749736557242056487941267521536.0,
 /* pow(2.0, 117.0) */  166153499473114484112975882535043072.0,
 /* pow(2.0, 118.0) */  332306998946228968225951765070086144.0,
 /* pow(2.0, 119.0) */  664613997892457936451903530140172288.0,
 /* pow(2.0, 120.0) */  1329227995784915872903807060280344576.0,
 /* pow(2.0, 121.0) */  2658455991569831745807614120560689152.0,
 /* pow(2.0, 122.0) */  5316911983139663491615228241121378304.0,
 /* pow(2.0, 123.0) */  10633823966279326983230456482242756608.0,
 /* pow(2.0, 124.0) */  21267647932558653966460912964485513216.0,
 /* pow(2.0, 125.0) */  42535295865117307932921825928971026432.0,
 /* pow(2.0, 126.0) */  85070591730234615865843651857942052864.0,
 /* pow(2.0, 127.0) */  170141183460469231731687303715884105728.0,
 /* pow(2.0, 128.0) */  340282366920938463463374607431768211456.0,
 /* pow(2.0, 129.0) */  680564733841876926926749214863536422912.0,
 /* pow(2.0, 130.0) */  1361129467683753853853498429727072845824.0,
 /* pow(2.0, 131.0) */  2722258935367507707706996859454145691648.0,
 /* pow(2.0, 132.0) */  5444517870735015415413993718908291383296.0,
 /* pow(2.0, 133.0) */  10889035741470030830827987437816582766592.0,
 /* pow(2.0, 134.0) */  21778071482940061661655974875633165533184.0,
 /* pow(2.0, 135.0) */  43556142965880123323311949751266331066368.0,
 /* pow(2.0, 136.0) */  87112285931760246646623899502532662132736.0,
 /* pow(2.0, 137.0) */  174224571863520493293247799005065324265472.0,
 /* pow(2.0, 138.0) */  348449143727040986586495598010130648530944.0,
 /* pow(2.0, 139.0) */  696898287454081973172991196020261297061888.0,
 /* pow(2.0, 140.0) */  1393796574908163946345982392040522594123776.0,
 /* pow(2.0, 141.0) */  2787593149816327892691964784081045188247552.0,
 /* pow(2.0, 142.0) */  5575186299632655785383929568162090376495104.0,
 /* pow(2.0, 143.0) */  11150372599265311570767859136324180752990208.0,
 /* pow(2.0, 144.0) */  22300745198530623141535718272648361505980416.0,
 /* pow(2.0, 145.0) */  44601490397061246283071436545296723011960832.0,
 /* pow(2.0, 146.0) */  89202980794122492566142873090593446023921664.0,
 /* pow(2.0, 147.0) */  178405961588244985132285746181186892047843328.0,
 /* pow(2.0, 148.0) */  356811923176489970264571492362373784095686656.0,
 /* pow(2.0, 149.0) */  713623846352979940529142984724747568191373312.0,
 /* pow(2.0, 150.0) */  1427247692705959881058285969449495136382746624.0,
 /* pow(2.0, 151.0) */  2854495385411919762116571938898990272765493248.0,
 /* pow(2.0, 152.0) */  5708990770823839524233143877797980545530986496.0,
 /* pow(2.0, 153.0) */  11417981541647679048466287755595961091061972992.0,
 /* pow(2.0, 154.0) */  22835963083295358096932575511191922182123945984.0,
 /* pow(2.0, 155.0) */  45671926166590716193865151022383844364247891968.0,
 /* pow(2.0, 156.0) */  91343852333181432387730302044767688728495783936.0,
 /* pow(2.0, 157.0) */  182687704666362864775460604089535377456991567872.0,
};


Uwe Schulzweida's avatar
Uwe Schulzweida committed
532
const double _pow16tab[71] = {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
 /* pow(16.0,  0.0) */  1.0,
 /* pow(16.0,  1.0) */  16.0,
 /* pow(16.0,  2.0) */  256.0,
 /* pow(16.0,  3.0) */  4096.0,
 /* pow(16.0,  4.0) */  65536.0,
 /* pow(16.0,  5.0) */  1048576.0,
 /* pow(16.0,  6.0) */  16777216.0,
 /* pow(16.0,  7.0) */  268435456.0,
 /* pow(16.0,  8.0) */  4294967296.0,
 /* pow(16.0,  9.0) */  68719476736.0,
 /* pow(16.0, 10.0) */  1099511627776.0,
 /* pow(16.0, 11.0) */  17592186044416.0,
 /* pow(16.0, 12.0) */  281474976710656.0,
 /* pow(16.0, 13.0) */  4503599627370496.0,
 /* pow(16.0, 14.0) */  72057594037927936.0,
 /* pow(16.0, 15.0) */  1152921504606846976.0,
 /* pow(16.0, 16.0) */  18446744073709551616.0,
 /* pow(16.0, 17.0) */  295147905179352825856.0,
 /* pow(16.0, 18.0) */  4722366482869645213696.0,
 /* pow(16.0, 19.0) */  75557863725914323419136.0,
 /* pow(16.0, 20.0) */  1208925819614629174706176.0,
 /* pow(16.0, 21.0) */  19342813113834066795298816.0,
 /* pow(16.0, 22.0) */  309485009821345068724781056.0,
 /* pow(16.0, 23.0) */  4951760157141521099596496896.0,
 /* pow(16.0, 24.0) */  79228162514264337593543950336.0,
 /* pow(16.0, 25.0) */  1267650600228229401496703205376.0,
 /* pow(16.0, 26.0) */  20282409603651670423947251286016.0,
 /* pow(16.0, 27.0) */  324518553658426726783156020576256.0,
 /* pow(16.0, 28.0) */  5192296858534827628530496329220096.0,
 /* pow(16.0, 29.0) */  83076749736557242056487941267521536.0,
 /* pow(16.0, 30.0) */  1329227995784915872903807060280344576.0,
 /* pow(16.0, 31.0) */  21267647932558653966460912964485513216.0,
 /* pow(16.0, 32.0) */  340282366920938463463374607431768211456.0,
 /* pow(16.0, 33.0) */  5444517870735015415413993718908291383296.0,
 /* pow(16.0, 34.0) */  87112285931760246646623899502532662132736.0,
 /* pow(16.0, 35.0) */  1393796574908163946345982392040522594123776.0,
 /* pow(16.0, 36.0) */  22300745198530623141535718272648361505980416.0,
 /* pow(16.0, 37.0) */  356811923176489970264571492362373784095686656.0,
 /* pow(16.0, 38.0) */  5708990770823839524233143877797980545530986496.0,
 /* pow(16.0, 39.0) */  91343852333181432387730302044767688728495783936.0,
 /* pow(16.0, 40.0) */  1461501637330902918203684832716283019655932542976.0,
 /* pow(16.0, 41.0) */  23384026197294446691258957323460528314494920687616.0,
 /* pow(16.0, 42.0) */  374144419156711147060143317175368453031918731001856.0,
 /* pow(16.0, 43.0) */  5986310706507378352962293074805895248510699696029696.0,
 /* pow(16.0, 44.0) */  95780971304118053647396689196894323976171195136475136.0,
 /* pow(16.0, 45.0) */  1532495540865888858358347027150309183618739122183602176.0,
 /* pow(16.0, 46.0) */  24519928653854221733733552434404946937899825954937634816.0,
 /* pow(16.0, 47.0) */  392318858461667547739736838950479151006397215279002157056.0,
 /* pow(16.0, 48.0) */  6277101735386680763835789423207666416102355444464034512896.0,
 /* pow(16.0, 49.0) */  100433627766186892221372630771322662657637687111424552206336.0,
 /* pow(16.0, 50.0) */  1606938044258990275541962092341162602522202993782792835301376.0,
 /* pow(16.0, 51.0) */  25711008708143844408671393477458601640355247900524685364822016.0,
 /* pow(16.0, 52.0) */  411376139330301510538742295639337626245683966408394965837152256.0,
 /* pow(16.0, 53.0) */  6582018229284824168619876730229402019930943462534319453394436096.0,
 /* pow(16.0, 54.0) */  105312291668557186697918027683670432318895095400549111254310977536.0,
 /* pow(16.0, 55.0) */  1684996666696914987166688442938726917102321526408785780068975640576.0,
 /* pow(16.0, 56.0) */  26959946667150639794667015087019630673637144422540572481103610249216.0,
 /* pow(16.0, 57.0) */  431359146674410236714672241392314090778194310760649159697657763987456.0,
 /* pow(16.0, 58.0) */  6901746346790563787434755862277025452451108972170386555162524223799296.0,
 /* pow(16.0, 59.0) */  110427941548649020598956093796432407239217743554726184882600387580788736.0,
 /* pow(16.0, 60.0) */  1766847064778384329583297500742918515827483896875618958121606201292619776.0,
 /* pow(16.0, 61.0) */  28269553036454149273332760011886696253239742350009903329945699220681916416.0,
 /* pow(16.0, 62.0) */  452312848583266388373324160190187140051835877600158453279131187530910662656.0,
 /* pow(16.0, 63.0) */  7237005577332262213973186563042994240829374041602535252466099000494570602496.0,
 /* pow(16.0, 64.0) */  115792089237316195423570985008687907853269984665640564039457584007913129639936.0,
 /* pow(16.0, 65.0) */  1852673427797059126777135760139006525652319754650249024631321344126610074238976.0,
 /* pow(16.0, 66.0) */  29642774844752946028434172162224104410437116074403984394101141506025761187823616.0,
 /* pow(16.0, 67.0) */  474284397516047136454946754595585670566993857190463750305618264096412179005177856.0,
 /* pow(16.0, 68.0) */  7588550360256754183279148073529370729071901715047420004889892225542594864082845696.0,
 /* pow(16.0, 69.0) */  121416805764108066932466369176469931665150427440758720078238275608681517825325531136.0,
 /* pow(16.0, 70.0) */  1942668892225729070919461906823518906642406839052139521251812409738904285205208498176.0,
};

static int _pow2tab_size = sizeof(_pow2tab)/sizeof(double);

void gen_pow2tab(void)
{
  int jloop;

  for ( jloop = 0; jloop < 158; jloop++ )
    printf(" /* pow(2.0, %2d.0) */  %.1f,\n", jloop,  pow(2.0, (double) jloop));
}


void gen_pow16tab(void)
{
  double pval;
  int iexp;

  for ( iexp = 0; iexp < 71; iexp++ )
    {
      pval = pow(16.0, (double)(iexp));
      printf(" /* pow(16.0, %2d.0) */  %.1f,\n", iexp, pval);
    }
}


double intpow2(int x)
{
  if ( x < _pow2tab_size )
    return (_pow2tab[x]);
  else
    return (pow(2.0, (double) x));
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
637
638
639
/* 
gcc -g -Wall -O3 -march=native -std=c99 -DTEST_MINMAXVAL minmax_val.c
 result on bailung (gcc 4.7):
Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
641
  orig    : fmin: -500000  fmax: 499999  time:   4.84s
  sse2    : fmin: -500000  fmax: 499999  time:   4.82s
Uwe Schulzweida's avatar
Uwe Schulzweida committed
642
643

gcc -g -Wall -O3 -march=native -std=c99 -DTEST_MINMAXVAL minmax_val.c
Uwe Schulzweida's avatar
Uwe Schulzweida committed
644
645
646
 result on thunder5 (gcc 4.7.2):
  orig    : fmin: -500000  fmax: 499999  time:   3.10s
  avx     : fmin: -500000  fmax: 499999  time:   2.84s
Uwe Schulzweida's avatar
Uwe Schulzweida committed
647
648

icc -g -Wall -O3 -march=native -std=c99 -vec-report=1 -DTEST_MINMAXVAL minmax_val.c
Uwe Schulzweida's avatar
Uwe Schulzweida committed
649
650
651
 result on thunder5 (icc 13.1.2):
  orig    : fmin: -500000  fmax: 499999  time:   2.83s
  avx     : fmin: -500000  fmax: 499999  time:   2.92s
Uwe Schulzweida's avatar
Uwe Schulzweida committed
652

Uwe Schulzweida's avatar
Uwe Schulzweida committed
653
xlc_r -g -O3 -qhot -q64 -qarch=auto -qtune=auto -qreport -DTEST_MINMAXVAL minmax_val.c
Uwe Schulzweida's avatar
Uwe Schulzweida committed
654
 result on blizzard (xlc 12):
Uwe Schulzweida's avatar
Uwe Schulzweida committed
655
656
  orig    : fmin: -500000  fmax: 499999  time:   7.26s
  pwr6u6  : fmin: -500000  fmax: 499999  time:   5.92s
Uwe Schulzweida's avatar
Uwe Schulzweida committed
657
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
658
#if defined(_ARCH_PWR6)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
659
660
#pragma options nostrict
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
661
662
663

#include <stdlib.h>

Uwe Schulzweida's avatar
Uwe Schulzweida committed
664
//#undef _GET_X86_COUNTER
Uwe Schulzweida's avatar
Uwe Schulzweida committed
665
//#undef _GET_IBM_COUNTER
Uwe Schulzweida's avatar
Uwe Schulzweida committed
666
//#undef _GET_MACH_COUNTER
Uwe Schulzweida's avatar
Uwe Schulzweida committed
667
//#undef _ARCH_PWR6
Uwe Schulzweida's avatar
Uwe Schulzweida committed
668

Uwe Schulzweida's avatar
Uwe Schulzweida committed
669
#if defined(_GET_IBM_COUNTER)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
670
#include <libhpc.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
671
#elif defined(_GET_X86_COUNTER)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
672
#include <x86intrin.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
673
#elif defined(_GET_MACH_COUNTER)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
674
#include <mach/mach_time.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
675
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
676

Uwe Schulzweida's avatar
Uwe Schulzweida committed
677
678
679
680
681
682
#if   defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
#if (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 4)
#define GNUC_PUSH_POP
#endif
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
683
684
685
686
687
688
689
#if   defined(__GNUC__) && (__GNUC__ >= 4)
#elif defined(__ICC)    && (__ICC >= 1100)
#elif defined(__clang__)
#else
#define DISABLE_SIMD
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
690
#if !defined(TEST_MINMAXVAL)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
691
#define DISABLE_SIMD
Uwe Schulzweida's avatar
Uwe Schulzweida committed
692
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
693

Uwe Schulzweida's avatar
Uwe Schulzweida committed
694
695
#if defined(DISABLE_SIMD)
# if defined(ENABLE_AVX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
696
697
#  define _ENABLE_AVX
# endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
698
# if defined(ENABLE_SSE2)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
699
700
#  define _ENABLE_SSE2
# endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
701
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
702

Uwe Schulzweida's avatar
Uwe Schulzweida committed
703
704
#if !defined(DISABLE_SIMD)
# if defined(__AVX__)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
705
706
#  define _ENABLE_AVX
# endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
707
# if defined(__SSE2__)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
708
709
710
#  define _ENABLE_SSE2
# endif
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
711

Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
713
714
#include <float.h>
#include <stdint.h>
#include <inttypes.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
715

Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
#if defined(_ENABLE_AVX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
717
#include <immintrin.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
718
#elif defined(_ENABLE_SSE2)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
719
#include <emmintrin.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
720
721
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
722

Uwe Schulzweida's avatar
Uwe Schulzweida committed
723
#if defined(_ENABLE_AVX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724

Uwe Schulzweida's avatar
Uwe Schulzweida committed
725
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
void avx_minmax_val(const double *restrict buf, size_t nframes, double *min, double *max)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
727
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728
  double fmin[4], fmax[4];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
729
  __m256d current_max, current_min, work;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
730

Uwe Schulzweida's avatar
Uwe Schulzweida committed
731
  // load max and min values into all four slots of the YMM registers
Uwe Schulzweida's avatar
Uwe Schulzweida committed
732
733
  current_min = _mm256_set1_pd(*min);
  current_max = _mm256_set1_pd(*max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
734

Uwe Schulzweida's avatar
Uwe Schulzweida committed
735
736
  // Work input until "buf" reaches 32 byte alignment
  while ( ((unsigned long)buf) % 32 != 0 && nframes > 0) {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
737

Uwe Schulzweida's avatar
Uwe Schulzweida committed
738
739
740
741
742
743
744
    // Load the next double into the work buffer
    work = _mm256_set1_pd(*buf);
    current_min = _mm256_min_pd(current_min, work);
    current_max = _mm256_max_pd(current_max, work);
    buf++;
    nframes--;
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
745

Uwe Schulzweida's avatar
Uwe Schulzweida committed
746
  while (nframes >= 16) {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
747

Uwe Schulzweida's avatar
Uwe Schulzweida committed
748
749
750
    (void) _mm_prefetch(buf+8, _MM_HINT_NTA);

    work = _mm256_load_pd(buf);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
751
752
753
    current_min = _mm256_min_pd(current_min, work);
    current_max = _mm256_max_pd(current_max, work);
    buf += 4;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
754
755

    work = _mm256_load_pd(buf);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757
758
    current_min = _mm256_min_pd(current_min, work);
    current_max = _mm256_max_pd(current_max, work);
    buf += 4;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
760
761
762

    (void) _mm_prefetch(buf+8, _MM_HINT_NTA);

    work = _mm256_load_pd(buf);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763
764
765
    current_min = _mm256_min_pd(current_min, work);
    current_max = _mm256_max_pd(current_max, work);
    buf += 4;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766
767

    work = _mm256_load_pd(buf);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
768
769
770
771
772
    current_min = _mm256_min_pd(current_min, work);
    current_max = _mm256_max_pd(current_max, work);
    buf += 4;
    nframes -= 16;
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
773

Uwe Schulzweida's avatar
Uwe Schulzweida committed
774
775
776
777
778
779
780
781
  // work through aligned buffers
  while (nframes >= 4) {
    work = _mm256_load_pd(buf);
    current_min = _mm256_min_pd(current_min, work);
    current_max = _mm256_max_pd(current_max, work);
    buf += 4;
    nframes -= 4;
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
782

Uwe Schulzweida's avatar
Uwe Schulzweida committed
783
784
785
786
787
788
789
790
  // work through the remainung values
  while ( nframes > 0) {
    work = _mm256_set1_pd(*buf);
    current_min = _mm256_min_pd(current_min, work);
    current_max = _mm256_max_pd(current_max, work);
    buf++;
    nframes--;
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
791

Uwe Schulzweida's avatar
Uwe Schulzweida committed
792
  // find min & max value through shuffle tricks
Uwe Schulzweida's avatar
Uwe Schulzweida committed
793

Uwe Schulzweida's avatar
Uwe Schulzweida committed
794
  work = current_min;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
795
  work = _mm256_shuffle_pd(work, work, 5);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
797
  work = _mm256_min_pd (work, current_min);
  current_min = work;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
798
  work = _mm256_permute2f128_pd(work, work, 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
799
  work = _mm256_min_pd (work, current_min);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
800
801
  _mm256_storeu_pd(fmin, work);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
802
  work = current_max;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
803
804
  work = current_max;
  work = _mm256_shuffle_pd(work, work, 5);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
805
806
  work = _mm256_max_pd (work, current_max);
  current_max = work;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
807
  work = _mm256_permute2f128_pd(work, work, 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
808
  work = _mm256_max_pd (work, current_max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
809
810
811
812
  _mm256_storeu_pd(fmax, work);

  *min = fmin[0];
  *max = fmax[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
813

Uwe Schulzweida's avatar
Uwe Schulzweida committed
814
815
  return;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
816

Uwe Schulzweida's avatar
Uwe Schulzweida committed
817
#elif defined(_ENABLE_SSE2)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
818

Uwe Schulzweida's avatar
Uwe Schulzweida committed
819
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
820
void sse2_minmax_val(const double *restrict buf, size_t nframes, double *min, double *max)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
{
  __m128d current_max, current_min, work;
  
  // load starting max and min values into all slots of the XMM registers
  current_min = _mm_set1_pd(*min);
  current_max = _mm_set1_pd(*max);
  
  // work on input until buf reaches 16 byte alignment
  while ( ((unsigned long)buf) % 16 != 0 && nframes > 0) {
    
    // load one double and replicate
    work = _mm_set1_pd(*buf);    
    current_min = _mm_min_pd(current_min, work);
    current_max = _mm_max_pd(current_max, work);    
    buf++;
    nframes--;
  }
  
  while (nframes >= 8) {
    // use 64 byte prefetch for double octetts
Uwe Schulzweida's avatar
Uwe Schulzweida committed
841
    // __builtin_prefetch(buf+64,0,0); // for GCC 4.3.2 +
Uwe Schulzweida's avatar
Uwe Schulzweida committed
842

Uwe Schulzweida's avatar
Uwe Schulzweida committed
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
    work = _mm_load_pd(buf);
    current_min = _mm_min_pd(current_min, work);
    current_max = _mm_max_pd(current_max, work);
    buf += 2;
    work = _mm_load_pd(buf);
    current_min = _mm_min_pd(current_min, work);
    current_max = _mm_max_pd(current_max, work);
    buf += 2;
    work = _mm_load_pd(buf);
    current_min = _mm_min_pd(current_min, work);
    current_max = _mm_max_pd(current_max, work);
    buf += 2;
    work = _mm_load_pd(buf);
    current_min = _mm_min_pd(current_min, work);
    current_max = _mm_max_pd(current_max, work);
    buf += 2;
    nframes -= 8;
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
861

Uwe Schulzweida's avatar
Uwe Schulzweida committed
862
863
864
865
866
867
868
869
  // work through smaller chunks of aligned buffers without prefetching
  while (nframes >= 2) {
    work = _mm_load_pd(buf);
    current_min = _mm_min_pd(current_min, work);
    current_max = _mm_max_pd(current_max, work);
    buf += 2;
    nframes -= 2;
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
870

Uwe Schulzweida's avatar
Uwe Schulzweida committed
871
872
873
874
875
876
877
878
879
  // work through the remaining value
  while ( nframes > 0) {
    // load the last double and replicate
    work = _mm_set1_pd(*buf);
    current_min = _mm_min_pd(current_min, work);
    current_max = _mm_max_pd(current_max, work);
    buf++;
    nframes--;
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
880

Uwe Schulzweida's avatar
Uwe Schulzweida committed
881
882
883
884
885
886
887
888
889
  // find final min and max value through shuffle tricks
  work = current_min;
  work = _mm_shuffle_pd(work, work, _MM_SHUFFLE2(0, 1));
  work = _mm_min_pd (work, current_min);
  _mm_store_sd(min, work);
  work = current_max;
  work = _mm_shuffle_pd(work, work, _MM_SHUFFLE2(0, 1));
  work = _mm_max_pd (work, current_max);
  _mm_store_sd(max, work);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
890

Uwe Schulzweida's avatar
Uwe Schulzweida committed
891
892
  return;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
893

Uwe Schulzweida's avatar
Uwe Schulzweida committed
894
#endif // SIMD
Uwe Schulzweida's avatar
Uwe Schulzweida committed
895

Uwe Schulzweida's avatar
Uwe Schulzweida committed
896
#if defined(_ARCH_PWR6)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
897
898
899
static
void pwr6_minmax_val_unrolled6(const double *restrict data, long idatasize, double *fmin, double *fmax)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
900
#define __UNROLL_DEPTH_1 6
Uwe Schulzweida's avatar
Uwe Schulzweida committed
901
  size_t datasize = idatasize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
902

Uwe Schulzweida's avatar
Uwe Schulzweida committed
903
  // to allow pipelining we have to unroll 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
904

Uwe Schulzweida's avatar
Uwe Schulzweida committed
905
  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
906
907
908
    size_t i, j;
    size_t residual =  datasize % __UNROLL_DEPTH_1;
    size_t ofs = datasize - residual;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
910
    double register dmin[__UNROLL_DEPTH_1];
    double register dmax[__UNROLL_DEPTH_1];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
911

Uwe Schulzweida's avatar
Uwe Schulzweida committed
912
913
914
915
916
917
918
919
920
921
922
923
924
925
    for ( j = 0; j < __UNROLL_DEPTH_1; j++) 
      {
	dmin[j] = data[0];
	dmax[j] = data[0];
      }
    
    for ( i = 0; i < datasize - residual; i += __UNROLL_DEPTH_1 ) 
      {
	for (j = 0; j < __UNROLL_DEPTH_1; j++) 
	  {
	    dmin[j] = __fsel(dmin[j] - data[i+j], data[i+j], dmin[j]);
	    dmax[j] = __fsel(data[i+j] - dmax[j], data[i+j], dmax[j]);
	  }
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
926

Uwe Schulzweida's avatar
Uwe Schulzweida committed
927
928
929
930
931
    for (j = 0; j < residual; j++) 
      {
	dmin[j] = __fsel(dmin[j] - data[ofs+j], data[ofs+j], dmin[j]);
	dmax[j] = __fsel(data[ofs+j] - dmax[j], data[ofs+j], dmax[j]);
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
932

Uwe Schulzweida's avatar
Uwe Schulzweida committed
933
934
935
936
937
938
939
    for ( j = 0; j < __UNROLL_DEPTH_1; j++) 
      {
	*fmin = __fsel(*fmin - dmin[j], dmin[j], *fmin);
	*fmax = __fsel(dmax[j] - *fmax, dmax[j], *fmax);
      }
  }
#undef __UNROLL_DEPTH_1
Uwe Schulzweida's avatar
Uwe Schulzweida committed
940
941
}
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
942

Uwe Schulzweida's avatar
Uwe Schulzweida committed
943
944
945
946
#if defined(TEST_MINMAXVAL) && defined(__GNUC__)
static
void minmax_val_orig(const double *restrict data, long idatasize, double *fmin, double *fmax) __attribute__ ((noinline));
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
947

Uwe Schulzweida's avatar
Uwe Schulzweida committed
948
#if defined(GNUC_PUSH_POP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
949
#pragma GCC push_options
Uwe Schulzweida's avatar
Uwe Schulzweida committed
950
#pragma GCC optimize ("O3", "fast-math")
Uwe Schulzweida's avatar
Uwe Schulzweida committed
951
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
952
953
954
955
956
957
static
void minmax_val_orig(const double *restrict data, long idatasize, double *fmin, double *fmax)
{
  size_t i;
  size_t datasize = idatasize;
  double dmin = *fmin, dmax = *fmax;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
958

Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
#if   defined(CRAY)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
#pragma _CRI ivdep
Uwe Schulzweida's avatar
Uwe Schulzweida committed
961
#elif defined(SX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
962
#pragma vdir nodep
Uwe Schulzweida's avatar
Uwe Schulzweida committed
963
#elif defined(__uxp__)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
964
965
#pragma loop novrec
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
966
967
968
969
970
971
972
973
974
975
976
  for ( i = 0; i < datasize; ++i )
    {
      dmin = dmin < data[i] ? dmin : data[i];
      dmax = dmax > data[i] ? dmax : data[i];
      //      if ( dmin > data[i] ) dmin = data[i];
      //      if ( dmax < data[i] ) dmax = data[i];
    }

  *fmin = dmin;
  *fmax = dmax;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
977
#if defined(GNUC_PUSH_POP)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
978
979
980
981
982
983
#pragma GCC pop_options
#endif

static
void minmax_val(const double *restrict data, long idatasize, double *fmin, double *fmax)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
984
#if defined(_GET_X86_COUNTER) || defined(_GET_MACH_COUNTER) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
985
986
987
988
989
990
  uint64_t start_minmax, end_minmax;
#endif
  size_t datasize = idatasize;

  if ( idatasize < 1 ) return;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
991
#if defined(_GET_X86_COUNTER) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
992
993
  start_minmax = _rdtsc();
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
994
#if defined(_GET_MACH_COUNTER) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
995
996
997
  start_minmax = mach_absolute_time();
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
998
#if defined(_ENABLE_AVX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
999
1000
1001

  avx_minmax_val(data, datasize, fmin, fmax);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1002
#elif defined(_ENABLE_SSE2)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1003
1004
1005
1006
1007

  sse2_minmax_val(data, datasize, fmin, fmax);

#else

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1008
#if defined(_ARCH_PWR6)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1009
1010
1011
1012
#define __UNROLL_DEPTH_1 6

  // to allow pipelining we have to unroll 

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1013
#if defined(_GET_IBM_COUNTER)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1014
1015
1016
1017
1018
  hpmStart(1, "minmax fsel");
#endif

  pwr6_minmax_val_unrolled6(data, datasize, fmin, fmax);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1019
#if defined(_GET_IBM_COUNTER) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1020
1021
1022
1023
1024
1025
1026
  hpmStop(1);
#endif

#undef __UNROLL_DEPTH_1

#else // original loop

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1027
#if defined(_GET_IBM_COUNTER) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1028
1029
1030
1031
1032
  hpmStart(1, "minmax base");
#endif

  minmax_val_orig(data, datasize, fmin, fmax);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1033
#if defined(_GET_IBM_COUNTER) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1034
1035
  hpmStop(1);
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1036

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1037
#endif // _ARCH_PWR6 && original loop
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1038
#endif // SIMD
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1039

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1040
1041
#if defined(_GET_X86_COUNTER) || defined(_GET_MACH_COUNTER)
#if defined(_GET_X86_COUNTER) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1042
  end_minmax = _rdtsc();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1043
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1044
#if defined(_GET_MACH_COUNTER) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1045
1046
  end_minmax = mach_absolute_time();
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1047
#if defined(_ENABLE_AVX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1048
  printf("AVX minmax cycles:: %" PRIu64 "\n",  end_minmax-start_minmax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1049
  fprintf (stderr, "AVX min: %lf max: %lf\n", *fmin, *fmax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1050
#elif defined(_ENABLE_SSE2)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1051
  printf("SSE2 minmax cycles:: %" PRIu64 "\n", end_minmax-start_minmax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1052
  fprintf (stderr, "SSE2 min: %lf max: %lf\n", *fmin, *fmax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1053
#else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1054
  printf("loop minmax cycles:: %" PRIu64 "\n", end_minmax-start_minmax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1055
  fprintf (stderr, "loop min: %lf max: %lf\n", *fmin, *fmax);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1056
1057
#endif
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1058

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1059
1060
  return;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1061

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1062
#if defined(TEST_MINMAXVAL)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085

#include <stdio.h>
#include <sys/time.h>

static
double dtime()
{
  double tseconds = 0.0;
  struct timeval mytime;
  gettimeofday(&mytime, NULL);
  tseconds = (double) (mytime.tv_sec + (double)mytime.tv_usec*1.0e-6);
  return (tseconds);
}

#define NRUN 10000

int main(void)
{
  long datasize = 1000000;
  double *data = NULL;
  double fmin, fmax;
  double t_begin, t_end;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1086
1087
1088
1089
1090
1091
1092
1093
#if   defined(__ICC)
  printf("icc\n");
#elif defined(__clang__)
  printf("clang\n");
#elif defined(__GNUC__)
  printf("gcc\n");
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1094
  data = malloc(datasize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1095
1096
1097

  for ( long i = datasize-1; i >= 0; i-- ) data[i] = (double) (-datasize/2 + i);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
  t_begin = dtime();
  for ( int i = 0; i < NRUN; ++i )
    {
      fmin = fmax = data[0];
      minmax_val(data, datasize, &fmin, &fmax);
    }
  t_end = dtime();
  printf("minmax_val: fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1107
1108
1109
1110
1111
1112
1113
  t_begin = dtime();
  for ( int i = 0; i < NRUN; ++i )
    {
      fmin = fmax = data[0];
      minmax_val_orig(data, datasize, &fmin, &fmax);
    }
  t_end = dtime();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1114
  printf("orig      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1115

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1116
#if defined(_ENABLE_AVX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1117
1118
1119
1120
1121
1122
1123
  t_begin = dtime();
  for ( int i = 0; i < NRUN; ++i )
    {
      fmin = fmax = data[0];
      avx_minmax_val(data, datasize, &fmin, &fmax);
    }
  t_end = dtime();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1124
1125
  printf("avx       : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
#elif defined(_ENABLE_SSE2)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1126
1127
1128
1129
1130
1131
1132
  t_begin = dtime();
  for ( int i = 0; i < NRUN; ++i )
    {
      fmin = fmax = data[0];
      sse2_minmax_val(data, datasize, &fmin, &fmax);
    }
  t_end = dtime();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1133
  printf("sse2      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1134
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1135
#if defined(_ARCH_PWR6)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1136
1137
1138
1139
1140
1141
1142
  t_begin = dtime();
  for ( int i = 0; i < NRUN; ++i )
    {
      fmin = fmax = data[0];
      pwr6_minmax_val_unrolled6(data, datasize, &fmin, &fmax);
    }
  t_end = dtime();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1143
  printf("pwr6u6  : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1144
1145
1146
1147
1148
1149
#endif

  return (0);
}
#endif // TEST_MINMAXVAL

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1150
1151
1152
#undef DISABLE_SIMD
#undef _ENABLE_AVX
#undef _ENABLE_SSE2
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1153
#undef GNUC_PUSH_POP
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
/* 
gcc -g -Wall -O3 -march=native -std=c99 -DTEST_MINMAXVAL encode_double_array.c
 result on bailung (gcc 4.7):
  orig    : val1: 1  val2: 1  val3: 2  valn: 66  time: 8.4166s
  sse41   : val1: 1  val2: 1  val3: 2  valn: 66  time: 7.1522s

gcc -g -Wall -O3 -march=native -std=c99 -DTEST_MINMAXVAL encode_double_array.c
 result on thunder5 (gcc 4.7):
  orig    : val1: 1  val2: 1  val3: 2  valn: 66  time: 6.21976s
  avx     : val1: 1  val2: 1  val3: 2  valn: 66  time: 4.54485s

icc -g -Wall -O3 -march=native -std=c99 -vec-report=1 -DTEST_MINMAXVAL encode_double_array.c
 result on thunder5 (icc 13.2):
  orig    : val1: 1  val2: 1  val3: 2  valn: 66  time: 14.6279s
  avx     : val1: 1  val2: 1  val3: 2  valn: 66  time:  4.9776s

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1170
xlc_r -g -O3 -qhot -q64 -qarch=auto -qtune=auto -qreport -DTEST_MINMAXVAL encode_double_array.c
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1171
 result on blizzard (xlc 12):
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1172
1173
1174
1175
  orig    : val1: 1  val2: 1  val3: 2  valn: 66  time: 132.25s
  unrolled: val1: 1  val2: 1  val3: 2  valn: 66  time:  27.202s
  orig    : val1: 1  val2: 1  val3: 2  valn: 66  time: 106.627s  // without -qhot
  unrolled: val1: 1  val2: 1  val3: 2  valn: 66  time:  39.929s  // without -qhot
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1176
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1177
1178
1179
#ifdef _ARCH_PWR6
#pragma options nostrict
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189

#ifdef TEST_MINMAXVAL
#include <stdio.h>
#include <stdlib.h>
#define  GRIBPACK     unsigned char
#define  IS_BIGENDIAN()  (u_byteorder.c[sizeof(long) - 1])
#define  U_BYTEORDER     static union {unsigned long l; unsigned char c[sizeof(long)];} u_byteorder = {1}
#define  Error(x,y)
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1190
1191
//#undef _GET_X86_COUNTER
//#undef _GET_MACH_COUNTER
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1192
//#undef _GET_IBM_COUNTER
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1193
1194
//#undef _ARCH_PWR6

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1195
1196
1197
1198
1199
1200
1201
1202
#if defined _GET_IBM_COUNTER
#include <libhpc.h>
#elif defined _GET_X86_COUNTER
#include <x86intrin.h>
#elif defined _GET_MACH_COUNTER
#include <mach/mach_time.h>
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1203
#include <stdint.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1204

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1205
1206
#if   defined(__GNUC__) && (__GNUC__ >= 4)
#elif defined(__ICC)    && (__ICC >= 1100)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1207
#elif defined(__clang__)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1208
1209
1210
1211
#else
#define DISABLE_SIMD
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1212
//#define DISABLE_SIMD
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1213

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1214
#ifdef DISABLE_SIMD
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1215
1216
1217
1218
1219
1220
# ifdef ENABLE_AVX
#  define _ENABLE_AVX
# endif
# ifdef ENABLE_SSE4_1
#  define _ENABLE_SSE4_1
# endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1221
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1222
1223
1224
1225
1226
1227
1228
1229

#ifndef DISABLE_SIMD
# ifdef __AVX__
#  define _ENABLE_AVX
# endif
# ifdef __SSE4_1__
#  define _ENABLE_SSE4_1
# endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1230
1231
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1232
#if defined _ENABLE_AVX
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1233
#include <immintrin.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1234
#elif defined _ENABLE_SSE4_1
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1235
#include <smmintrin.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1236
1237
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1238
#if defined _ENABLE_AVX
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1239
1240

static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1241
void avx_encode_double_array_2byte(size_t datasize, 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1242
1243
				   unsigned char * restrict lGrib,
				   const double * restrict data, 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1244
				   double zref, double factor, size_t *gz) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1245
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1246
  size_t i, j, residual;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1247
  const double *dval = data;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1248
  __m128i *sgrib = (__m128i *) (lGrib+(*gz));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259

  const __m128i swap = _mm_set_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1);

  const __m256d c0 = _mm256_set1_pd(zref);
  const __m256d c1 = _mm256_set1_pd(factor);
  const __m256d c2 = _mm256_set1_pd(0.5);
  
  __m256d d0, d3, d2, d1;
  __m128i i0, i1, i2, i3;
  __m128i s0, s1;  

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1260
1261
1262
  residual = datasize % 16;

  for (i = 0; i < (datasize-residual); i += 16)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
    {
      (void) _mm_prefetch(dval+8, _MM_HINT_NTA);
      //_____________________________________________________________________________

      d0 = _mm256_loadu_pd (dval);
      d0 = _mm256_sub_pd (d0, c0);
      d0 = _mm256_mul_pd (d0, c1);
      d0 = _mm256_add_pd (d0, c2);

      i0 = _mm256_cvttpd_epi32 (d0);
      
      //_____________________________________________________________________________
      
      d1 = _mm256_loadu_pd (dval+4);
      d1 = _mm256_sub_pd (d1, c0);
      d1 = _mm256_mul_pd (d1, c1);
      d1 = _mm256_add_pd (d1, c2);
      
      i1 = _mm256_cvttpd_epi32 (d1);

      //_____________________________________________________________________________

      s0 = _mm_packus_epi32(i0, i1);
      s0 = _mm_shuffle_epi8 (s0, swap);
      (void) _mm_storeu_si128 (sgrib, s0);

      //_____________________________________________________________________________

      (void) _mm_prefetch(dval+16, _MM_HINT_NTA);

      //_____________________________________________________________________________
      
      d2 = _mm256_loadu_pd (dval+8);
      d2 = _mm256_sub_pd (d2, c0);
      d2 = _mm256_mul_pd (d2, c1);
      d2 = _mm256_add_pd (d2, c2);
      
      i2 = _mm256_cvttpd_epi32 (d2);

      //_____________________________________________________________________________
      
      d3 = _mm256_loadu_pd (dval+12);
      d3 = _mm256_sub_pd (d3, c0);
      d3 = _mm256_mul_pd (d3, c1);
      d3 = _mm256_add_pd (d3, c2);
      
      i3 = _mm256_cvttpd_epi32 (d3);

      //_____________________________________________________________________________

      s1 = _mm_packus_epi32(i2, i3);
      s1 = _mm_shuffle_epi8 (s1, swap);
      (void) _mm_storeu_si128 (sgrib+1, s1);

      //_____________________________________________________________________________
           
      dval += 16;
      sgrib += 2;
    }

  if (i != datasize)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
    {
      uint16_t ui16;
      for ( j = i; j < datasize; j++ )
	{
	  ui16 = (uint16_t) ((data[j] - zref) * factor + 0.5);
	  lGrib[*gz+2*j  ] = ui16 >>  8;
	  lGrib[*gz+2*j+1] = ui16;
	}
    }
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1334
1335
1336
1337
1338
  *gz += 2*datasize;

  return;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1339
#elif defined _ENABLE_SSE4_1
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1340
1341

static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1342
void sse41_encode_double_array_2byte(size_t datasize, 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1343
1344
				     unsigned char * restrict lGrib,
				     const double * restrict data, 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1345
				     double zref, double factor, size_t *gz) 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1346
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1347
  size_t i, j, residual;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1348
  const double *dval = data;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1349
  __m128i *sgrib = (__m128i *) (lGrib+(*gz));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360

  const __m128i swap = _mm_set_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1);

  const __m128d c0 = _mm_set1_pd(zref);
  const __m128d c1 = _mm_set1_pd(factor);
  const __m128d c2 = _mm_set1_pd(0.5);
  
  __m128d d0, d4, d3, d2, d1;
  __m128i i0, i1, i2, i3, i4;
  __m128i s0, s1;  

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1361
1362
1363
  residual = datasize % 16;

  for (i = 0; i < (datasize-residual); i += 16)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1364
1365
1366
1367
    {
      (void) _mm_prefetch(dval+8, _MM_HINT_NTA);
      //_____________________________________________________________________________

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1368
      d0 = _mm_loadu_pd (dval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1369
1370
1371
1372
      d0 = _mm_sub_pd (d0, c0);
      d0 = _mm_mul_pd (d0, c1);
      d0 = _mm_add_pd (d0, c2);
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1373
      d4 = _mm_loadu_pd (dval+2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1374
1375
1376
1377
1378
1379
1380
      d4 = _mm_sub_pd (d4, c0);
      d4 = _mm_mul_pd (d4, c1);
      d4 = _mm_add_pd (d4, c2);

      i0 = _mm_cvttpd_epi32 (d0);
      i4 = _mm_cvttpd_epi32 (d4);  
      i0 = _mm_unpacklo_epi64 (i0, i4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1381

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1382
1383
      //_____________________________________________________________________________
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1384
      d1 = _mm_loadu_pd (dval+4);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1385
1386
1387
1388
      d1 = _mm_sub_pd (d1, c0);
      d1 = _mm_mul_pd (d1, c1);
      d1 = _mm_add_pd (d1, c2);
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1389
      d4 = _mm_loadu_pd (dval+6);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1390
1391
1392
1393
1394
      d4 = _mm_sub_pd (d4, c0);
      d4 = _mm_mul_pd (d4, c1);
      d4 = _mm_add_pd (d4, c2);
      
      i1 = _mm_cvttpd_epi32 (d1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1395
      i4 = _mm_cvttpd_epi32 (d4);  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1396
1397
1398
1399
1400
1401
      i1 = _mm_unpacklo_epi64 (i1, i4);

      //_____________________________________________________________________________

      s0 = _mm_packus_epi32(i0, i1);
      s0 = _mm_shuffle_epi8 (s0, swap);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1402
      (void) _mm_storeu_si128 (sgrib, s0);