TNG version 1.7.3
[gromacs.git] / src / external / tng_io / src / compression / vals16.c
blob48186f675924fd5558146889d2c0907256265538
1 /* This code is part of the tng compression routines.
3 * Written by Daniel Spangberg
4 * Copyright (c) 2010, 2013, The GROMACS development team.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the Revised BSD License.
9 */
12 #include "../../include/compression/vals16.h"
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, const int nvals,
17 unsigned int *vals16, int *nvals16)
19 int i;
20 int j=0;
21 for (i=0; i<nvals; i++)
23 if (vals[i]<=0x7FFFU)
24 vals16[j++]=vals[i];
25 else
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
34 unsigned int lohi=(hi&0x7FFFU)|0x8000U;
35 unsigned int hihi=hi>>15;
36 vals16[j++]=lohi;
37 vals16[j++]=hihi;
41 #if 0
42 /* Test that things that detect that this is bad really works. */
43 vals16[0]=0;
44 #endif
45 *nvals16=j;
48 void Ptngc_comp_conv_from_vals16(unsigned int *vals16, const int nvals16,
49 unsigned int *vals, int *nvals)
51 int i=0;
52 int j=0;
53 while (i<nvals16)
55 if (vals16[i]<=0x7FFFU)
56 vals[j++]=vals16[i++];
57 else
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
65 unsigned int hihi=vals16[i++];
66 vals[j++]=(lo&0x7FFFU)|((hi&0x7FFFU)<<15)|(hihi<<30);
70 *nvals=j;