Line 1... |
Line 1... |
; Copyright 2014, Sinclair R.F., Inc.
|
; Copyright 2014, Sinclair R.F., Inc.
|
;
|
;
|
; Unsigned arithmetic operations.
|
; Unsigned arithmetic operations.
|
|
|
|
; Notation:
|
|
; ux_n is the n'th byte of ux where n=0 is the LSB
|
|
; example: ( u0_0 u0_1 ) are the LSB and MSB of a 2-byte 16-bit value.
|
|
; u0 and u1 are two input vectors, us is their sum
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;
|
;
|
; Add two unsigned 8-bit values to produce an unsigned 16-bit value.
|
; Add two unsigned 8-bit values to produce an unsigned 16-bit value.
|
; Method: Calculate the sum of the msb of the two raw values and the msb of the
|
; Method: calculate and push the carry bit onto the return stack, calculate the
|
; sums of the 7 lsbs of the two values to get the msb of the sum and
|
; 8-bit sum of the two 8-bit values, use the previously stored and
|
; the lsb of the MSB of the 16-bit sum.
|
; computed carry bit as the MSB of the 16-bit return value.
|
; 36 instructions
|
; 6 instructions
|
;
|
;
|
; ( u1 u2 - (u1+u2)_LSB (u1+u2)_MSB )
|
; ( u0 u1 - us_0 us_1 )
|
.function math__add_u8_u8_u16
|
.function math__add_u8_u8_u16
|
; and the two 7 lsbs and put the 7 lsb of that sum on the return stack
|
; ( u_0 u_1 - u_0 u_1 ) r:( - c )
|
over 0x7F & over 0x7F & + dup 0x7F & >r
|
; ( u0 u1 - us_0 us_1 )
|
; add the msb of the sum of the 7 lsbs and the two inputs
|
+c >r + r>
|
0x80 & <<msb swap 0x80 & <<msb + swap 0x80 & <<msb +
|
.return
|
; construct the MSB of the sum as bit 1 of the sums of the msbs
|
|
dup 0>> swap
|
|
; set the msb of the LSB if the lsb of the sum of the msbs is non-zero
|
|
0x01 & 0<> 0x80 & r> or
|
|
; swap the orders so that the MSB is on the top of the data stack
|
|
.return(swap)
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
; miscellaneous unsigned addition operations
|
; miscellaneous unsigned addition operations
|
|
|
|
; ( u0_0 u0_1 u0 - us_0 us_1 )
|
|
; 9 instructions
|
.function math__add_u16_u8_u16
|
.function math__add_u16_u8_u16
|
swap >r .call(math__add_u8_u8_u16) r> .return(+)
|
; ( u0_0 u0_1 u1 - u0_0 u1 ) r:( - u0_1 )
|
|
swap >r
|
|
; ( u0_0 u1 - us_0 c_0 )
|
|
+c >r + r>
|
|
; ( c_0 - us_1 ) r:( u0_1 )
|
|
r> .return(+)
|
|
|
|
; ( u0_0 u0_1 u1 - us_0 us_1 us_2 )
|
|
; 13 instructions
|
.function math__add_u16_u8_u24
|
.function math__add_u16_u8_u24
|
swap >r .call(math__add_u8_u8_u16) r> .call(math__add_u8_u8_u16) .return
|
; ( u0_0 u0_1 u1 - u0_0 u1 ) r:( - u0_1 )
|
|
swap >r
|
|
; ( u0_0 u1 - us_0 c_0 )
|
|
+c >r + r>
|
|
; ( c_0 - us_1 us_2 ) r:( u0_1 - )
|
|
r> +c >r + r>
|
|
.return
|
|
|
|
; ( u0_0 u0_1 u0_2 u1 - us_0 us_1 us_2 )
|
.function math__add_u24_u8_u24
|
.function math__add_u24_u8_u24
|
swap >r .call(math__add_u16_u8_u24)
|
swap >r .call(math__add_u16_u8_u24)
|
r> .return(+)
|
r> .return(+)
|
|
|
|
; ( u0_0 u0_1 u0_2 u1 - us_0 us_1 us_2 us_3 )
|
.function math__add_u24_u8_u32
|
.function math__add_u24_u8_u32
|
swap >r .call(math__add_u16_u8_u24)
|
swap >r .call(math__add_u16_u8_u24)
|
r> .call(math__add_u8_u8_u16)
|
r> .call(math__add_u8_u8_u16)
|
.return
|
.return
|
|
|