1 /****************************************************************************
2     Copyright (C) 1987-2015 by Jeffery P. Hansen
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 ****************************************************************************/
18 #include <assert.h>
19 #include <ctype.h>
20 #include "thyme.h"
21 
22 #define DEBUG_VALUE 0
23 
24 #define isBDig(x)	((x) == SYM_ONE || (x) == SYM_ZERO)
25 
26 #define ABS(n)		((n) < 0 ? -(n) : (n))
27 
28 static char *sym_table_lc = "*01@zlhx";
29 static char *sym_table = "*01@zLHx";
30 static int sym_table_len = 8;
31 
32 static int value_print_showbits = 1;
33 
34 static struct value_fl *value_freePool = 0;
35 
36 /*
37   Allocate a temporary Value object
38  */
new_Value(int nbits)39 Value *new_Value(int nbits)
40 {
41   struct value_fl *S;
42   static int fl_count = 0;
43   static int ma_count = 0;
44 
45   if (!value_freePool) {
46     S = (struct value_fl*) malloc(sizeof(struct value_fl));
47     if (nbits > 0)
48       Value_init((Value*)S,nbits);
49     else
50       Value_init((Value*)S,1);
51 #if DEBUG_VALUE_MEMMGR
52     S->state.status = 1;
53 #endif
54     ma_count++;
55   } else {
56     S = value_freePool;
57     value_freePool = value_freePool->next;
58     fl_count++;
59 #if DEBUG_VALUE_MEMMGR
60     if (S->state.status != -1) {
61       vgio_echo("bad state object in alloc_State() (status=%d).\n",S->state.status);
62       assert(0);
63     }
64     S->state.status = 2;
65 #endif
66     if (nbits > 0)
67       Value_reinit((Value*)S,nbits);
68   }
69 
70   S->state.flags = SF_NONE;
71   S->state.permFlags = 0;
72 
73 #if DEBUG_VALUE
74   if (((fl_count+ma_count) % 10000) == 0) {
75     vgio_echo("value: fl:%d ma:%d   (%f)\n",fl_count,ma_count,(double)ma_count/(double)fl_count);
76     fflush(stdout);
77   }
78 #endif
79 
80   return (Value*)S;
81 }
82 
delete_Value(Value * S)83 void delete_Value(Value *S)
84 {
85   struct value_fl *flS = (struct value_fl *)S;
86   flS->next = value_freePool;
87   value_freePool = flS;
88 
89 #if DEBUG_VALUE_MEMMGR
90   if (S->status != 1 && S->status != 2) {
91     vgio_echo("bad state object in free_State() (status=%d).\n",S->status);
92     assert(0);
93   }
94   S->status = -1;
95 #endif
96 }
97 
98 /*****************************************************************************
99  *
100  * Make a value stored in the one[] array consistant in the other arrays.
101  *
102  * Parameter:
103  *     S		Value to be "normalized"
104  *
105  *****************************************************************************/
Value_normalize(Value * r)106 void Value_normalize(Value *r)
107 {
108   int wc = SSNUMWORDS(r->nbits);
109   int i;
110 
111   for (i = 0;i < wc;i++) {
112     r->zero[i] = ~r->one[i];
113     r->flt[i]  = 0;
114   }
115 }
116 
117 /*
118          Logic values
119          0  1  x  z  L  H
120 -------------------------
121 one      0  1  1  0  0  1
122 zero     1  0  1  0  1  0
123 flt      0  0  1  1  1  1
124 */
charToSym(char c)125 static StateSymbol charToSym(char c)
126 {
127   char *x;
128   StateSymbol p;
129 
130   if (c == '?') c = 'z';
131   if (isupper((int)c))
132     c = tolower(c);
133   x = strchr(sym_table_lc,c);
134   p = x ? (StateSymbol)(x-sym_table_lc) : SYM_INVALID;
135 
136   return p;
137 }
138 
Value_init(Value * S,int nbits)139 void Value_init(Value *S,int nbits)
140 {
141   int wc = SSNUMWORDS(nbits);
142 
143 #if DEBUG_VALUE_MEMMGR
144   S->status = 0;
145 #endif
146   S->nbits = nbits;
147   S->nalloc = wc;
148   S->one = (unsigned*)malloc(wc*sizeof(unsigned));
149   S->zero = (unsigned*)malloc(wc*sizeof(unsigned));
150   S->flt = (unsigned*)malloc(wc*sizeof(unsigned));
151   S->flags = SF_NONE;
152   S->permFlags = 0;
153 }
154 
Value_uninit(Value * S)155 void Value_uninit(Value *S)
156 {
157   free(S->one);
158   free(S->zero);
159   free(S->flt);
160 }
161 
Value_reinit(Value * S,int nbits)162 void Value_reinit(Value *S,int nbits)
163 {
164   int nwc = SSNUMWORDS(nbits);
165 
166   if (nwc <= S->nalloc) {
167     S->nbits = nbits;
168     S->nalloc = nwc;
169   } else {
170     Value_uninit(S);
171     Value_init(S,nbits);
172   }
173 }
174 
175 /*****************************************************************************
176  *
177  * Test to see if the bits in a from lowz up are all zero.
178  *
179  *****************************************************************************/
Value_isPartZero(Value * A,int lowz)180 int Value_isPartZero(Value *A,int lowz)
181 {
182   int wc = SSNUMWORDS(lowz);
183   unsigned mask = (lowz&SSBITMASK) ? LMASK((lowz & SSBITMASK)) : SSWORDMASK;
184   int wc2 = SSNUMWORDS(A->nbits);
185   unsigned mask2 = (A->nbits&SSBITMASK) ? LMASK((A->nbits & SSBITMASK)) : SSWORDMASK;
186   int i;
187 
188   if (wc2 == wc)
189     mask = (~mask) & mask2;
190   else
191     mask = (~mask);
192 
193   if ((A->one[wc-1]&mask) != 0) return 0;
194   if ((A->zero[wc-1]&mask) != mask) return 0;
195   if ((A->flt[wc-1]&mask) != 0) return 0;
196 
197   for (i = wc;i < wc2;i++) {
198     if (i == wc2-1) {
199       if ((A->one[i]&mask2) != 0) return 0;
200       if ((A->zero[i]&mask2) != mask2) return 0;
201       if ((A->flt[i]&mask2) != 0) return 0;
202     } else {
203       if ((A->one[i]) != 0) return 0;
204       if ((A->zero[i]) != SSWORDMASK) return 0;
205       if ((A->flt[i]) != 0) return 0;
206     }
207   }
208 
209   return 1;
210 }
211 
212 /*****************************************************************************
213  *
214  * Check for exact equivalence (including tests z and x bits)
215  *
216  * Paramaters:
217  *     A,B		Values to compare
218  *
219  * Returns:		Non-zero if A and B are identical including x/z bits.
220  *
221  *
222  *****************************************************************************/
Value_isEqual(Value * A,Value * B)223 int Value_isEqual(Value *A,Value *B)
224 {
225   unsigned nbits = imin(A->nbits, B->nbits);
226   int wc = SSNUMWORDS(nbits);
227   unsigned mask = (nbits&SSBITMASK) ? LMASK((nbits & SSBITMASK)) : SSWORDMASK;
228   int i;
229 
230   /*
231    * Check for equality up to smaller of a and b bit size.
232    */
233   for (i = 0;i < wc;i++) {
234     if (i == wc-1) {
235       if ((A->one[i]&mask) != (B->one[i]&mask)) return 0;
236       if ((A->zero[i]&mask) != (B->zero[i]&mask)) return 0;
237       if ((A->flt[i]&mask) != (B->flt[i]&mask)) return 0;
238     } else {
239       if (A->one[i] != B->one[i]) return 0;
240       if (A->zero[i] != B->zero[i]) return 0;
241       if (A->flt[i] != B->flt[i]) return 0;
242     }
243   }
244 
245   /*
246    * If bit sizes are different, extend with zeros.
247    */
248   if (A->nbits > B->nbits) {
249     return Value_isPartZero(A,B->nbits);
250   } else if (A->nbits < B->nbits) {
251     return Value_isPartZero(B,A->nbits);
252   } else
253     return 1;
254 }
255 
256 /*****************************************************************************
257  *
258  * Returns the transition type
259  *
260  *
261  *****************************************************************************/
Value_transitionType(Value * A,Value * B)262 transtype_t Value_transitionType(Value *A,Value *B)
263 {
264   if (Value_nbits(A) == 1) {
265     StateSymbol fromSym = Value_getBitSym(A,0);
266     StateSymbol toSym   = Value_getBitSym(B,0);
267 
268     if (fromSym == toSym) return TT_NONE;
269     if (toSym == SYM_ZERO) return TT_NEGEDGE;
270     if (fromSym == SYM_ONE && toSym != SYM_FLOAT) return TT_NEGEDGE;
271     return TT_POSEDGE;
272   } else {
273     if (Value_isEqual(A,B))
274       return TT_NONE;
275     else
276       return TT_EDGE;
277   }
278 }
279 
Value_unknown(Value * S)280 void Value_unknown(Value *S)
281 {
282   int wc = SSNUMWORDS(S->nbits);
283   int i;
284 
285   for (i = 0;i < wc;i++) {
286     S->zero[i] = ~0;
287     S->one[i] = ~0;
288     S->flt[i] = ~0;
289   }
290 }
291 
Value_float(Value * S)292 void Value_float(Value *S)
293 {
294   int wc = SSNUMWORDS(S->nbits);
295   int i;
296 
297   for (i = 0;i < wc;i++) {
298     S->zero[i] = 0;
299     S->one[i] = 0;
300     S->flt[i] = ~0;
301   }
302 }
303 
Value_zero(Value * S)304 void Value_zero(Value *S)
305 {
306   int wc = SSNUMWORDS(S->nbits);
307   int i;
308 
309   for (i = 0;i < wc;i++) {
310     S->zero[i] = ~0;
311     S->one[i] = 0;
312     S->flt[i] = 0;
313   }
314 }
315 
Value_one(Value * S)316 void Value_one(Value *S)
317 {
318   int wc = SSNUMWORDS(S->nbits);
319   int i;
320 
321   for (i = 0;i < wc;i++) {
322     S->one[i] = ~0;
323     S->flt[i] = 0;
324     S->zero[i] = 0;
325   }
326 }
327 
328 /*****************************************************************************
329  *
330  * Make S a logic 1 (low bit is 1, all others 0)
331  *
332  *****************************************************************************/
Value_lone(Value * S)333 void Value_lone(Value *S)
334 {
335   Value_zero(S);
336   S->one[0] |= 1;
337   S->zero[0] &= ~1;
338 }
339 
Value_isLogic(Value * S)340 int Value_isLogic(Value *S)
341 {
342   register int i;
343   register unsigned mask = SSWORDMASK;
344   register int wc = SSNUMWORDS(S->nbits);
345 
346   for (i = 0;i < wc;i++) {
347     if (i == wc-1 && (S->nbits & SSBITMASK))
348       mask = LMASK(S->nbits);
349 
350     if ((S->flt[i] & mask)) return 0;
351   }
352   return 1;
353 }
354 
Value_isZero(Value * S)355 int Value_isZero(Value *S)
356 {
357   register int i;
358   register unsigned mask = SSWORDMASK;
359   register int wc = SSNUMWORDS(S->nbits);
360 
361   for (i = 0;i < wc;i++) {
362     if (i == wc-1 && (S->nbits & SSBITMASK))
363       mask = LMASK(S->nbits);
364 
365     if (((S->flt[i] & mask) != 0)
366 	|| ((S->one[i] & mask) != 0)
367 	|| ((S->zero[i] & mask) != mask))
368       return 0;
369 
370   }
371   return 1;
372 }
373 
Value_isFloat(Value * S)374 int Value_isFloat(Value *S)
375 {
376   register int i;
377   register unsigned mask = SSWORDMASK;
378   register int wc = SSNUMWORDS(S->nbits);
379 
380   for (i = 0;i < wc;i++) {
381     if (i == wc-1 && (S->nbits & SSBITMASK))
382       mask = LMASK(S->nbits);
383 
384     if (((S->flt[i] & mask) != mask)
385 	|| ((S->one[i] & mask) != 0)
386 	|| ((S->zero[i] & mask) != 0))
387       return 0;
388   }
389   return 1;
390 }
391 
Value_isUnknown(Value * S)392 int Value_isUnknown(Value *S)
393 {
394   register int i;
395   register unsigned mask = SSWORDMASK;
396   register int wc = SSNUMWORDS(S->nbits);
397 
398   for (i = 0;i < wc;i++) {
399     if (i == wc-1 && (S->nbits & SSBITMASK))
400       mask = LMASK(S->nbits);
401 
402     if (((S->flt[i] & mask) != mask)
403 	|| ((S->one[i] & mask) != mask)
404 	|| ((S->zero[i] & mask) != mask))
405       return 0;
406   }
407   return 1;
408 }
409 
Value_hasUnknown(Value * S)410 Boolean Value_hasUnknown(Value *S)
411 {
412 	int i, wordCount;
413 
414 	wordCount = SSNUMWORDS(S->nbits);
415 
416 	for (i = 0; i < wordCount; ++i) {
417 		if (i == wordCount-1 && (S->nbits & SSBITMASK)) {
418 			unsigned mask = LMASK(S->nbits);
419 			if (S->flt[i] & S->zero[i] & S->one[i] & mask)
420 				return (GATE_TRUE);
421 		} else if (S->flt[i] & S->zero[i] & S->one[i])
422 			return (GATE_TRUE);
423 	}
424 	return (GATE_FALSE);
425 }
426 
Value_hasFloat(Value * S)427 Boolean Value_hasFloat(Value *S)
428 {
429 	int i, wordCount;
430 
431 	wordCount = SSNUMWORDS(S->nbits);
432 
433 	for (i = 0; i < wordCount; ++i) {
434 		if (i == wordCount-1 && (S->nbits & SSBITMASK)) {
435 			unsigned mask = LMASK(S->nbits);
436 			if (S->flt[i] & ~(S->zero[i] | S->one[i]) & mask)
437 				return (GATE_TRUE);
438 		} else if (S->flt[i] & ~(S->zero[i] | S->one[i]))
439 			return (GATE_TRUE);
440 	}
441 	return (GATE_FALSE);
442 }
443 
Value_xclear(Value * S)444 void Value_xclear(Value *S)
445 {
446   int wc = SSNUMWORDS(S->nbits);
447   int i;
448 
449   for (i = 0;i < wc;i++) {
450     S->one[i] = 0;
451     S->flt[i] = 0;
452     S->zero[i] = 0;
453   }
454 }
455 
Value_getBitSym(Value * S,int i)456 StateSymbol Value_getBitSym(Value *S,int i)
457 {
458   int w;
459   unsigned b;
460   StateSymbol x = SYM_NUL1;
461 
462   if (i >= S->nbits)
463     return SYM_ZERO;
464 
465   w = i >> SSWORDSHIFT;
466   b = 1 << (i & SSBITMASK);
467   if ((S->zero[w] & b)) x = (StateSymbol)(x | SYM_ZERO);
468   if ((S->one[w] & b))  x = (StateSymbol)(x | SYM_ONE);
469   if ((S->flt[w] & b))  x = (StateSymbol)(x | SYM_FLOAT);
470 
471   return x;
472 }
473 
Value_putBitSym(Value * S,int bit,StateSymbol p)474 void Value_putBitSym(Value *S,int bit,StateSymbol p)
475 {
476   int w;
477   unsigned b;
478 
479   if (bit >= S->nbits) return;
480 
481   w = bit >> SSWORDSHIFT;
482   b = 1 << (bit & SSBITMASK);
483   if ((p & SYM_ZERO)) S->zero[w] |= b; else S->zero[w] &= ~b;
484   if ((p & SYM_ONE)) S->one[w] |= b; else S->one[w] &= ~b;
485   if ((p & SYM_FLOAT)) S->flt[w] |= b; else S->flt[w] &= ~b;
486 }
487 
488 /*****************************************************************************
489  *
490  * Extend symbols in a value from bit d to the top bit.
491  *
492  * Parameters:
493  *     S		Word to be extended
494  *     d		Position to begin extension
495  *
496  * If the bit S[d-1] is a 1 or 0, then fill all higher bit position with a 0,
497  * if it is other than a 1 or 0, then fill all higher position with that symbol.
498  * This is used so that specifications like "16'bz" will result in a 16-bit
499  * value of all floats.
500  *
501  *****************************************************************************/
Value_extend(Value * S,int d)502 void Value_extend(Value *S,int d)
503 {
504   int i;
505   StateSymbol p = (d > 0) ? Value_getBitSym(S,d-1) : SYM_ZERO;
506   if (p == SYM_ONE) p = SYM_ZERO;
507 
508   for (i = d;i < S->nbits;i++)
509     Value_putBitSym(S,i,p);
510 }
511 
Value_extendSym(Value * S,int d,StateSymbol sym)512 static void Value_extendSym(Value *S,int d,StateSymbol sym)
513 {
514   int i;
515 
516   for (i = d;i < S->nbits;i++)
517     Value_putBitSym(S,i,sym);
518 }
519 
520 /*****************************************************************************
521  *
522  * Resize a Value value object
523  *
524  * Paramaters:
525  *     R			Value value object
526  *     nbits			Number of bits
527  *     dosign			Do sign extension.
528  *
529  *****************************************************************************/
Value_resize(Value * R,int nbits)530 void Value_resize(Value *R,int nbits)
531 {
532   if (nbits < R->nbits) { 		/* Downsizing */
533     R->nbits = nbits;
534   } else if (nbits > R->nbits) {	/* Upsizing */
535     int ob = R->nbits;
536     int nwc = SSNUMWORDS(nbits);
537 
538     if (nwc > R->nalloc) {
539       R->nalloc = nwc;
540       R->zero = (unsigned*) realloc(R->zero,nwc*sizeof(unsigned));
541       R->one  = (unsigned*) realloc(R->one,nwc*sizeof(unsigned));
542       R->flt  = (unsigned*) realloc(R->flt,nwc*sizeof(unsigned));
543     }
544     R->nbits = nbits;
545 
546     Value_extendSym(R,ob,SYM_ZERO);
547   }
548 }
549 
Value_toInt(Value * S,unsigned * I)550 int Value_toInt(Value *S,unsigned *I)
551 {
552   if (!Value_isLogic(S)) return -1;
553 
554   if (Value_isReal(S))
555     *I = (unsigned) *(real_t*)&S->one[0];
556   else
557     *I = S->one[0] & LMASK(S->nbits);
558 
559   return 0;
560 }
561 
Value_toReal(Value * S,real_t * n)562 int Value_toReal(Value *S,real_t *n)
563 {
564   if (!Value_isLogic(S)) return -1;
565 
566   if (Value_isReal(S)) {
567     *n = *(real_t*)&S->one[0];
568   } else {
569     *n = (real_t)S->one[0];
570   }
571 
572   return 0;
573 }
574 
Value_toTime(Value * S,simtime_t * I)575 int Value_toTime(Value *S,simtime_t *I)
576 {
577   if (!Value_isLogic(S)) return -1;
578 
579 #if SSWORDSIZE == 64
580   *I = S->one[0] & LMASK(S->nbits);
581 #else
582   if (S->nbits < SSWORDSIZE)
583     *I = S->one[0] & LMASK(S->nbits);
584   else if (S->nbits == SSWORDSIZE)
585     *I = S->one[0];
586   else if (S->nbits < 2*SSWORDSIZE)
587     *I = (((simtime_t)S->one[1]&LMASK(S->nbits-SSWORDSIZE)) << 32) | S->one[0];
588   else
589     *I = (((simtime_t)S->one[1]) << 32) | S->one[0];
590 #endif
591   return 0;
592 }
593 
Value_convertBits(Value * S,const char * A,int nbits)594 int Value_convertBits(Value *S,const char *A,int nbits)
595 {
596   int d = strlen(A);
597   int i;
598   int return_value = 0;
599 
600   if (!nbits) nbits = d;
601 
602   Value_reinit(S,nbits);
603   Value_xclear(S);
604 
605   for (i = 0;i < d;i++) {
606     StateSymbol p = charToSym(A[d-i-1]);
607     if (p == SYM_INVALID) {
608       return_value = -1;
609       p = SYM_ZERO;
610     }
611 
612     Value_putBitSym(S,i,p);
613   }
614   Value_extend(S,d);
615 
616   return return_value;
617 }
618 
Value_convertHex(Value * S,const char * A,int nbits)619 int Value_convertHex(Value *S,const char *A,int nbits)
620 {
621   int d = strlen(A);
622   int return_value = 0;
623   int i;
624 
625   if (nbits == 0) nbits = d*4;
626 
627   Value_reinit(S,nbits);
628   Value_xclear(S);
629 
630   for (i = 0;i < d;i++) {
631     char c = A[d-i-1];
632     if (isxdigit((int)c)) {
633       int b = 4*i;
634       int v;
635       if (isdigit((int)c))
636 	v = c - '0';
637       else {
638 	if (isupper((int)c)) c = tolower(c);
639 	v = c - 'a' + 10;
640       }
641       Value_putBitSym(S,b,  (v&0x1)?SYM_ONE:SYM_ZERO);
642       Value_putBitSym(S,b+1,(v&0x2)?SYM_ONE:SYM_ZERO);
643       Value_putBitSym(S,b+2,(v&0x4)?SYM_ONE:SYM_ZERO);
644       Value_putBitSym(S,b+3,(v&0x8)?SYM_ONE:SYM_ZERO);
645     } else {
646       StateSymbol p = charToSym(c);
647       int b = 4*i;
648 
649       if (p == SYM_INVALID) {
650 	return_value = -1;
651 	p = SYM_ZERO;
652       }
653 
654       Value_putBitSym(S,b,p);
655       Value_putBitSym(S,b+1,p);
656       Value_putBitSym(S,b+2,p);
657       Value_putBitSym(S,b+3,p);
658     }
659   }
660   Value_extend(S,4*d);
661 
662   return return_value;
663 }
664 
Value_convertOct(Value * S,const char * A,int nbits)665 int Value_convertOct(Value *S,const char *A,int nbits)
666 {
667   int d = strlen(A);
668   int i;
669   int return_value = 0;
670 
671   if (nbits == 0) nbits = d*3;
672 
673   Value_reinit(S,nbits);
674   Value_xclear(S);
675 
676   for (i = 0;i < d;i++) {
677     char c = A[d-i-1];
678     if (isodigit((int)c)) {
679       int b = 3*i;
680       int v;
681 
682       v = c - '0';
683       Value_putBitSym(S,b,  (v&0x1)?SYM_ONE:SYM_ZERO);
684       Value_putBitSym(S,b+1,(v&0x2)?SYM_ONE:SYM_ZERO);
685       Value_putBitSym(S,b+2,(v&0x4)?SYM_ONE:SYM_ZERO);
686     } else {
687       StateSymbol p = charToSym(c);
688       int b = 3*i;
689 
690       if (p == SYM_INVALID) {
691 	return_value = -1;
692 	p = SYM_ZERO;
693       }
694 
695       Value_putBitSym(S,b,p);
696       Value_putBitSym(S,b+1,p);
697       Value_putBitSym(S,b+2,p);
698     }
699   }
700   Value_extend(S,3*d);
701 
702   return return_value;
703 }
704 
705 /*****************************************************************************
706  *
707  * Convert a string in decimal format to a value.
708  *
709  * Parameters:
710  *      S 		Place to store value
711  *      p 		String to convert
712  *      nbits 		Number of bits for value (or 0 for auto sizing)
713  *
714  *****************************************************************************/
Value_convertDec(Value * S,const char * p,int nbits)715 int Value_convertDec(Value *S,const char *p,int nbits)
716 {
717   int l = strlen(p);
718   unsigned n;
719   int return_value = 0;
720 
721   S->flags = SF_DEC;
722 
723   if (l <= 9) {
724     /*
725      * Value is short enough to fit into 32 bit value
726      */
727     if (S->nbits == 0)
728       Value_reinit(S,SSWORDSIZE);
729     else
730       Value_reinit(S,nbits);
731 
732     if (sscanf(p,"%u",&n) != 1) {
733       *S->one = *S->zero = *S->flt = 0;
734       return -1;
735     }
736     S->one[0] = n;
737     if (nbits == 0)
738       S->nbits = SSWORDSIZE;
739   } else {
740     const double K = 3.32192809488736234791;	/* 1/log10(2) */
741     int reqbits = (int)(l*K+1);			/* Estimated bits required */
742     int wc;
743 
744     if (nbits == 0)
745       Value_reinit(S,reqbits);
746     else
747       Value_reinit(S,nbits);
748 
749     wc = SSNUMWORDS(S->nbits);
750     multint_cvtstr(S->one,wc,p);
751 
752     /*
753      * Readjust size if automatic sizing is used.
754      */
755     if (nbits == 0) {
756       while (S->one[wc-1] == 0 && wc > 1) wc--;
757       S->nbits = wc*SSWORDSIZE;
758     }
759   }
760 
761   Value_normalize(S);
762 
763   return return_value;
764 }
765 
766 /*****************************************************************************
767  *
768  * Convert a text string to an Value object
769  *
770  * Parameters:
771  *     S		Target state value in which to store scanned value
772  *     A		Ascii string with verilog format constant.
773  *
774  *****************************************************************************/
Value_convert(Value * S,const char * A)775 int Value_convert(Value *S,const char *A)
776 {
777   int nbits = 0;
778   const char *p;
779   char c;
780   int return_value = 0;
781 
782   S->flags = SF_NONE;
783 
784   if (sscanf(A,"%d'%c",&nbits,&c) == 2) {
785     p = strchr(A,'\'')+2;
786   } else if (sscanf(A,"'%c",&c) == 1) {
787     nbits = 0;
788     p = strchr(A,'\'')+2;
789   } else {
790     nbits = 0;
791     p = A;
792     c = 'i';
793   }
794 
795   switch (c) {
796   case 'b' :
797     return_value = Value_convertBits(S,p,nbits);
798     S->flags = SF_BIN;
799     break;
800   case 'h' :
801     return_value = Value_convertHex(S,p,nbits);
802     S->flags = SF_HEX;
803     break;
804   case 'o' :
805     return_value = Value_convertOct(S,p,nbits);
806     S->flags = SF_OCT;
807     break;
808   case 'd' :
809     return_value = Value_convertDec(S,p,nbits);
810     S->flags = SF_DEC;
811     break;
812   case 'i' :
813     return_value = Value_convertDec(S,p,nbits);
814     S->flags = SF_INT;
815     break;
816   default:
817     S->nbits = 1;
818     *S->one = *S->zero = *S->flt = 0;
819     return -1;
820   }
821 
822   return return_value;
823 }
824 
Value_convertFromInt(Value * S,unsigned I)825 int Value_convertFromInt(Value *S,unsigned I)
826 {
827   if (S->nbits > SSWORDSIZE) return -1;
828   S->one[0] = I;
829   S->zero[0] = ~I;
830   S->flt[0] = 0;
831   return 0;
832 }
833 
834 /*****************************************************************************
835  *
836  * Convert an integer to a 32-bit/64-bit Value
837  *
838  * Parameters:
839  *     S		Target state value in which to store scanned value
840  *     n		Integer to convert
841  *
842  *****************************************************************************/
Value_convertI(Value * S,int n)843 int Value_convertI(Value *S,int n)
844 {
845   Value_resize(S,SSWORDSIZE);
846   S->flags = SF_INT;
847   S->one[0] = n;
848   S->zero[0] = ~n;
849   S->flt[0] = 0;
850 
851   return 0;
852 }
853 
854 /*****************************************************************************
855  *
856  * Convert a real
857  *
858  * Parameters:
859  *     S		Target state value in which to store scanned value
860  *     n		Integer to convert
861  *
862  *****************************************************************************/
Value_convertR(Value * S,real_t n)863 int Value_convertR(Value *S,real_t n)
864 {
865   Value_resize(S,SSREALSIZE);
866 
867   S->flags = SF_REAL;
868   memmove(S->one,&n,SSREALBYTES);
869 
870   Value_normalize(S);
871 
872   return 0;
873 }
874 
875 /*****************************************************************************
876  *
877  * Convert a real NaN
878  *
879  * Parameters:
880  *     S		Target state value in which to store scanned value
881  *     n		Integer to convert
882  *
883  *****************************************************************************/
Value_convertNaN(Value * S)884 int Value_convertNaN(Value *S)
885 {
886   Value_resize(S,SSREALSIZE);
887 
888   S->flags = SF_REAL;
889   memset(S->one,0,SSREALBYTES);
890   memset(S->zero,0,SSREALBYTES);
891   memset(S->flt,0xff,SSREALBYTES);
892 
893   return 0;
894 }
895 
896 /*****************************************************************************
897  *
898  * Convert a 64-bit time to a 64-bit Value
899  *
900  * Parameters:
901  *     S		Target state value in which to store scanned value
902  *     n		Integer to convert
903  *
904  *****************************************************************************/
Value_convertTime(Value * S,simtime_t n)905 int Value_convertTime(Value *S,simtime_t n)
906 {
907   Value_resize(S,sizeof(simtime_t)*8);
908   S->flags = SF_INT;
909 
910 #if TKGATE_WORDSIZE == 64
911   S->one[0] = n;
912   S->zero[0] = ~n;
913   S->flt[0] = 0;
914 #else
915   S->one[0] = n & 0xffffffff;
916   S->zero[0] = ~n & 0xffffffff;
917   S->flt[0] = 0;
918 
919   S->one[1] = (n>>32) & 0xffffffff;
920   S->zero[1] = ~(n>>32) & 0xffffffff;
921   S->flt[1] = 0;
922 #endif
923 
924   return 0;
925 }
926 
927 /*****************************************************************************
928  *
929  * Convert a string into an Value encoding that string.
930  *
931  * Parameters:
932  *     S		Target value
933  *     str		String representation of value to be converted
934  *
935  *****************************************************************************/
Value_convertStr(Value * S,const char * str)936 int Value_convertStr(Value *S,const char *str)
937 {
938   int l = strlen(str);
939   int nbits = imax(l*8,8);
940   int wc = SSNUMWORDS(nbits);
941   int i,j;
942   const char *s_end = str+l-1;
943 
944   Value_reinit(S,nbits);
945   Value_zero(S);
946 
947   for (i = 0;i < wc;i++) {
948     for (j = 0;j < SSWORDBYTES;j++) {
949       const char *cstart = s_end-SSWORDBYTES*i-j;
950       if (cstart >= str)
951 	S->one[i] |= (*cstart & 0xff) << 8*j;
952     }
953   }
954 
955   Value_normalize(S);
956   S->flags = SF_STRING;
957 
958   return 0;
959 }
960 
Value_printSymbol(int x,FILE * f)961 void Value_printSymbol(int x,FILE *f)
962 {
963   if (x >= 0 && x < sym_table_len)
964     fputc(sym_table[x],f);
965   else
966     fprintf(f,":%d:",x);
967 }
968 
Value_symbolStr(int x,char * p)969 int Value_symbolStr(int x,char *p)
970 {
971   if (x >= 0 && x < sym_table_len) {
972     *p = sym_table[x];
973     return 1;
974   } else
975     return sprintf(p,":%d:",x);
976 }
977 
Value_getstr_str(Value * S,char * p,int prefix)978 static int Value_getstr_str(Value *S,char *p,int prefix)
979 {
980   int wc = SSNUMWORDS(S->nbits);
981   int i,j;
982 
983   if (prefix)
984     *p++ = '"';
985   for (i = wc-1;i >= 0;i--) {
986     for (j = SSWORDBYTES-1;j >= 0;j--) {
987       unsigned c = (S->one[i] >> (j*8)) & 0xff;
988       if (c) {
989 	if (prefix && (c == '"' || c == '\\')) *p++ = '\\';
990 	*p++ = c;
991       }
992     }
993   }
994   if (prefix)
995     *p++ = '"';
996 
997   *p = 0;
998   return 0;
999 }
1000 
1001 /*****************************************************************************
1002  *
1003  * Convert an integer in value form into its string representation.
1004  *
1005  * Parameters:
1006  *     S		Value to be converted
1007  *     str		Buffer to which to write string.
1008  *
1009  *****************************************************************************/
Value_getstr_int(Value * S,char * str)1010 static int Value_getstr_int(Value *S,char *str)
1011 {
1012   /*
1013    * IEEE Std 1364-2001 17.1.1.4
1014    */
1015   if (Value_isUnknown(S))
1016 	sprintf(str, "x");
1017   else if (Value_isFloat(S))
1018 	sprintf(str, "z");
1019   else if (Value_hasUnknown(S))
1020 	sprintf(str, "X");
1021   else if (Value_hasFloat(S))
1022 	sprintf(str, "Z");
1023   else if (Value_isLogic(S)) {
1024     if (Value_nbits(S) <= SSWORDSIZE) {
1025       sprintf(str,"%u",S->one[0]&LMASK(Value_nbits(S)));
1026     } else {
1027       int wc = SSNUMWORDS(S->nbits);
1028 
1029       S->one[wc-1] &= LMASK(Value_nbits(S));
1030       multint_getstr(S->one,wc,str,1024);
1031     }
1032   } else
1033   /** @TODO generate Verga error  */
1034     sprintf(str,"ERROR");
1035 
1036   return 0;
1037 }
1038 
1039 #if 0
1040 static int Value_getstr_time(Value *S,char *p)
1041 {
1042   if (Value_isLogic(S)) {
1043 #if SSWORDSIZE == 64
1044     sprintf(p,"%u",S->one[0]&LMASK(Value_nbits(S)));
1045 #else
1046     if (Value_nbits(S) <= SSWORDSIZE) {
1047       sprintf(p,"%u",S->one[0]&LMASK(Value_nbits(S)));
1048     } else {
1049       sprintf(p,"%llu",((((simtime_t)S->one[1]&LMASK(S->nbits-SSWORDSIZE)) << 32) | S->one[0]));
1050     }
1051 #endif
1052   } else
1053     sprintf(p,"x");
1054 
1055   return 0;
1056 }
1057 #endif
1058 
Value_getstr_dec(Value * S,char * p,int prefix)1059 static int Value_getstr_dec(Value *S,char *p,int prefix)
1060 {
1061   if (prefix)
1062     p += sprintf(p,"%d'd",Value_nbits(S));
1063 
1064   if (Value_isLogic(S)) {
1065     if (Value_nbits(S) <= SSWORDSIZE)
1066       sprintf(p,"%u",S->one[0]&LMASK(Value_nbits(S)));
1067     else
1068       Value_getstr_int(S,p);
1069   } else
1070     sprintf(p,"x");
1071 
1072   return 0;
1073 }
1074 
1075 /*****************************************************************************
1076  *
1077  * Generate the string encoded by value S
1078  *
1079  *****************************************************************************/
Value_toString(Value * S,char * p)1080 int Value_toString(Value *S,char *p)
1081 {
1082   Value_getstr_str(S,p,0);
1083   return 0;
1084 }
1085 
1086 /*****************************************************************************
1087  *
1088  * Trim leading 0/x/z digits from the string representation of a value
1089  *
1090  * Parameters:
1091  *     p		String to be trimmed
1092  *
1093  * Returns:		Pointer to end of trimmed string.
1094  *
1095  *****************************************************************************/
Value_trim(char * p)1096 static char *Value_trim(char *p)
1097 {
1098   char *q = p;
1099   char top = *q;
1100 
1101   if (isxdigit(top) && top != '0')
1102     return p + strlen(p);
1103 
1104   while (top == *q) q++;
1105 
1106   if ((!*q || !isxdigit(*q) || !isxdigit(top)) && q != p) q--;
1107 
1108   if (p != q)
1109     memmove(p,q,strlen(q)+1);
1110   return p + strlen(p);
1111 }
1112 
Value_getstr_bin(Value * S,char * p,int prefix)1113 static int Value_getstr_bin(Value *S,char *p,int prefix)
1114 {
1115   char *trimPart;
1116   char *q = p;
1117   int i;
1118 
1119   if (prefix)
1120     p += sprintf(p,"%d'b",Value_nbits(S));
1121   trimPart = p;
1122 
1123   for (i = Value_nbits(S)-1;i >= 0;i--) {
1124 
1125     Value_symbolStr(Value_getBitSym(S,i),p);
1126     p++;
1127   }
1128 
1129   *p = 0;
1130   return Value_trim(trimPart) - q;
1131 }
1132 
Value_getstr_oct(Value * S,char * p,int prefix)1133 static int Value_getstr_oct(Value *S,char *p,int prefix)
1134 {
1135   int N = S->nbits;
1136   int M = ((N+2)/3)*3;		/* Round to multiple of 3 */
1137   int i;
1138   char *q = p;
1139   char *trimPart;
1140 
1141   if (prefix)
1142     p += sprintf(p,"%d'o",N);
1143   trimPart = p;
1144 
1145 
1146   for (i = M;i > 0;i -= 3) {
1147     int x0 = Value_getBitSym(S,i-3);
1148     int x1 = Value_getBitSym(S,i-2);
1149     int x2 = Value_getBitSym(S,i-1);
1150     int bits_same,logic_digs;
1151 
1152     bits_same = (x2 == x0 || i-1 >= N)
1153       && (x1 == x0 || i-2 >= N);
1154     logic_digs = isBDig(x0) && isBDig(x1) && isBDig(x2);
1155 
1156     if (bits_same && !isBDig(x0))
1157       p += Value_symbolStr(x0,p);
1158     else if (logic_digs) {
1159       int d = 0;
1160       if (x0 == SYM_ONE) d |= 0x1;
1161       if (x1 == SYM_ONE) d |= 0x2;
1162       if (x2 == SYM_ONE) d |= 0x4;
1163 
1164       p += sprintf(p,"%d",d);
1165     } else {
1166       if (value_print_showbits) {
1167 	p += sprintf(p,"(");
1168 	if (i-1 < N) p += Value_symbolStr(x2,p);
1169 	if (i-2 < N) p += Value_symbolStr(x1,p);
1170 	if (i-3 < N) p += Value_symbolStr(x0,p);
1171 	p += sprintf(p,")");
1172       } else
1173 	p += sprintf(p,"?");
1174     }
1175   }
1176   *p = 0;
1177 
1178   return Value_trim(trimPart) - q;
1179 }
1180 
1181 
Value_getstr_real(Value * S,char * p,int useG)1182 static int Value_getstr_real(Value *S,char *p,int useG)
1183 {
1184   if (!Value_isLogic(S))
1185     return sprintf(p,"NaN");
1186   else {
1187     real_t n = *(real_t*)&S->one[0];
1188     if (useG)
1189       return sprintf(p,"%g",n);
1190     else
1191       return sprintf(p,"%f",n);
1192   }
1193 }
1194 
Value_getstr_hex(Value * S,char * p,int prefix)1195 static int Value_getstr_hex(Value *S,char *p,int prefix)
1196 {
1197   int N = S->nbits;
1198   int M = (N & 0x3) ? (N | 0x3)+1 : N;	/* Round to multiple of 4 */
1199   int i;
1200   char *q = p;
1201   char *trimPart;
1202 
1203   if (prefix)
1204     p += sprintf(p,"%d'h",N);
1205   trimPart = p;
1206 
1207   for (i = M;i > 0;i -= 4) {
1208     int x0 = Value_getBitSym(S,i-4);
1209     int x1 = Value_getBitSym(S,i-3);
1210     int x2 = Value_getBitSym(S,i-2);
1211     int x3 = Value_getBitSym(S,i-1);
1212     int bits_same,logic_digs;
1213 
1214     bits_same = (x3 == x0 || i-1 >= N)
1215       && (x2 == x0 || i-2 >= N)
1216 	&& (x1 == x0 || i-3 >= N);
1217     logic_digs = isBDig(x0) && isBDig(x1) && isBDig(x2) && isBDig(x3);
1218 
1219     if (bits_same && !isBDig(x0))
1220       p += Value_symbolStr(x0,p);
1221     else if (logic_digs) {
1222       int d = 0;
1223       if (x0 == SYM_ONE) d |= 0x1;
1224       if (x1 == SYM_ONE) d |= 0x2;
1225       if (x2 == SYM_ONE) d |= 0x4;
1226       if (x3 == SYM_ONE) d |= 0x8;
1227 
1228       if (d <= 9)
1229 	p += sprintf(p,"%d",d);
1230       else
1231 	p += sprintf(p,"%c",d-10+'a');
1232     } else {
1233       if (value_print_showbits) {
1234 	p += sprintf(p,"(");
1235 	if (i-1 < N) p += Value_symbolStr(x3,p);
1236 	if (i-2 < N) p += Value_symbolStr(x2,p);
1237 	if (i-3 < N) p += Value_symbolStr(x1,p);
1238 	if (i-4 < N) p += Value_symbolStr(x0,p);
1239 	p += sprintf(p,")");
1240       } else
1241 	p += sprintf(p,"?");
1242     }
1243   }
1244   *p = 0;
1245 
1246   return Value_trim(trimPart) - q;
1247 }
1248 
1249 
1250 /*****************************************************************************
1251  *
1252  * Get a string representing the state value.
1253  *
1254  * Parameters:
1255  *     s		Value to convert to string
1256  *     p		Buffer in which to write string representation
1257  *
1258  *****************************************************************************/
Value_getstr(Value * S,char * p)1259 int Value_getstr(Value *S,char *p)
1260 {
1261   if ((S->flags & SF_REAL)) {
1262     Value_getstr_real(S,p,1);
1263     return 0;
1264   }
1265 
1266   /*
1267    * Display as a decimal value
1268    */
1269   if ((S->flags & SF_INT)) {
1270     Value_getstr_int(S,p);
1271 #if 0
1272     if (Value_nbits(S) == SSWORDSIZE)
1273       Value_getstr_int(S,p);
1274     else
1275       Value_getstr_hex(S,p,1);
1276 #endif
1277     return 0;
1278   }
1279 
1280   if ((S->flags & SF_DEC)) {
1281     Value_getstr_dec(S,p,1);
1282 #if 0
1283     if (Value_nbits(S) <= SSWORDSIZE)
1284       Value_getstr_dec(S,p,1);
1285     else
1286       Value_getstr_hex(S,p,1);
1287 #endif
1288     return 0;
1289   }
1290 
1291   if ((S->flags & SF_HEX))
1292     return Value_getstr_hex(S,p,1);
1293 
1294   if ((S->flags & SF_OCT))
1295     return Value_getstr_oct(S,p,1);
1296 
1297   if ((S->flags & SF_BIN))
1298     return Value_getstr_bin(S,p,1);
1299 
1300   if ((S->flags & SF_STRING)) {
1301     return Value_getstr_str(S,p,1);
1302   }
1303 
1304   if (Value_nbits(S) == 1)
1305     return Value_getstr_bin(S,p,1);
1306   else
1307     return Value_getstr_hex(S,p,1);
1308 }
1309 
1310 /*****************************************************************************
1311  *
1312  * Get a string representing the state value, but exclude the "integer" types.
1313  *
1314  * Parameters:
1315  *     s		Value to convert to string
1316  *     p		Buffer in which to write string representation
1317  *
1318  *****************************************************************************/
Value_getvstr(Value * S,char * p)1319 int Value_getvstr(Value *S,char *p)
1320 {
1321   if (Value_nbits(S) == 1)
1322     return Value_getstr_bin(S,p,1);
1323   else
1324     return Value_getstr_hex(S,p,1);
1325 }
1326 
1327 
1328 /*****************************************************************************
1329  *
1330  * Convert state value to a string with formatting.
1331  *
1332  * Parameters:
1333  *     S	State value to convert
1334  *     fmt	A format string (see below)
1335  *     p	Buffer in which to write formatted string
1336  *
1337  * 'fmt' must be a single Verilog format string.  The first character must be
1338  * a '%' and the last character must be the type control character.
1339  *
1340  *****************************************************************************/
Value_format(Value * S,const char * fmt,char * p)1341 int Value_format(Value *S,const char *fmt,char *p)
1342 {
1343   const char *x = fmt + strlen(fmt) - 1;
1344   int n;
1345   int has_length = 0;
1346   int right_just = 1;
1347   int zero_fill = 0;
1348   int p_len;
1349 
1350   if (sscanf(fmt+1,"0%d",&n) == 1) {
1351     zero_fill = 1;
1352     has_length = 1;
1353   } else if (sscanf(fmt+1,"-%d",&n) == 1) {
1354     right_just = 0;
1355     has_length = 1;
1356   } else if (sscanf(fmt+1,"%d",&n) == 1) {
1357     has_length = 1;
1358   }
1359 
1360   switch (*x) {
1361   case 'd' :
1362   case 'D' :
1363     Value_getstr_int(S,p);
1364     break;
1365   case 'b' :
1366   case 'B' :
1367     Value_getstr_bin(S,p,0);
1368     break;
1369   case 'h' :
1370   case 'H' :
1371     Value_getstr_hex(S,p,0);
1372     break;
1373   case 'o' :
1374   case 'O' :
1375     Value_getstr_oct(S,p,0);
1376     break;
1377   case 'c' :
1378   case 'C' :
1379     break;
1380   case 's' :
1381   case 'S' :
1382     Value_getstr_str(S,p,0);
1383     break;
1384   case 'm' :
1385   case 'M' :
1386     break;
1387   case 'v' :
1388   case 'V' :
1389     break;
1390 #if 0
1391   case 't' :
1392   case 'T' :
1393     Value_getstr_time(S,p);
1394     break;
1395 #endif
1396   case 'e' :
1397   case 'E' :
1398     break;
1399   case 'f' :
1400   case 'F' :
1401     Value_getstr_real(S,p,0);
1402     break;
1403   case 'g' :
1404   case 'G' :
1405     Value_getstr_real(S,p,1);
1406     break;
1407   }
1408 
1409   p_len = strlen(p);
1410 
1411   if (has_length && p_len < n) {
1412     if (right_just) {
1413       int fc = ' ';
1414       int i;
1415       if (zero_fill) {
1416 	if (isxdigit(*p))
1417 	  fc = '0';
1418 	else
1419 	  fc = *p;
1420       }
1421 
1422       memmove(p+n-p_len,p,p_len+1);
1423       for (i = 0;i < n-p_len;i++)
1424 	p[i] = fc;
1425     } else {
1426       int i;
1427       for (i = p_len;i < n;i++)
1428 	p[i] = ' ';
1429       p[n] = 0;
1430     }
1431   }
1432 
1433   return 0;
1434 }
1435 
1436 
1437 
Value_print(Value * S,FILE * f)1438 void Value_print(Value *S,FILE *f)
1439 {
1440   char buf[STRMAX];
1441 
1442   Value_getstr(S,buf);
1443   fprintf(f,"%s",buf);
1444 }
1445 
1446 /*****************************************************************************
1447  *
1448  * Make A and B the same bit size by increasing the size of the smaller one.
1449  *
1450  * Parameters:
1451  *     A,B		Value values to be resized
1452  *
1453  *****************************************************************************/
Value_makeSameSize(Value * A,Value * B)1454 void Value_makeSameSize(Value *A,Value *B)
1455 {
1456   if (A->nbits < B->nbits) {
1457     Value_resize(A,B->nbits);
1458   } else if (B->nbits < A->nbits) {
1459     Value_resize(B,A->nbits);
1460   }
1461 }
1462 
1463 /*****************************************************************************
1464  *
1465  * Make A, B and C the same bit size by increasing the size of the smaller ones.
1466  *
1467  * Parameters:
1468  *     A,B,C		Value values to be resized
1469  *
1470  *****************************************************************************/
Value_makeSameSize3(Value * A,Value * B,Value * C)1471 void Value_makeSameSize3(Value *A,Value *B,Value *C)
1472 {
1473   int maxSize = A->nbits;
1474   if (maxSize < B->nbits) maxSize = B->nbits;
1475   if (maxSize < C->nbits) maxSize = C->nbits;
1476 
1477   Value_resize(A,maxSize);
1478   Value_resize(B,maxSize);
1479   Value_resize(C,maxSize);
1480 }
1481 
1482 /*****************************************************************************
1483  *
1484  * Copy a Value value
1485  *
1486  * Parameters:
1487  *     r		Target of assignment
1488  *     a		Source of assignment
1489  *
1490  *****************************************************************************/
Value_copy(Value * r,Value * a)1491 void Value_copy(Value *r,Value *a)
1492 {
1493   int wc = SSNUMWORDS(r->nbits);
1494   int i;
1495 
1496   r->flags = a->flags;
1497 
1498   for (i = 0;i < wc;i++) {
1499     r->one[i] = a->one[i];
1500     r->zero[i] = a->zero[i];
1501     r->flt[i] = a->flt[i];
1502   }
1503 }
1504 
1505 /*****************************************************************************
1506  *
1507  * Copy range of bits
1508  *
1509  * Parameters:
1510  *     R		Target Value value
1511  *     rl		Low bit in R at which to start copy
1512  *     A		Source Value value
1513  *     ah		High bit in source to copy
1514  *     al		Low bit in source to copy
1515  *
1516  * Returns:		Type of transition that occured.
1517  *
1518  * Copy a range of bits.  Copies bits in the range [ah:al] of A into R
1519  * starting at bit rl.
1520  *
1521  *****************************************************************************/
Value_copyRange(Value * R,int rl,Value * A,int ah,int al)1522 transtype_t Value_copyRange(Value *R,int rl,Value *A,int ah,int al)
1523 {
1524   transtype_t tt = TT_NONE;
1525   int rh = rl+(ah-al);						/* High bit of target in R */
1526 
1527   if (ah == al) {
1528     /*****************************************************************************
1529      * Special case for one-bit copy.
1530      *****************************************************************************/
1531     StateSymbol fromSym = Value_getBitSym(R,rl);
1532     StateSymbol toSym   = Value_getBitSym(A,ah);
1533 
1534     R->flags = (ValueFlags)(R->flags | A->flags);
1535 
1536     if (fromSym == toSym)
1537       tt = TT_NONE;
1538     else if (toSym == SYM_ZERO)
1539       tt = TT_NEGEDGE;
1540     else if (fromSym == SYM_ONE && toSym != SYM_FLOAT)
1541       tt = TT_NEGEDGE;
1542     else
1543       tt = TT_POSEDGE;
1544 
1545     if (Value_nbits(R) != 1 && tt != TT_NONE)
1546       tt = TT_EDGE;
1547 
1548     Value_putBitSym(R,rl,toSym);
1549   } else {
1550     int rl_w = rl >> SSWORDSHIFT;
1551     int rl_b = rl & SSBITMASK;
1552     int al_w = al >> SSWORDSHIFT;
1553     int al_b = al & SSBITMASK;
1554     int rh_w = rh >> SSWORDSHIFT;
1555     int rh_b = rh & SSBITMASK;
1556     int dst_w;
1557     int src_w;
1558 
1559     if (rl_b == al_b) {
1560       /*****************************************************************************
1561        * Copy subregion with shift that is a multiple of word size
1562        *****************************************************************************/
1563       src_w = al_w;
1564       for (dst_w = rl_w; dst_w <= rh_w; dst_w++, src_w++) {
1565 	unsigned rone, rzero, rflt, mask;
1566 
1567 	/*
1568 	 * "mask" is a mask of bits in R that should change in this word.
1569 	 */
1570 	mask = SSWORDMASK;
1571 	if (dst_w == rl_w) mask &= HMASKZ(rl_b);
1572 	if (dst_w == rh_w) mask &= LMASK(rh_b+1);
1573 
1574 	rone  = (A->one[src_w]&mask)  | (R->one[dst_w]&~mask);
1575 	rzero = (A->zero[src_w]&mask) | (R->zero[dst_w]&~mask);
1576 	rflt  = (A->flt[src_w]&mask)  | (R->flt[dst_w]&~mask);
1577 
1578 	/*
1579 	 * If any bits of R changed, update the value and record this as an edge.
1580 	 */
1581 	if ((rone ^ R->one[dst_w]) || (rzero ^ R->zero[dst_w]) || (rflt ^ R->flt[dst_w]) ) {
1582 	  tt = TT_EDGE;
1583 	  R->one[dst_w]  = rone;
1584 	  R->zero[dst_w] = rzero;
1585 	  R->flt[dst_w]  = rflt;
1586 	}
1587       }
1588     } else if (rl_b > al_b) {
1589       /*****************************************************************************
1590        * Up shifting
1591        *****************************************************************************/
1592       int b_up = (rl_b - al_b) & SSBITMASK;			/* Shift up bits */
1593       int b_dn = SSWORDSIZE - b_up;			/* Shift down for next word */
1594 
1595       src_w = al_w;
1596       for (dst_w = rl_w; dst_w <= rh_w; dst_w++, src_w++) {
1597 	unsigned aone, azero, aflt, rone, rzero, rflt, mask;
1598     if (src_w < A->nalloc) {
1599 	  aone  = A->one[src_w] << b_up;
1600 	  azero = A->zero[src_w] << b_up;
1601 	  aflt  = A->flt[src_w] << b_up;
1602     }
1603     else {
1604       aone  = 0;
1605 	  azero = 0;
1606 	  aflt  = 0;
1607     }
1608 	if (src_w > 0) {
1609 	  aone  |= A->one[src_w-1] >> b_dn;
1610 	  azero |= A->zero[src_w-1] >> b_dn;
1611 	  aflt  |= A->flt[src_w-1] >> b_dn;
1612 	}
1613 
1614 	/*
1615 	 * "mask" is a mask of bits in R that should change in this word.
1616 	 */
1617 	mask = SSWORDMASK;
1618 	if (dst_w == rl_w) mask &= HMASKZ(rl_b);
1619 	if (dst_w == rh_w) mask &= LMASK(rh_b+1);
1620 
1621 	rone  = (aone&mask)  | (R->one[dst_w]&~mask);
1622 	rzero = (azero&mask) | (R->zero[dst_w]&~mask);
1623 	rflt  = (aflt&mask)  | (R->flt[dst_w]&~mask);
1624 
1625 	/*
1626 	 * If any bits an R changed, update the value and record this as an edge.
1627 	 */
1628 	if ((rone ^ R->one[dst_w]) || (rzero ^ R->zero[dst_w]) || (rflt ^ R->flt[dst_w]) ) {
1629 	  tt = TT_EDGE;
1630 	  R->one[dst_w]  = rone;
1631 	  R->zero[dst_w] = rzero;
1632 	  R->flt[dst_w]  = rflt;
1633 	}
1634       }
1635     } else {
1636       /*****************************************************************************
1637        * Down shifting
1638        *****************************************************************************/
1639       int ah_w = ah >> SSWORDSHIFT;
1640       int b_dn = (al_b - rl_b) & SSBITMASK;			/* Shift down bits */
1641       int b_up = SSWORDSIZE - b_dn;			/* Shift up for next word */
1642 
1643       src_w = al_w;
1644       for (dst_w = rl_w; dst_w <= rh_w; dst_w++, src_w++) {
1645 	unsigned aone, azero, aflt, rone, rzero, rflt, mask;
1646 
1647 	aone  = A->one[src_w] >> b_dn;
1648 	azero = A->zero[src_w] >> b_dn;
1649 	aflt  = A->flt[src_w] >> b_dn;
1650 	if ( (src_w <= ah_w) && (src_w+1 < A->nalloc) ) {
1651 	  aone  |= A->one[src_w+1] << b_up;
1652 	  azero |= A->zero[src_w+1] << b_up;
1653 	  aflt  |= A->flt[src_w+1] << b_up;
1654 	}
1655 
1656 	/*
1657 	 * "mask" is a mask of bits in R that should change in this word.
1658 	 */
1659 	mask = SSWORDMASK;
1660 	if (dst_w == rl_w) mask &= HMASKZ(rl_b);
1661 	if (dst_w == rh_w) mask &= LMASK(rh_b+1);
1662 
1663 	rone  = (aone&mask)  | (R->one[dst_w]&~mask);
1664 	rzero = (azero&mask) | (R->zero[dst_w]&~mask);
1665 	rflt  = (aflt&mask)  | (R->flt[dst_w]&~mask);
1666 
1667 	/*
1668 	 * If any bits an R changed, update the value and record this as an edge.
1669 	 */
1670 	if ((rone ^ R->one[dst_w]) || (rzero ^ R->zero[dst_w]) || (rflt ^ R->flt[dst_w]) ) {
1671 	  tt = TT_EDGE;
1672 	  R->one[dst_w]  = rone;
1673 	  R->zero[dst_w] = rzero;
1674 	  R->flt[dst_w]  = rflt;
1675 	}
1676       }
1677     }
1678   }
1679   return tt;
1680 }
1681 
1682 /*****************************************************************************
1683  *
1684  *  Wire merge function for "wire" and "tri"
1685  *
1686  * Parameters:
1687  *      R		Return value
1688  *      A		Wire A
1689  *      B		Wire B
1690  *
1691  *
1692  *WIRE                one                zero                flt
1693  *   0 1 x z L H        0 1 x z L H        0 1 x z L H        0 1 x z L H  01z
1694  *  +-----------       +-----------       +-----------       +-----------  ---
1695  * 0|0 x x 0 0 x      0|0 1 1 0 0 1      0|1 1 1 1 1 1      0|0 1 1 0 0 1  100
1696  * 1|x 1 x 1 x 1      1|1 1 1 1 1 1      1|1 0 1 0 1 0      1|1 0 1 0 1 0  010
1697  * x|x x x x x x      x|1 1 1 1 1 1      x|1 1 1 1 1 1      x|1 1 1 1 1 1  111
1698  * z|0 1 x z L H      z|0 1 1 0 0 1      z|1 0 1 0 1 0      z|0 0 1 1 1 1  001
1699  * L|0 x x L L x      L|0 1 1 0 0 1      L|1 1 1 1 1 1      L|0 1 1 1 1 1  101
1700  * H|x 1 x H x H      H|1 1 1 1 1 1      H|1 0 1 0 1 0      H|1 0 1 1 1 1  011
1701  *
1702  *****************************************************************************/
Value_wire(Value * R,Value * A,Value * B)1703 void Value_wire(Value *R,Value *A,Value *B)
1704 {
1705   int wc = SSNUMWORDS(R->nbits);
1706   register int i;
1707 
1708   for (i = 0;i < wc;i++) {
1709     register unsigned RisOne = A->one[i]|B->one[i];
1710     register unsigned RisZero = A->zero[i]|B->zero[i];
1711     register unsigned RhasFloat = (A->flt[i]&B->flt[i]);
1712 
1713     R->one[i]  = RisOne;
1714     R->zero[i] = RisZero;
1715     R->flt[i]  = ~(RisOne ^ RisZero) | RhasFloat;
1716   }
1717 }
1718 
1719 /*****************************************************************************
1720  *
1721  *  Wire merge function for "wand" and "triand"
1722  *
1723  * Parameters:
1724  *      R		Return value
1725  *      A		Wire A
1726  *      B		Wire B
1727  *
1728  *WAND/TRIAND        one                zero                flt
1729  *   0 1 x z L H       0 1 x z L H        0 1 x z L H        0 1 x z L H  01z
1730  *  +-----------      +-----------       +-----------       +-----------  ---
1731  * 0|0 0 0 0 0 0     0|0 0 0 0 0 0      0|1 1 1 1 1 1      0|0 0 0 0 0 0  100
1732  * 1|0 1 x 1 x 1     1|0 1 1 1 1 1      1|1 0 1 0 1 0      1|0 0 1 0 1 0  010
1733  * x|0 x x x x x     x|0 1 1 1 1 1      x|1 1 1 1 1 1      x|0 1 1 1 1 1  111
1734  * z|0 1 x z L H     z|0 1 1 0 0 1      z|1 0 1 0 1 0      z|0 0 1 1 1 1  001
1735  * L|0 x x L L x     L|0 1 1 0 0 1      L|1 1 1 1 1 1      L|0 1 1 1 1 1  101
1736  * H|0 1 x H x H     H|0 1 1 1 1 1      H|1 0 1 0 1 0      H|0 0 1 1 1 1  011
1737  *
1738  */
Value_wand(Value * R,Value * A,Value * B)1739 void Value_wand(Value *R,Value *A,Value *B)
1740 {
1741   int wc = SSNUMWORDS(R->nbits);
1742   register int i;
1743 
1744   for (i = 0;i < wc;i++) {
1745     register unsigned RisOne = A->one[i]|B->one[i];
1746     register unsigned RisZero = A->zero[i]|B->zero[i];
1747     register unsigned RhasFloat = (A->flt[i]&B->flt[i]);
1748     register unsigned Rzero = (~A->one[i]&A->zero[i]&~A->flt[i]) | (~B->one[i]&B->zero[i]&~B->flt[i]);
1749 
1750     R->one[i]  = RisOne & ~Rzero;
1751     R->zero[i] = RisZero;
1752     R->flt[i]  = (~(RisOne ^ RisZero) | RhasFloat) & ~Rzero;
1753   }
1754 }
1755 
1756 /*****************************************************************************
1757  *
1758  *  Wire merge function for "wor" and "trior"
1759  *
1760  * Parameters:
1761  *      R		Return value
1762  *      A		Wire A
1763  *      B		Wire B
1764  *
1765  *WOR/TRIOR          one                zero                flt
1766  *   0 1 x z L H       0 1 x z L H        0 1 x z L H        0 1 x z L H  01z
1767  *  +-----------      +-----------       +-----------       +-----------  ---
1768  * 0|0 1 x 0 0 x     0|0 1 1 0 0 1      0|1 0 1 1 1 1      0|0 0 1 0 0 1  100
1769  * 1|1 1 1 1 1 1     1|1 1 1 1 1 1      1|0 0 0 0 0 0      1|0 0 0 0 0 0  010
1770  * x|x 1 x x x x     x|1 1 1 1 1 1      x|1 0 1 1 1 1      x|1 0 1 1 1 1  111
1771  * z|0 1 x z L H     z|0 1 1 0 0 1      z|1 0 1 0 1 0      z|0 0 1 1 1 1  001
1772  * L|0 1 x L L x     L|0 1 1 0 0 1      L|1 0 1 1 1 1      L|0 0 1 1 1 1  101
1773  * H|x 1 x H x H     H|1 1 1 1 1 1      H|1 0 1 0 1 0      H|1 0 1 1 1 1  011
1774  */
Value_wor(Value * R,Value * A,Value * B)1775 void Value_wor(Value *R,Value *A,Value *B)
1776 {
1777   int wc = SSNUMWORDS(R->nbits);
1778   register int i;
1779 
1780   for (i = 0;i < wc;i++) {
1781     register unsigned RisOne = A->one[i]|B->one[i];
1782     register unsigned RisZero = A->zero[i]|B->zero[i];
1783     register unsigned RhasFloat = (A->flt[i]&B->flt[i]);
1784     register unsigned Rone = (A->one[i]&~A->zero[i]&~A->flt[i]) | (B->one[i]&~B->zero[i]&~B->flt[i]);
1785 
1786     R->one[i]  = RisOne;
1787     R->zero[i] = RisZero & ~Rone;
1788     R->flt[i]  = (~(RisOne ^ RisZero) | RhasFloat) & ~Rone;
1789   }
1790 }
1791 
1792 
1793 /*****************************************************************************
1794  *
1795  *  Wire merge function for "tri0"
1796  *
1797  * Parameters:
1798  *      R		Return value
1799  *      A		Wire A
1800  *      B		Wire B
1801  *
1802  *TRI0
1803  *   0 1 x z L H
1804  *  +-----------
1805  * 0|0 x x 0 0 x
1806  * 1|x 1 x 1 x 1
1807  * x|x x x x x x
1808  * z|0 1 x 0 0 x
1809  * L|0 x x 0 0 x
1810  * H|x 1 x x x x
1811  */
Value_tri0(Value * R,Value * A,Value * B)1812 void Value_tri0(Value *R,Value *A,Value *B)
1813 {
1814   int wc = SSNUMWORDS(R->nbits);
1815   register int i;
1816 
1817   for (i = 0;i < wc;i++) {
1818     register unsigned RisOne = A->one[i]|B->one[i];
1819     register unsigned RisZero = A->zero[i]|B->zero[i];
1820     register unsigned RhasFloat = (A->flt[i]&B->flt[i]);
1821     register unsigned Rconflict = RisOne & (RisZero | RhasFloat);
1822 
1823     R->one[i]  = Rconflict | RisOne;
1824     R->zero[i] = Rconflict | ~RisOne;
1825     R->flt[i]  = Rconflict;
1826   }
1827 }
1828 
1829 /*****************************************************************************
1830  *
1831  *  Wire merge function for "tri1"
1832  *
1833  * Parameters:
1834  *      R		Return value
1835  *      A		Wire A
1836  *      B		Wire B
1837  *
1838  *TRI1
1839  *   0 1 x z L H
1840  *  +-----------
1841  * 0|0 x x 0 0 x
1842  * 1|x 1 x 1 x 1
1843  * x|x x x x x x
1844  * z|0 1 x 1 x 1
1845  * L|0 x x x x x
1846  * H|x 1 x 1 x 1
1847  */
Value_tri1(Value * R,Value * A,Value * B)1848 void Value_tri1(Value *R,Value *A,Value *B)
1849 {
1850   int wc = SSNUMWORDS(R->nbits);
1851   register int i;
1852 
1853   for (i = 0;i < wc;i++) {
1854     register unsigned RisOne = A->one[i]|B->one[i];
1855     register unsigned RisZero = A->zero[i]|B->zero[i];
1856     register unsigned RhasFloat = (A->flt[i]&B->flt[i]);
1857     register unsigned Rconflict = RisZero & (RisOne | RhasFloat);
1858 
1859     R->one[i]  = Rconflict | ~RisZero;
1860     R->zero[i] = Rconflict | RisZero;
1861     R->flt[i]  = Rconflict;
1862   }
1863 }
1864 
1865 /*****************************************************************************
1866  *
1867  *  Wire assignment function for trireg wires
1868  *
1869  * Parameters:
1870  *      R		Return value
1871  *      A		Driven value
1872  *      B		Current net value
1873  *
1874  *TRIREG
1875  *          (A)          one                zero                flt
1876  *       0 1 x z L H       0 1 x z L H        0 1 x z L H        0 1 x z L H  01z
1877  *      +-----------      +-----------       +-----------       +-----------  ---
1878  *     0|0 1 x 0 0 x     0|0 1 1 0 0 1      0|1 0 1 1 1 1      0|0 0 1 0 0 1  100
1879  *     1|0 1 x 1 x 1     1|0 1 1 1 1 1      1|1 0 1 0 1 0      1|0 0 1 0 1 0  010
1880  * (B) x|0 1 x x x x     x|0 1 1 1 1 1      x|1 0 1 1 1 1      x|0 0 1 1 1 1  111
1881  *     z|0 1 x z L H     z|0 1 1 0 0 1      z|1 0 1 0 1 0      z|0 0 1 1 1 1  001
1882  *     L|0 1 x L L x     L|0 1 1 0 0 1      L|1 0 1 1 1 1      L|0 0 1 1 1 1  101
1883  *     H|0 1 x H x H     H|0 1 1 1 1 1      H|1 0 1 0 1 0      H|0 0 1 1 1 1  011
1884  */
Value_trireg(Value * R,Value * A,Value * B)1885 void Value_trireg(Value *R,Value *A,Value *B)
1886 {
1887   int wc = SSNUMWORDS(R->nbits);
1888   register int i;
1889 
1890   for (i = 0;i < wc;i++) {
1891     register unsigned Aone = A->one[i];
1892     register unsigned Azero = A->zero[i];
1893     register unsigned Afloat = A->flt[i];
1894     register unsigned Bzero = B->zero[i];
1895     register unsigned Bone = B->one[i];
1896     register unsigned Bfloat = B->flt[i];
1897 
1898     R->one[i]  = (~Afloat & Aone)  | (Afloat & ~(~Bone & ~Aone));
1899     R->zero[i] = (~Afloat & Azero) | (Afloat & ~(~Bzero & ~Azero));
1900     R->flt[i]  = (Afloat & Bfloat) |  (R->one[i]&R->zero[i]);
1901   }
1902 }
1903 
1904 
1905 /*****************************************************************************
1906  *
1907  * Shift I and place the result in R.
1908  *
1909  * Parameters:
1910  *
1911  *    R		Return value from operation
1912  *    I		Value to be shifted
1913  *    n		Bits to shift (>0 for left shift, <0 for right shift)
1914  *    in1	Shift-in value to use in 'one' field
1915  *    in0	Shift-in value to use in 'zero' field
1916  *    inZ	Shift-in value to use in 'flt' field
1917  *
1918  *****************************************************************************/
Value_roll(Value * R,Value * I,int n)1919 void Value_roll(Value *R,Value *I,int n)
1920 {
1921   int bits = R->nbits;
1922   unsigned mask = (bits<SSWORDSIZE) ? ((1<<bits)-1) : ~0;
1923 
1924   if (n < 0)
1925     n += bits;
1926 
1927   assert(I->nbits <= SSWORDSIZE);
1928   assert(n >= 0);
1929 
1930   if (n > 0) {
1931     unsigned emask = (n<SSWORDSIZE) ? ((1<<n)-1) : ~0;
1932 
1933     R->one[0] = (I->one[0] << n) | ((I->one[0] >> (bits-n)) & emask);
1934     R->zero[0] = (I->zero[0] << n) | ((I->zero[0] >> (bits-n)) & emask);
1935     R->flt[0] = (I->flt[0] << n) | ((I->flt[0] >> (bits-n)) & emask);
1936   } else {
1937     R->one[0] = I->one[0];
1938     R->zero[0] = I->zero[0];
1939     R->flt[0] = I->flt[0];
1940   }
1941 
1942   R->one[0] &= mask;
1943   R->zero[0] &= mask;
1944   R->flt[0] &= mask;
1945 }
1946 
1947 
1948 /*****************************************************************************
1949  *
1950  * Shift I and place the result in R.
1951  *  for nbits <= SSWORDSIZE
1952  *
1953  * Parameters:
1954  *
1955  *    R		Return value from operation
1956  *    I		Value to be shifted
1957  *    n		Bits to shift (>0 for left shift, <0 for right shift)
1958  *    in1	Shift-in value to use in 'one' field
1959  *    in0	Shift-in value to use in 'zero' field
1960  *    inZ	Shift-in value to use in 'flt' field
1961  *
1962  *****************************************************************************/
Value_w_shift(Value * R,Value * I,int n,int in1,int in0,int inZ)1963 void Value_w_shift(Value *R,Value *I,int n,int in1,int in0,int inZ)
1964 {
1965   unsigned mask = LMASK(R->nbits);
1966 
1967   /* special case */
1968   if (abs(n) >= R->nbits) {
1969     Value_zero(R);
1970     return;
1971   }
1972 
1973   if (n > 0) {
1974     unsigned emask = (n<SSWORDSIZE) ? ((1<<n)-1) : ~0;
1975 
1976     R->one[0] = (I->one[0] << n) | (in1*emask);
1977     R->zero[0] = (I->zero[0] << n) | (in0*emask);
1978     R->flt[0] = (I->flt[0] << n) | (inZ*emask);
1979   } else if (n < 0) {
1980     unsigned emask;
1981 
1982     n = -n;
1983     emask = ((n<SSWORDSIZE) ? ((1<<n)-1) : ~0) << (R->nbits-n);
1984 
1985     I->one[0] &= mask;
1986     I->zero[0] &= mask;
1987     I->flt[0] &= mask;
1988 
1989     R->one[0] = (I->one[0] >> n) | (in1*emask);
1990     R->zero[0] = (I->zero[0] >> n) | (in0*emask);
1991     R->flt[0] = (I->flt[0] >> n) | (inZ*emask);
1992   } else {
1993     R->one[0] = I->one[0];
1994     R->zero[0] = I->zero[0];
1995     R->flt[0] = I->flt[0];
1996   }
1997 
1998   R->one[0] &= mask;
1999   R->zero[0] &= mask;
2000   R->flt[0] &= mask;
2001 }
2002 
2003 /*****************************************************************************
2004  *
2005  * same, for nbits > SSWORDSIZE
2006  *
2007  *****************************************************************************/
Value_shift(Value * R,Value * I,int n,int in1,int in0,int inZ)2008 void Value_shift(Value *R,Value *I,int n,int in1,int in0,int inZ) {
2009   int na = abs(n);
2010   int shift = na & SSBITMASK;
2011   int ishift = SSWORDSIZE - shift;
2012   int wordshift = na >> SSWORDSHIFT;
2013 
2014   int wc = SSNUMWORDS(R->nbits);
2015   int i;
2016 
2017   /*
2018      src represents the word we're copying in from
2019      prv is the one before that
2020      if there aren't enough words there, they point to in
2021 
2022      for a left shift:
2023      destword srcword prvword
2024 
2025      Both start at in1/in0/inZ until 'in range' to read from I[i-wordshift], etc..
2026   */
2027   unsigned src1 = in1 ? ~0 : 0;
2028   unsigned src0 = in0 ? ~0 : 0;
2029   unsigned srcZ = inZ ? ~0 : 0;
2030   unsigned prv1 = src1;
2031   unsigned prv0 = src0;
2032   unsigned prvZ = srcZ;
2033 
2034   if (n > 0) {
2035     /* left shift */
2036     for (i = 0; i < wc; i++) {
2037       if (i - wordshift >= 0) {
2038 	src1 = I->one[i - wordshift];
2039 	src0 = I->zero[i - wordshift];
2040 	srcZ = I->flt[i - wordshift];
2041       }
2042       if (!shift)
2043 	prv1 = prv0 = prvZ = 0;
2044       else if (i - wordshift - 1 >= 0) {
2045 	prv1 = I->one[i - wordshift - 1];
2046 	prv0 = I->zero[i - wordshift - 1];
2047 	prvZ = I->flt[i - wordshift - 1];
2048       }
2049 
2050       R->one[i] = (src1 << shift) | (prv1 >> ishift);
2051       R->zero[i] = (src0 << shift) | (prv0 >> ishift);
2052       R->flt[i] = (srcZ << shift) | (prvZ >> ishift);
2053     }
2054 
2055   } else if (n < 0) {
2056     /* right shift */
2057     for (i = wc-1; i >= 0; i--) {
2058       if (i + wordshift < wc) {
2059 	src1 = I->one[i + wordshift];
2060 	src0 = I->zero[i + wordshift];
2061 	srcZ = I->flt[i + wordshift];
2062       }
2063       if (!shift)
2064 	prv1 = prv0 = prvZ = 0;
2065       else if (i + wordshift + 1 < wc) {
2066 	prv1 = I->one[i + wordshift + 1];
2067 	prv0 = I->zero[i + wordshift + 1];
2068 	prvZ = I->flt[i + wordshift + 1];
2069       }
2070 
2071       R->one[i] = (prv1 << ishift) | (src1 >> shift);
2072       R->zero[i] = (prv0 << ishift) | (src0 >> shift);
2073       R->flt[i] = (prvZ << ishift) | (srcZ >> shift);
2074     }
2075 
2076   } else {
2077     for (i=0; i<wc; i++) {
2078       R->one[i] = I->one[i];
2079       R->zero[i] = I->zero[i];
2080       R->flt[i] = I->flt[i];
2081     }
2082   }
2083 }
2084