09.07.2015 Views

6. Digital simulation in Alecsis - LEDA

6. Digital simulation in Alecsis - LEDA

6. Digital simulation in Alecsis - LEDA

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>6.</strong> <strong>Digital</strong> <strong>simulation</strong> <strong>in</strong> <strong>Alecsis</strong> 87<strong>6.</strong>1.4.1. Operator lengthofThis operator is syntactically very similar to the operator sizeof. It is a unary operator whose operandneeds to be a vector of enumeration type. (vector of signals or vector of variables). The vector can be a formalvariable, local variable, or a formal/local signal. In the case of a formal variable, AleC++ passes as a hiddenparameter the length of the actual argument extracted by the operator. The situation is much simpler with signals --a special <strong>in</strong>struction of the <strong>Alecsis</strong> virtual processor that returns the length of the signal-vector. Note that operand oflengthof operator cannot be a local po<strong>in</strong>ter, because lengthof would return 0 <strong>in</strong> that case.If a vector is declared with <strong>in</strong>verse dimensionality, the lengthof returns the negative value of its length.Usually, for overload<strong>in</strong>g vector-operands, you need the absolute value of the length. Direction can have animportant role with some specific operators (left shift<strong>in</strong>g >).three_t v1[0:3], v2[3:0];<strong>in</strong>t L1 = lengthof (v1); // L1 is 4<strong>in</strong>t L2 = lengthof (v2); // L2 is -4<strong>6.</strong>1.4.2. Buffers for temporary solutionsLogical operators often return a result, that can itself be an operand <strong>in</strong> a more complex expression. S<strong>in</strong>ce weare deal<strong>in</strong>g with a vector, a temporary memory location needs to be reserved for the result, and another temporarymemory to prevent the overwrite of that result by the new operation. The follow<strong>in</strong>g example illustrates this:# def<strong>in</strong>e Vreturn(v,size) {asm movq.l %d4, size; return v; }# def<strong>in</strong>e BUFF_SIZE 512three_t _op_buff[BUFF_SIZE], _res_buff[BUFF_SIZE];three_t *operator& (three_t *op1, three_t *op2) {<strong>in</strong>t size1 = abs (lengthof op1);<strong>in</strong>t size2 = abs (lengthof op2);if (size1 != size2) {pr<strong>in</strong>tf("<strong>in</strong>compatible lengths - op &, type three_t\n");exit(1);}for (<strong>in</strong>t i=0; i< size1; i++) _op_buff[i] = op1[i] & op2[i];memcpy(_res_buff, _op_buff, size1);Vreturn(_res_buff, size1)}three_t *operator= (three_t *op1, three_t *op2) {<strong>in</strong>t size = abs(lengthof op1);memcpy(op1, op2, size);memcpy(_res_buff, op1, size);Vreturn(_res_buff, size)}foo () {static three_t v1[]="0100", v2[]="11x0", v3[4], v4[4];v4 = v3 = v1 & v2;}The example illustrates a concept of us<strong>in</strong>g two buffers -- one for calculations, the other for the return of theresult. In this particular example use of only one buffer would be a satisfactory solution. However, <strong>in</strong> some morecomplex operations it may happen for one of the operands to be a result of an overload operation, which may meanthat we are read<strong>in</strong>g and chang<strong>in</strong>g the same vector simultaneously. That may produce errors that are very difficult to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!