summaryrefslogtreecommitdiff
path: root/src/compression/vals16.c
blob: c4ed635747c7dc223380511e872f3c08548223c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* This code is part of the tng compression routines.
 *
 * Written by Daniel Spangberg
 * Copyright (c) 2010, 2013, The GROMACS development team.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Revised BSD License.
 */


#include "../../include/compression/vals16.h"

/* Coding 32 bit ints in sequences of 16 bit ints. Worst case
   the output is 3*nvals long. */
void Ptngc_comp_conv_to_vals16(unsigned int *vals,int nvals,
                         unsigned int *vals16, int *nvals16)
{
  int i;
  int j=0;
  for (i=0; i<nvals; i++)
    {
      if (vals[i]<=0x7FFFU)
        vals16[j++]=vals[i];
      else
        {
          unsigned int lo=(vals[i]&0x7FFFU)|0x8000U;
          unsigned int hi=vals[i]>>15;
          vals16[j++]=lo;
          if (hi<=0x7FFFU)
            vals16[j++]=hi;
          else
            {
              unsigned int lohi=(hi&0x7FFFU)|0x8000U;
              unsigned int hihi=hi>>15;
              vals16[j++]=lohi;
              vals16[j++]=hihi;
            }
        }
    }
#if 0
  /* Test that things that detect that this is bad really works. */
  vals16[0]=0;
#endif
  *nvals16=j;
}

void Ptngc_comp_conv_from_vals16(unsigned int *vals16,int nvals16,
                           unsigned int *vals, int *nvals)
{
  int i=0;
  int j=0;
  while (i<nvals16)
    {
      if (vals16[i]<=0x7FFFU)
        vals[j++]=vals16[i++];
      else
        {
          unsigned int lo=vals16[i++];
          unsigned int hi=vals16[i++];
          if (hi<=0x7FFFU)
            vals[j++]=(lo&0x7FFFU)|(hi<<15);
          else
            {
              unsigned int hihi=vals16[i++];
              vals[j++]=(lo&0x7FFFU)|((hi&0x7FFFU)<<15)|(hihi<<30);
            }
        }
    }
  *nvals=j;
}
contact: Jan Huwald // Impressum