Add TNG writing and reading support
[alexxy/gromacs.git] / src / external / tng_io / src / compression / vals16.c
1 /* This code is part of the tng compression routines.
2  *
3  * Written by Daniel Spangberg
4  * Copyright (c) 2010, 2013, The GROMACS development team.
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the Revised BSD License.
9  */
10
11
12 #include "../../include/compression/vals16.h"
13
14 /* Coding 32 bit ints in sequences of 16 bit ints. Worst case
15    the output is 3*nvals long. */
16 void Ptngc_comp_conv_to_vals16(unsigned int *vals,int nvals,
17                          unsigned int *vals16, int *nvals16)
18 {
19   int i;
20   int j=0;
21   for (i=0; i<nvals; i++)
22     {
23       if (vals[i]<=0x7FFFU)
24         vals16[j++]=vals[i];
25       else
26         {
27           unsigned int lo=(vals[i]&0x7FFFU)|0x8000U;
28           unsigned int hi=vals[i]>>15;
29           vals16[j++]=lo;
30           if (hi<=0x7FFFU)
31             vals16[j++]=hi;
32           else
33             {
34               unsigned int lohi=(hi&0x7FFFU)|0x8000U;
35               unsigned int hihi=hi>>15;
36               vals16[j++]=lohi;
37               vals16[j++]=hihi;
38             }
39         }
40     }
41 #if 0
42   /* Test that things that detect that this is bad really works. */
43   vals16[0]=0;
44 #endif
45   *nvals16=j;
46 }
47
48 void Ptngc_comp_conv_from_vals16(unsigned int *vals16,int nvals16,
49                            unsigned int *vals, int *nvals)
50 {
51   int i=0;
52   int j=0;
53   while (i<nvals16)
54     {
55       if (vals16[i]<=0x7FFFU)
56         vals[j++]=vals16[i++];
57       else
58         {
59           unsigned int lo=vals16[i++];
60           unsigned int hi=vals16[i++];
61           if (hi<=0x7FFFU)
62             vals[j++]=(lo&0x7FFFU)|(hi<<15);
63           else
64             {
65               unsigned int hihi=vals16[i++];
66               vals[j++]=(lo&0x7FFFU)|((hi&0x7FFFU)<<15)|(hihi<<30);
67             }
68         }
69     }
70   *nvals=j;
71 }