; ***************************************************************************
; copyright   : (C) 2026 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************

#ifndef COMMON_MULTIPLY8_H
#define COMMON_MULTIPLY8_H


; ---------------------------------------------------------------------------
; @routine Utils_Mulu8x8_16
;
; Multiplies two unsigned 8 bit values resulting in one 16 bit value.
; This is a rather simple but reasonable fast routine working just like you would
; when doing multiplications with pen and paper.
;
; R17:R16 = R18 * R19
;
; TODO: adapt for signed values
;
; @param  R18     8 bit value A
; @param  R19     8 bit value B
; @return R17:R16 16 bit result
; @clobbers R19, R20, R21

Utils_Mulu8x8_16:
  clr r17
  clr r16
  clr r20
  ldi r21, 8                     ; 8 bit multiplicator
Utils_Mulu8x8_16_loop:
    lsr r19
    brcc Utils_Mulu8x8_16_noadd ; current digit in B is 0, don't add shifted A to result
    add r16, r18
    adc r17, r20
Utils_Mulu8x8_16_noadd:
    dec r21
    breq Utils_Mulu8x8_16_done
    lsl r18
  rjmp Utils_Mulu8x8_16_loop
Utils_Mulu8x8_16_done:
  clc
  ret
; @end





#endif ; COMMON_MULTIPLY8_H

