; *************************************************************************** ; copyright : (C) 2025 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_DIVIDE8_H #define COMMON_DIVIDE8_H ; --------------------------------------------------------------------------- ; @routine Utils_Divu8_8_8 ; ; Divides two unsigned 8 bit values resulting in one 8 bit value. ; This is a rather simple but reasonable fast routine working just like you would ; when doing divisions with pen and paper. ; ; R16 = R18 / R19 ; R17 = R18 mod R19 ; TODO: adapt for signed values ; ; @param R18 8 bit value A ; @param R19 8 bit value B ; @return R16 8 bit result ; @return R17 8 bit remainder ; @clobbers R25 Utils_Divu8_8_8: mov r16, r18 ; r16=result (intitially: dividend) clr r17 ; r17=remainder ldi r25, 8 ; 8 bits to divide Utils_Divu8_8_8_loop: lsl r16 ; shift 0 bit into result rol r17 sub r17, r19 ; try to subtract divisor from value brcs Utils_Divu8_8_8_nofit ; jmp if dividend < divisor ori r16, 1 ; otherwise set bit in result rjmp Utils_Divu8_8_8_loop_end Utils_Divu8_8_8_nofit: add r17, r19 ; undo subtraction ; r16=result Utils_Divu8_8_8_loop_end: dec r25 brne Utils_Divu8_8_8_loop ret ; @end #endif