XJTAG

Apply for your FREE Trial of JTAG Boundary 
		Scan Development and Test Tools
Search

Handling Dynamic Width Integers in XJEase

Introduction

Unlike most conventional programming languages, XJEase supports dynamic-width integers, which can be up to 2^32 (over four billion) bits wide. Dealing with these integers can cause a significant speed reduction if not carefully considered. This article describes this feature and a technique for avoiding these situations.

Dynamic-Width Integers

Integers in XJEase can be declared to be of any width up to 2^32 bits, or will default to a single bit width if the width is not explicitly declared (early versions of XJEase defaulted to 32 bit wide integers). Integers in XJEase can only increase in width and will never shrink (unless explicitly coded using the bit-slice operator) so that information cannot be accidentally lost in XJEase operations.

Integers will always grow to fit the size of the result of an operation; thus shifting an integer left by one will always cause it to increase its width by one bit (leading zeros are not truncated), and multiplying an integer will increase the width to a value sufficient to hold the result. This should be borne in mind when manipulating integers, as resizing of variables requires both processor time and additional memory and can thus reduce code performance.

An Example

As an example, the following code will cause the variable shift_reg to increase in size by one bit each time the loop is executed, as the left shift operation will require an additional bit to store the result and this will never be removed.

Deserialiser () (INT output)
  INT shift_reg WIDTH 8;
  INT i;
  INT bit WIDTH 1;

  FOR i := 1 FOR 8
    SET bit := INPUT_PIN;
    shift_reg := (shift_reg << 1) | bit;
  END;

  output := shift_reg[7..0];
END;

This behaviour can have a significant effect on the performance of XJEase code, and can be avoided if the code is modified as shown below:

Deserialiser () (INT output)
  INT shift_reg WIDTH 8;
  INT i;
  INT bit WIDTH 1;

  FOR i := 1 FOR 8
    SET bit := INPUT_PIN;
    shift_reg := shift_reg[6..0] : bit;
  END;

  output := shift_reg;
END;

Here the bit-slice and concatenation operators ([6..0] and : respectively) have been used to ensure that the variable shift_reg never grows to more than eight bits wide. This saves the processing time which would have been spent resizing the shift_reg variable on each iteration of the loop.

Another pitfall for the unwary arising from the use of dynamic-width integers is using the WIDTHOF expression on a dynamic-width variable. For example, the following code will generate an infinite loop, as the WIDTHOF operator will return an ever-increasing value on each iteration of the loop.

Deserialiser () (INT output)
  INT shift_reg WIDTH 8;
  INT i;
  INT bit WIDTH 1;

  FOR i := 1 FOR WIDTHOF(shift_reg)
    SET bit := INPUT_PIN;
    shift_reg := (shift_reg << 1) | bit;
  END;

  output := shift_reg;
END;

Conclusion

Remember that XJEase variables will automatically grow in width if required to accommodate the result of any operation. This unnoticed behaviour can consume significant processor resources, but can usually be avoided with careful coding techniques.

Knowledge Base Index |