~ubuntu-branches/ubuntu/quantal/pytables/quantal

« back to all changes in this revision

Viewing changes to src/typeconv.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2005-11-27 20:25:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127202534-l8jzyd8357krw40h
Tags: 1.1.1-1ubuntu1
* Sync with Debian:
  + Use python 2.4 as default

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***********************************************************************
 
2
 *
 
3
 *      License: BSD
 
4
 *      Created: December 21, 2004
 
5
 *      Author:  Ivan Vilata i Balaguer - reverse:com.carabos@ivilata
 
6
 *      Modified:
 
7
 *        Function inlining and some castings for 64-bit adressing
 
8
 *        Francesc Altet 2004-12-27
 
9
 *
 
10
 *      $Source: /cvsroot/pytables/pytables/src/typeconv.c,v $
 
11
 *      $Id: typeconv.c 1009 2005-06-15 13:39:15Z faltet $
 
12
 *
 
13
 ***********************************************************************/
 
14
 
 
15
/* Type conversion functions for PyTables types which are stored
 
16
 * with a different representation between Numarray and HDF5.
 
17
 */
 
18
 
 
19
#include "typeconv.h"
 
20
#include <math.h>
 
21
#include <assert.h>
 
22
 
 
23
 
 
24
#if (!defined _ISOC99_SOURCE && !defined __USE_ISOC99)
 
25
long int lround(double x)
 
26
{
 
27
  double trunx;
 
28
 
 
29
  if (x > 0.0) {
 
30
    trunx = floor(x);
 
31
    if (x - trunx >= 0.5)
 
32
      trunx += 1;
 
33
  } else {
 
34
    trunx = ceil(x);
 
35
    if (trunx - x >= 0.5)
 
36
      trunx -= 1;
 
37
  }
 
38
 
 
39
  return (long int)(trunx);
 
40
}
 
41
#endif  /* !_ISOC99_SOURCE && !__USE_ISOC99 */
 
42
 
 
43
 
 
44
void conv_float64_timeval32(void *base,
 
45
                            unsigned long byteoffset,
 
46
                            unsigned long bytestride,
 
47
                            PY_LONG_LONG nrecords,
 
48
                            unsigned long nelements,
 
49
                            int sense)
 
50
{
 
51
  PY_LONG_LONG  record;
 
52
  unsigned long  element, gapsize;
 
53
  double  *fieldbase;
 
54
  union {
 
55
    PY_LONG_LONG  i64;
 
56
    double  f64;
 
57
  } tv;
 
58
 
 
59
  assert(bytestride > 0);
 
60
  assert(nelements > 0);
 
61
 
 
62
  /* Byte distance from end of field to beginning of next field. */
 
63
  gapsize = bytestride - nelements * sizeof(double);
 
64
 
 
65
  fieldbase = (double *)((unsigned char *)(base) + byteoffset);
 
66
 
 
67
  for (record = 0;  record < nrecords;  record++) {
 
68
    for (element = 0;  element < nelements;  element++) {
 
69
      if (sense == 0) {
 
70
        /* Convert from float64 to timeval32. */
 
71
        tv.i64 = (((PY_LONG_LONG)(*fieldbase) << 32)
 
72
                  | (lround((*fieldbase - (int)(*fieldbase)) * 1e+6)
 
73
                     & 0x0ffffffff));
 
74
        *fieldbase = tv.f64;
 
75
      } else {
 
76
        /* Convert from timeval32 to float64. */
 
77
        tv.f64 = *fieldbase;
 
78
        /* the next computation is 64 bit-platforms aware */
 
79
        *fieldbase = 1e-6 * (int)tv.i64 + (tv.i64 >> 32);
 
80
      }
 
81
      fieldbase++;
 
82
    }
 
83
 
 
84
    fieldbase = (double *)((unsigned char *)(fieldbase) + gapsize);
 
85
 
 
86
    /* XXX: Need to check if this works on platforms which require
 
87
       64-bit data to be aligned.  ivb(2005-01-07) */
 
88
  }
 
89
 
 
90
  assert(fieldbase == (base + byteoffset + bytestride * nrecords));
 
91
}