7 // #include "basics.h" included later to avoid macro recursion for malloc
14 static void *Malloc (size_t n)
17 if (n == 0) return NULL;
18 p = (void*) malloc (n);
20 { fprintf (stderr,"Could not allocate %i bytes\n",n);
26 static void Free (void *p)
31 static void *Realloc (void *p, size_t n)
33 { if (p == NULL) return Malloc (n);
34 if (n == 0) { Free(p); return NULL; }
35 p = (void*) realloc (p,n);
37 { fprintf (stderr,"Could not allocate %i bytes\n",n);
45 // bits needed to represent a number between 0 and n
55 // returns e[p..p+len-1], assuming len <= W
57 uint bitread (uint *e, uint p, uint len)
63 { if (p) answ |= (*(e+1)) << (W-p);
65 else { if (p+len > W) answ |= (*(e+1)) << (W-p);
72 // writes e[p..p+len-1] = s, len <= W
74 void bitwrite (register uint *e, register uint p,
75 register uint len, register uint s)
79 { *e |= (*e & ((1<<p)-1)) | (s << p);
82 *e = (*e & ~((1<<p)-1)) | (s >> (W-p));
84 else { if (p+len <= W)
85 { *e = (*e & ~(((1<<len)-1)<<p)) | (s << p);
88 *e = (*e & ((1<<p)-1)) | (s << p);
90 *e = (*e & ~((1<<len)-1)) | (s >> (W-p));
93 // writes e[p..p+len-1] = 0
95 void bitzero2 (register uint *e, register uint p,
109 *e &= ~(((1<<len)-1)<<p);