]> git.sur5r.net Git - cc65/blob - libsrc/specialmath/mul40.s
Optimized mul20 & mul40 and extracted to new library.
[cc65] / libsrc / specialmath / mul40.s
1 ; mul40.s
2 ;
3 ; This file is part of
4 ; cc65 - a freeware C compiler for 6502 based systems
5 ;
6 ; https://github.com/cc65/cc65
7 ;
8 ; See "LICENSE" file for legal information.
9 ;
10 ;
11 ; unsigned int __fastcall__ mul40(unsigned char value);
12
13 ; REMARKS: Function is defined to return with carry-flag cleared
14
15
16         .importzp       tmp4
17         .export         _mul40
18
19 .proc   _mul40                  ; = 33 bytes, 48/53 cycles
20
21         sta     tmp4            ; remember value for later addition...
22         ldx     #0              ; clear high-byte
23         asl     a               ; * 2
24         bcc     mul4            ; high-byte affected?
25         ldx     #2              ; this will be the 1st high-bit soon...
26
27 mul4:   asl     a               ; * 4                  
28         bcc     mul5            ; high-byte affected?
29         inx                     ; => yes, apply to 0 high-bit
30         clc                     ; prepare addition
31
32 mul5:   adc     tmp4            ; * 5
33         bcc     mul10           ; high-byte affected?
34         inx                     ; yes, correct...
35
36 mul10:  stx     tmp4            ; continue with classic shifting...
37         
38         asl     a               ; * 10
39         rol     tmp4                                    
40
41         asl     a               ; * 20 
42         rol     tmp4
43
44         asl     a               ; * 40
45         rol     tmp4
46
47         ldx     tmp4            ; deliver high-byte in X
48         rts
49
50 .endproc