Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Typedefs | Functions | Variables
hilbert.c File Reference

Go to the source code of this file.

Typedefs

typedef unsigned int byte
 

Functions

static void calctables (int n)
 
void hilbert_i2c (int n, int m, long int r, a)
 
void hilbert_c2i (int n, int m, a, long int *r)
 

Variables

static char rcsid [] = "$Header: /l/spencer/src/urt/lib/RCS/hilbert.c,v 3.0.1.1 1992/04/30 14:07:11 spencer Exp $"
 
static int nbits = 0
 
static byte p_to_s [512]
 
static byte s_to_p [512]
 
static byte p_to_J [512]
 
static byte bit [9]
 
static byte circshift [512][9]
 
static byte parity [512]
 
static byte bitof [512][9]
 

Typedef Documentation

typedef unsigned int byte

Definition at line 47 of file hilbert.c.

Function Documentation

static void calctables ( int  n)
static

Definition at line 62 of file hilbert.c.

64 {
65  register int i, b;
66  int two_n = 1 << n;
67 
68  if ( nbits == n )
69  return;
70 
71  nbits = n;
72  /* bit array is easy. */
73  for ( b = 0; b < n; b++ )
74  bit[b] = 1 << (n - b - 1);
75 
76  /* Next, do bitof. */
77  for ( i = 0; i < two_n; i++ )
78  for ( b = 0; b < n; b++ )
79  bitof[i][b] = (i & bit[b]) ? 1 : 0;
80 
81  /* circshift is independent of the others. */
82  for ( i = 0; i < two_n; i++ )
83  for ( b = 0; b < n; b++ )
84  circshift[i][b] = (i >> (b)) |
85  ((i << (n - b)) & (two_n - 1));
86 
87  /* So is parity. */
88  parity[0] = 0;
89  for ( i = 1, b = 1; i < two_n; i++ )
90  {
91  /* Parity of i is opposite of the number you get when you
92  * knock the high order bit off of i.
93  */
94  if ( i == b * 2 )
95  b *= 2;
96  parity[i] = !parity[i - b];
97  }
98 
99  /* Now do p_to_s, s_to_p, and p_to_J. */
100  for ( i = 0; i < two_n; i++ )
101  {
102  int s;
103 
104  s = i & bit[0];
105  for ( b = 1; b < n; b++ )
106  if ( bitof[i][b] ^ bitof[i][b-1] )
107  s |= bit[b];
108  p_to_s[i] = s;
109  s_to_p[s] = i;
110 
111  p_to_J[i] = n - 1;
112  for ( b = 0; b < n; b++ )
113  if ( bitof[i][b] != bitof[i][n-1] )
114  p_to_J[i] = b;
115  }
116 }
static byte circshift[512][9]
Definition: hilbert.c:52
static byte p_to_J[512]
Definition: hilbert.c:52
_urt_stack * s
Definition: rleClock.c:919
static unsigned char b
Definition: getami.c:692
static byte s_to_p[512]
Definition: hilbert.c:52
static byte bitof[512][9]
Definition: hilbert.c:52
static byte bit[9]
Definition: hilbert.c:52
int i
Definition: rletorla.c:82
static byte parity[512]
Definition: hilbert.c:52
static int nbits
Definition: hilbert.c:49
static byte p_to_s[512]
Definition: hilbert.c:52
void hilbert_c2i ( int  n,
int  m,
,
long int r 
)

Definition at line 245 of file hilbert.c.

250 {
251  byte rho[9], J, sigma, tau,
252  sigmaT, tauT, tauT1 = 0, omega, omega1 = 0, alpha[9];
253  register int i, b;
254  int Jsum;
255  long int rl;
256 
257  calctables(n);
258 
259  /* Unpack the coordinates into alpha[i]. */
260  /* First, zero out the alphas. */
261  switch ( m ) {
262  case 9: alpha[8] = 0;
263  case 8: alpha[7] = 0;
264  case 7: alpha[6] = 0;
265  case 6: alpha[5] = 0;
266  case 5: alpha[4] = 0;
267  case 4: alpha[3] = 0;
268  case 3: alpha[2] = 0;
269  case 2: alpha[1] = 0;
270  case 1: alpha[0] = 0;
271  }
272 
273  /* The loop that unpacks bits of a[b] into alpha[i] has been unrolled.
274  * The high-order bit of a[b] has to go into alpha[0], so we pre-shift
275  * a[b] so that its high-order bit is always in the 0x100 position.
276  */
277  for ( b = 0; b < n; b++ )
278  {
279  register int bt = bit[b], t = a[b] << (9 - m);
280 
281  switch (m)
282  {
283  case 9: if ( t & 0x01 ) alpha[8] |= bt;
284  case 8: if ( t & 0x02 ) alpha[7] |= bt;
285  case 7: if ( t & 0x04 ) alpha[6] |= bt;
286  case 6: if ( t & 0x08 ) alpha[5] |= bt;
287  case 5: if ( t & 0x10 ) alpha[4] |= bt;
288  case 4: if ( t & 0x20 ) alpha[3] |= bt;
289  case 3: if ( t & 0x40 ) alpha[2] |= bt;
290  case 2: if ( t & 0x80 ) alpha[1] |= bt;
291  case 1: if ( t & 0x100 ) alpha[0] |= bt;
292  }
293  }
294 
295  Jsum = 0;
296  for ( i = 0; i < m; i++ )
297  {
298  /* Compute omega[i] = omega[i-1] xor tauT[i-1]. */
299  if ( i == 0 )
300  omega = 0;
301  else
302  omega = omega1 ^ tauT1;
303 
304  sigmaT = alpha[i] ^ omega;
305  /* sigma[i] is the left circular shift of sigmaT[i]. */
306  if ( Jsum != 0 )
307  sigma = circshift[sigmaT][n - Jsum];
308  else
309  sigma = sigmaT;
310 
311  rho[i] = s_to_p[sigma];
312 
313  /* Now we can get the principle position. */
314  J = p_to_J[rho[i]];
315 
316  /* And compute tau[i] and tauT[i]. */
317  /* tau[i] complements low bit of sigma[i], and bit at J[i] if
318  * necessary to make even parity.
319  */
320  tau = sigma ^ 1;
321  if ( parity[tau] )
322  tau ^= bit[J];
323 
324  /* tauT[i] is right circular shift of tau[i]. */
325  if ( Jsum != 0 )
326  tauT = circshift[tau][Jsum];
327  else
328  tauT = tau;
329  Jsum += J;
330  if ( Jsum >= n )
331  Jsum -= n;
332 
333  /* Carry forth the "i-1" values. */
334  tauT1 = tauT;
335  omega1 = omega;
336  }
337 
338  /* Pack rho values into r. */
339  rl = 0;
340  for ( i = 0; i < m; i++ )
341  rl = (rl << n) | rho[i];
342  *r = rl;
343 }
static byte circshift[512][9]
Definition: hilbert.c:52
static byte p_to_J[512]
Definition: hilbert.c:52
static unsigned char r
Definition: getami.c:692
static unsigned char b
Definition: getami.c:692
static byte s_to_p[512]
Definition: hilbert.c:52
static byte bit[9]
Definition: hilbert.c:52
unsigned int byte
Definition: hilbert.c:47
int i
Definition: rletorla.c:82
static byte parity[512]
Definition: hilbert.c:52
static void calctables(int n)
Definition: hilbert.c:62
void hilbert_i2c ( int  n,
int  m,
long int  r,
 
)

Definition at line 136 of file hilbert.c.

141 {
142  byte rho[9], rh, J, sigma, tau,
143  sigmaT, tauT, tauT1 = 0, omega, omega1 = 0, alpha[9];
144  register int i, b;
145  int Jsum;
146 
147  /* Initialize bit twiddle tables. */
148  calctables(n);
149 
150  /* Distribute bits from r into rho. */
151  for ( i = m - 1; i >= 0; i-- )
152  {
153  rho[i] = r & ((1 << n) - 1);
154  r >>= n;
155  }
156 
157  /* Loop over bytes. */
158  Jsum = 0;
159  for ( i = 0; i < m; i++ )
160  {
161  rh = rho[i];
162  /* J[i] is principle position of rho[i]. */
163  J = p_to_J[rh];
164 
165  /* sigma[i] is derived from rho[i] by exclusive-oring adjacent bits. */
166  sigma = p_to_s[rh];
167 
168  /* tau[i] complements low bit of sigma[i], and bit at J[i] if
169  * necessary to make even parity.
170  */
171  tau = sigma ^ 1;
172  if ( parity[tau] )
173  tau ^= bit[J];
174 
175  /* sigmaT[i] is circular shift of sigma[i] by sum of J[0..i-1] */
176  /* tauT[i] is same circular shift of tau[i]. */
177  if ( Jsum > 0 )
178  {
179  sigmaT = circshift[sigma][Jsum];
180  tauT = circshift[tau][Jsum];
181  }
182  else
183  {
184  sigmaT = sigma;
185  tauT = tau;
186  }
187 
188  Jsum += J;
189  if ( Jsum >= n )
190  Jsum -= n;
191 
192  /* omega[i] is xor of omega[i-1] and tauT[i-1]. */
193  if ( i == 0 )
194  omega = 0;
195  else
196  omega = omega1 ^ tauT1;
197  omega1 = omega;
198  tauT1 = tauT;
199 
200  /* alpha[i] is xor of omega[i] and sigmaT[i] */
201  alpha[i] = omega ^ sigmaT;
202  }
203 
204  /* Build coordinates by taking bits from alphas. */
205  for ( b = 0; b < n; b++ )
206  {
207  register int ab, bt;
208  ab = 0;
209  bt = bit[b];
210  /* Unroll the loop that stuffs bits into ab.
211  * The result is shifted left by 9-m bits.
212  */
213  switch( m )
214  {
215  case 9: if ( alpha[8] & bt) ab |= 0x01;
216  case 8: if ( alpha[7] & bt) ab |= 0x02;
217  case 7: if ( alpha[6] & bt) ab |= 0x04;
218  case 6: if ( alpha[5] & bt) ab |= 0x08;
219  case 5: if ( alpha[4] & bt) ab |= 0x10;
220  case 4: if ( alpha[3] & bt) ab |= 0x20;
221  case 3: if ( alpha[2] & bt) ab |= 0x40;
222  case 2: if ( alpha[1] & bt) ab |= 0x80;
223  case 1: if ( alpha[0] & bt) ab |= 0x100;
224  }
225  a[b] = ab >> (9 - m);
226  }
227 }
static byte circshift[512][9]
Definition: hilbert.c:52
static byte p_to_J[512]
Definition: hilbert.c:52
static unsigned char r
Definition: getami.c:692
static unsigned char b
Definition: getami.c:692
static byte bit[9]
Definition: hilbert.c:52
unsigned int byte
Definition: hilbert.c:47
int i
Definition: rletorla.c:82
static byte parity[512]
Definition: hilbert.c:52
static void calctables(int n)
Definition: hilbert.c:62
static byte p_to_s[512]
Definition: hilbert.c:52

Variable Documentation

byte bit[9]
static

Definition at line 52 of file hilbert.c.

byte bitof[512][9]
static

Definition at line 52 of file hilbert.c.

byte circshift[512][9]
static

Definition at line 52 of file hilbert.c.

int nbits = 0
static

Definition at line 49 of file hilbert.c.

byte p_to_J[512]
static

Definition at line 52 of file hilbert.c.

byte p_to_s[512]
static

Definition at line 52 of file hilbert.c.

byte parity[512]
static

Definition at line 52 of file hilbert.c.

char rcsid[] = "$Header: /l/spencer/src/urt/lib/RCS/hilbert.c,v 3.0.1.1 1992/04/30 14:07:11 spencer Exp $"
static

Definition at line 27 of file hilbert.c.

byte s_to_p[512]
static

Definition at line 52 of file hilbert.c.