~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to .pc/changeset_r8510.diff/numpy/core/src/npymath/npy_math_private.h

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2010-10-07 10:19:13 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20101007101913-8b1kmt8ho4upcl9s
Tags: 1:1.4.1-5
* debian/patches/10_use_local_python.org_object.inv_sphinx.diff
  - fixed small typo in description
* debian/patches/changeset_r8364.diff
  - fix memory corruption (double free); thanks to Joseph Barillari for the
    report and to Michael Gilbert for pushing resolution; Closes: #581058

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * ====================================================
 
4
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 
7
 * Permission to use, copy, modify, and distribute this
 
8
 * software is freely granted, provided that this notice
 
9
 * is preserved.
 
10
 * ====================================================
 
11
 */
 
12
 
 
13
/*
 
14
 * from: @(#)fdlibm.h 5.1 93/09/24
 
15
 * $FreeBSD$
 
16
 */
 
17
 
 
18
#ifndef _NPY_MATH_PRIVATE_H_
 
19
#define _NPY_MATH_PRIVATE_H_
 
20
 
 
21
#include <Python.h>
 
22
#include <math.h>
 
23
 
 
24
#include "npy_config.h"
 
25
#include "npy_fpmath.h"
 
26
 
 
27
#include "numpy/npy_math.h"
 
28
#include "numpy/npy_cpu.h"
 
29
#include "numpy/npy_endian.h"
 
30
#include "numpy/npy_common.h"
 
31
 
 
32
/*
 
33
 * The original fdlibm code used statements like:
 
34
 *      n0 = ((*(int*)&one)>>29)^1;             * index of high word *
 
35
 *      ix0 = *(n0+(int*)&x);                   * high word of x *
 
36
 *      ix1 = *((1-n0)+(int*)&x);               * low word of x *
 
37
 * to dig two 32 bit words out of the 64 bit IEEE floating point
 
38
 * value.  That is non-ANSI, and, moreover, the gcc instruction
 
39
 * scheduler gets it wrong.  We instead use the following macros.
 
40
 * Unlike the original code, we determine the endianness at compile
 
41
 * time, not at run time; I don't see much benefit to selecting
 
42
 * endianness at run time.
 
43
 */
 
44
 
 
45
/*
 
46
 * A union which permits us to convert between a double and two 32 bit
 
47
 * ints.
 
48
 */
 
49
 
 
50
/* XXX: not really, but we already make this assumption elsewhere. Will have to
 
51
 * fix this at some point */
 
52
#define IEEE_WORD_ORDER NPY_BYTE_ORDER
 
53
 
 
54
#if IEEE_WORD_ORDER == NPY_BIG_ENDIAN
 
55
 
 
56
typedef union
 
57
{
 
58
  double value;
 
59
  struct
 
60
  {
 
61
    npy_uint32 msw;
 
62
    npy_uint32 lsw;
 
63
  } parts;
 
64
} ieee_double_shape_type;
 
65
 
 
66
#endif
 
67
 
 
68
#if IEEE_WORD_ORDER == NPY_LITTLE_ENDIAN
 
69
 
 
70
typedef union
 
71
{
 
72
  double value;
 
73
  struct
 
74
  {
 
75
    npy_uint32 lsw;
 
76
    npy_uint32 msw;
 
77
  } parts;
 
78
} ieee_double_shape_type;
 
79
 
 
80
#endif
 
81
 
 
82
/* Get two 32 bit ints from a double.  */
 
83
 
 
84
#define EXTRACT_WORDS(ix0,ix1,d)                                \
 
85
do {                                                            \
 
86
  ieee_double_shape_type ew_u;                                  \
 
87
  ew_u.value = (d);                                             \
 
88
  (ix0) = ew_u.parts.msw;                                       \
 
89
  (ix1) = ew_u.parts.lsw;                                       \
 
90
} while (0)
 
91
 
 
92
/* Get the more significant 32 bit int from a double.  */
 
93
 
 
94
#define GET_HIGH_WORD(i,d)                                      \
 
95
do {                                                            \
 
96
  ieee_double_shape_type gh_u;                                  \
 
97
  gh_u.value = (d);                                             \
 
98
  (i) = gh_u.parts.msw;                                         \
 
99
} while (0)
 
100
 
 
101
/* Get the less significant 32 bit int from a double.  */
 
102
 
 
103
#define GET_LOW_WORD(i,d)                                       \
 
104
do {                                                            \
 
105
  ieee_double_shape_type gl_u;                                  \
 
106
  gl_u.value = (d);                                             \
 
107
  (i) = gl_u.parts.lsw;                                         \
 
108
} while (0)
 
109
 
 
110
/* Set the more significant 32 bits of a double from an int.  */
 
111
 
 
112
#define SET_HIGH_WORD(d,v)                                      \
 
113
do {                                                            \
 
114
  ieee_double_shape_type sh_u;                                  \
 
115
  sh_u.value = (d);                                             \
 
116
  sh_u.parts.msw = (v);                                         \
 
117
  (d) = sh_u.value;                                             \
 
118
} while (0)
 
119
 
 
120
/* Set the less significant 32 bits of a double from an int.  */
 
121
 
 
122
#define SET_LOW_WORD(d,v)                                       \
 
123
do {                                                            \
 
124
  ieee_double_shape_type sl_u;                                  \
 
125
  sl_u.value = (d);                                             \
 
126
  sl_u.parts.lsw = (v);                                         \
 
127
  (d) = sl_u.value;                                             \
 
128
} while (0)
 
129
 
 
130
/* Set a double from two 32 bit ints.  */
 
131
 
 
132
#define INSERT_WORDS(d,ix0,ix1)                                 \
 
133
do {                                                            \
 
134
  ieee_double_shape_type iw_u;                                  \
 
135
  iw_u.parts.msw = (ix0);                                       \
 
136
  iw_u.parts.lsw = (ix1);                                       \
 
137
  (d) = iw_u.value;                                             \
 
138
} while (0)
 
139
 
 
140
/*
 
141
 * A union which permits us to convert between a float and a 32 bit
 
142
 * int.
 
143
 */
 
144
 
 
145
typedef union
 
146
{
 
147
  float value;
 
148
  /* FIXME: Assumes 32 bit int.  */
 
149
  npy_uint32 word;
 
150
} ieee_float_shape_type;
 
151
 
 
152
/* Get a 32 bit int from a float.  */
 
153
 
 
154
#define GET_FLOAT_WORD(i,d)                                     \
 
155
do {                                                            \
 
156
  ieee_float_shape_type gf_u;                                   \
 
157
  gf_u.value = (d);                                             \
 
158
  (i) = gf_u.word;                                              \
 
159
} while (0)
 
160
 
 
161
/* Set a float from a 32 bit int.  */
 
162
 
 
163
#define SET_FLOAT_WORD(d,i)                                     \
 
164
do {                                                            \
 
165
  ieee_float_shape_type sf_u;                                   \
 
166
  sf_u.word = (i);                                              \
 
167
  (d) = sf_u.value;                                             \
 
168
} while (0)
 
169
 
 
170
#ifdef NPY_USE_C99_COMPLEX
 
171
#include <complex.h>
 
172
#endif
 
173
 
 
174
/*
 
175
 * Long double support
 
176
 */
 
177
#if defined(HAVE_LDOUBLE_INTEL_EXTENDED_12_BYTES_LE)
 
178
    /*
 
179
     * Intel extended 80 bits precision. Bit representation is
 
180
     *          |  junk  |     s  |eeeeeeeeeeeeeee|mmmmmmmm................mmmmmmm|
 
181
     *          | 16 bits|  1 bit |    15 bits    |            64 bits            |
 
182
     *          |             a[2]                |     a[1]     |    a[0]        |
 
183
     *
 
184
     * 16 stronger bits of a[2] are junk
 
185
     */
 
186
    typedef npy_uint32 IEEEl2bitsrep_part;
 
187
 
 
188
/* my machine */
 
189
 
 
190
    union IEEEl2bitsrep {
 
191
        npy_longdouble     e;
 
192
        IEEEl2bitsrep_part a[3];
 
193
    };
 
194
 
 
195
    #define LDBL_MANL_INDEX     0
 
196
    #define LDBL_MANL_MASK      0xFFFFFFFF
 
197
    #define LDBL_MANL_SHIFT     0
 
198
 
 
199
    #define LDBL_MANH_INDEX     1
 
200
    #define LDBL_MANH_MASK      0xFFFFFFFF
 
201
    #define LDBL_MANH_SHIFT     0
 
202
 
 
203
    #define LDBL_EXP_INDEX      2
 
204
    #define LDBL_EXP_MASK       0x7FFF
 
205
    #define LDBL_EXP_SHIFT      0
 
206
 
 
207
    #define LDBL_SIGN_INDEX     2
 
208
    #define LDBL_SIGN_MASK      0x8000
 
209
    #define LDBL_SIGN_SHIFT     15
 
210
 
 
211
    #define LDBL_NBIT           0x80000000
 
212
 
 
213
    typedef npy_uint32 ldouble_man_t;
 
214
    typedef npy_uint32 ldouble_exp_t;
 
215
    typedef npy_uint32 ldouble_sign_t;
 
216
#elif defined(HAVE_LDOUBLE_INTEL_EXTENDED_16_BYTES_LE)
 
217
    /*
 
218
     * Intel extended 80 bits precision, 16 bytes alignment.. Bit representation is
 
219
     *          |  junk  |     s  |eeeeeeeeeeeeeee|mmmmmmmm................mmmmmmm|
 
220
     *          | 16 bits|  1 bit |    15 bits    |            64 bits            |
 
221
     *          |             a[2]                |     a[1]     |    a[0]        |
 
222
     *
 
223
     * a[3] and 16 stronger bits of a[2] are junk
 
224
     */
 
225
    typedef npy_uint32 IEEEl2bitsrep_part;
 
226
 
 
227
    union IEEEl2bitsrep {
 
228
        npy_longdouble     e;
 
229
        IEEEl2bitsrep_part a[4];
 
230
    };
 
231
 
 
232
    #define LDBL_MANL_INDEX     0
 
233
    #define LDBL_MANL_MASK      0xFFFFFFFF
 
234
    #define LDBL_MANL_SHIFT     0
 
235
 
 
236
    #define LDBL_MANH_INDEX     1
 
237
    #define LDBL_MANH_MASK      0xFFFFFFFF
 
238
    #define LDBL_MANH_SHIFT     0
 
239
 
 
240
    #define LDBL_EXP_INDEX      2
 
241
    #define LDBL_EXP_MASK       0x7FFF
 
242
    #define LDBL_EXP_SHIFT      0
 
243
 
 
244
    #define LDBL_SIGN_INDEX     2
 
245
    #define LDBL_SIGN_MASK      0x8000
 
246
    #define LDBL_SIGN_SHIFT     15
 
247
 
 
248
    #define LDBL_NBIT           0x800000000
 
249
 
 
250
    typedef npy_uint32 ldouble_man_t;
 
251
    typedef npy_uint32 ldouble_exp_t;
 
252
    typedef npy_uint32 ldouble_sign_t;
 
253
#elif defined(HAVE_LDOUBLE_IEEE_DOUBLE_16_BYTES_BE) || \
 
254
      defined(HAVE_LDOUBLE_IEEE_DOUBLE_BE)
 
255
    /* 64 bits IEEE double precision aligned on 16 bytes: used by ppc arch on
 
256
     * Mac OS X */
 
257
 
 
258
    /*
 
259
     * IEEE double precision. Bit representation is
 
260
     *          |  s  |eeeeeeeeeee|mmmmmmmm................mmmmmmm|
 
261
     *          |1 bit|  11 bits  |            52 bits            |
 
262
     *          |          a[0]         |         a[1]            |
 
263
     */
 
264
    typedef npy_uint32 IEEEl2bitsrep_part;
 
265
 
 
266
    union IEEEl2bitsrep {
 
267
        npy_longdouble     e;
 
268
        IEEEl2bitsrep_part a[2];
 
269
    };
 
270
 
 
271
    #define LDBL_MANL_INDEX     1
 
272
    #define LDBL_MANL_MASK      0xFFFFFFFF
 
273
    #define LDBL_MANL_SHIFT     0
 
274
 
 
275
    #define LDBL_MANH_INDEX     0
 
276
    #define LDBL_MANH_MASK      0x000FFFFF
 
277
    #define LDBL_MANH_SHIFT     0
 
278
 
 
279
    #define LDBL_EXP_INDEX      0
 
280
    #define LDBL_EXP_MASK       0x7FF00000
 
281
    #define LDBL_EXP_SHIFT      20
 
282
 
 
283
    #define LDBL_SIGN_INDEX     0
 
284
    #define LDBL_SIGN_MASK      0x80000000
 
285
    #define LDBL_SIGN_SHIFT     31
 
286
 
 
287
    #define LDBL_NBIT           0
 
288
 
 
289
    typedef npy_uint32 ldouble_man_t;
 
290
    typedef npy_uint32 ldouble_exp_t;
 
291
    typedef npy_uint32 ldouble_sign_t;
 
292
#elif defined(HAVE_LDOUBLE_IEEE_DOUBLE_LE)
 
293
    /* 64 bits IEEE double precision, Little Endian. */
 
294
 
 
295
    /*
 
296
     * IEEE double precision. Bit representation is
 
297
     *          |  s  |eeeeeeeeeee|mmmmmmmm................mmmmmmm|
 
298
     *          |1 bit|  11 bits  |            52 bits            |
 
299
     *          |          a[1]         |         a[0]            |
 
300
     */
 
301
    typedef npy_uint32 IEEEl2bitsrep_part;
 
302
 
 
303
    union IEEEl2bitsrep {
 
304
        npy_longdouble     e;
 
305
        IEEEl2bitsrep_part a[2];
 
306
    };
 
307
 
 
308
    #define LDBL_MANL_INDEX     0
 
309
    #define LDBL_MANL_MASK      0xFFFFFFFF
 
310
    #define LDBL_MANL_SHIFT     0
 
311
 
 
312
    #define LDBL_MANH_INDEX     1
 
313
    #define LDBL_MANH_MASK      0x000FFFFF
 
314
    #define LDBL_MANH_SHIFT     0
 
315
 
 
316
    #define LDBL_EXP_INDEX      1
 
317
    #define LDBL_EXP_MASK       0x7FF00000
 
318
    #define LDBL_EXP_SHIFT      20
 
319
 
 
320
    #define LDBL_SIGN_INDEX     1
 
321
    #define LDBL_SIGN_MASK      0x80000000
 
322
    #define LDBL_SIGN_SHIFT     31
 
323
 
 
324
    #define LDBL_NBIT           0x00000080
 
325
 
 
326
    typedef npy_uint32 ldouble_man_t;
 
327
    typedef npy_uint32 ldouble_exp_t;
 
328
    typedef npy_uint32 ldouble_sign_t;
 
329
#elif defined(HAVE_LDOUBLE_IEEE_QUAD_BE)
 
330
    /*
 
331
     * IEEE quad precision, Big Endian. Bit representation is
 
332
     *          |  s  |eeeeeeeeeee|mmmmmmmm................mmmmmmm|
 
333
     *          |1 bit|  15 bits  |            112 bits           |
 
334
     *          |          a[0]         |           a[1]          |
 
335
     */
 
336
    typedef npy_uint64 IEEEl2bitsrep_part;
 
337
 
 
338
    union IEEEl2bitsrep {
 
339
        npy_longdouble     e;
 
340
        IEEEl2bitsrep_part a[2];
 
341
    };
 
342
 
 
343
    #define LDBL_MANL_INDEX     1
 
344
    #define LDBL_MANL_MASK      0xFFFFFFFFFFFFFFFF
 
345
    #define LDBL_MANL_SHIFT     0
 
346
 
 
347
    #define LDBL_MANH_INDEX     0
 
348
    #define LDBL_MANH_MASK      0x0000FFFFFFFFFFFF
 
349
    #define LDBL_MANH_SHIFT     0
 
350
 
 
351
    #define LDBL_EXP_INDEX      0
 
352
    #define LDBL_EXP_MASK       0x7FFF000000000000
 
353
    #define LDBL_EXP_SHIFT      48
 
354
 
 
355
    #define LDBL_SIGN_INDEX     0
 
356
    #define LDBL_SIGN_MASK      0x8000000000000000
 
357
    #define LDBL_SIGN_SHIFT     63
 
358
 
 
359
    #define LDBL_NBIT           0
 
360
 
 
361
    typedef npy_uint64 ldouble_man_t;
 
362
    typedef npy_uint64 ldouble_exp_t;
 
363
    typedef npy_uint32 ldouble_sign_t;
 
364
#endif
 
365
 
 
366
/* Get the sign bit of x. x should be of type IEEEl2bitsrep */
 
367
#define GET_LDOUBLE_SIGN(x) \
 
368
    (((x).a[LDBL_SIGN_INDEX] & LDBL_SIGN_MASK) >> LDBL_SIGN_SHIFT)
 
369
 
 
370
/* Set the sign bit of x to v. x should be of type IEEEl2bitsrep */
 
371
#define SET_LDOUBLE_SIGN(x, v) \
 
372
  ((x).a[LDBL_SIGN_INDEX] =                                             \
 
373
   ((x).a[LDBL_SIGN_INDEX] & ~LDBL_SIGN_MASK) |                         \
 
374
   (((IEEEl2bitsrep_part)(v) << LDBL_SIGN_SHIFT) & LDBL_SIGN_MASK))
 
375
 
 
376
/* Get the exp bits of x. x should be of type IEEEl2bitsrep */
 
377
#define GET_LDOUBLE_EXP(x) \
 
378
    (((x).a[LDBL_EXP_INDEX] & LDBL_EXP_MASK) >> LDBL_EXP_SHIFT)
 
379
 
 
380
/* Set the exp bit of x to v. x should be of type IEEEl2bitsrep */
 
381
#define SET_LDOUBLE_EXP(x, v) \
 
382
  ((x).a[LDBL_EXP_INDEX] =                                              \
 
383
   ((x).a[LDBL_EXP_INDEX] & ~LDBL_EXP_MASK) |                           \
 
384
   (((IEEEl2bitsrep_part)(v) << LDBL_EXP_SHIFT) & LDBL_EXP_MASK))
 
385
 
 
386
/* Get the manl bits of x. x should be of type IEEEl2bitsrep */
 
387
#define GET_LDOUBLE_MANL(x) \
 
388
    (((x).a[LDBL_MANL_INDEX] & LDBL_MANL_MASK) >> LDBL_MANL_SHIFT)
 
389
 
 
390
/* Set the manl bit of x to v. x should be of type IEEEl2bitsrep */
 
391
#define SET_LDOUBLE_MANL(x, v) \
 
392
  ((x).a[LDBL_MANL_INDEX] =                                             \
 
393
   ((x).a[LDBL_MANL_INDEX] & ~LDBL_MANL_MASK) |                         \
 
394
   (((IEEEl2bitsrep_part)(v) << LDBL_MANL_SHIFT) & LDBL_MANL_MASK))
 
395
 
 
396
/* Get the manh bits of x. x should be of type IEEEl2bitsrep */
 
397
#define GET_LDOUBLE_MANH(x) \
 
398
    (((x).a[LDBL_MANH_INDEX] & LDBL_MANH_MASK) >> LDBL_MANH_SHIFT)
 
399
 
 
400
/* Set the manh bit of x to v. x should be of type IEEEl2bitsrep */
 
401
#define SET_LDOUBLE_MANH(x, v) \
 
402
    ((x).a[LDBL_MANH_INDEX] = \
 
403
     ((x).a[LDBL_MANH_INDEX] & ~LDBL_MANH_MASK) |                       \
 
404
     (((IEEEl2bitsrep_part)(v) << LDBL_MANH_SHIFT) & LDBL_MANH_MASK))
 
405
 
 
406
/*
 
407
 * Those unions are used to convert a pointer of npy_cdouble to native C99
 
408
 * complex or our own complex type independently on whether C99 complex
 
409
 * support is available
 
410
 */
 
411
#ifdef NPY_USE_C99_COMPLEX
 
412
typedef union {
 
413
        npy_cdouble npy_z;
 
414
        complex double c99_z;
 
415
} __npy_cdouble_to_c99_cast;
 
416
 
 
417
typedef union {
 
418
        npy_cfloat npy_z;
 
419
        complex float c99_z;
 
420
} __npy_cfloat_to_c99_cast;
 
421
 
 
422
typedef union {
 
423
        npy_clongdouble npy_z;
 
424
        complex long double c99_z;
 
425
} __npy_clongdouble_to_c99_cast;
 
426
#else
 
427
typedef union {
 
428
        npy_cdouble npy_z;
 
429
        npy_cdouble c99_z;
 
430
} __npy_cdouble_to_c99_cast;
 
431
 
 
432
typedef union {
 
433
        npy_cfloat npy_z;
 
434
        npy_cfloat c99_z;
 
435
} __npy_cfloat_to_c99_cast;
 
436
 
 
437
typedef union {
 
438
        npy_clongdouble npy_z;
 
439
        npy_clongdouble c99_z;
 
440
} __npy_clongdouble_to_c99_cast;
 
441
#endif
 
442
 
 
443
#endif /* !_NPY_MATH_PRIVATE_H_ */