|
| 1 | + assume adl=1 |
| 2 | + |
| 3 | + section .text |
| 4 | + |
| 5 | + public __imulhu |
| 6 | + |
| 7 | +; UHL = ((uint48_t)UHL * (uint48_t)UBC) >> 24 |
| 8 | +__imulhu: |
| 9 | +; TODO: Optimize this routine as this is mostly just a copy paste of __i48mulu with some stuff removed. |
| 10 | +; |
| 11 | +; CC: 118*r(PC)+39*r(SPL)+38*w(SPL)+37 |
| 12 | +; CC: 117 bytes | 118F + 39R + 38W + 37 |
| 13 | + push de |
| 14 | + ; backup af |
| 15 | + push af |
| 16 | + push ix |
| 17 | + ld ix, 0 |
| 18 | + add ix, sp |
| 19 | + |
| 20 | + ; On stack to get upper byte when needed |
| 21 | + push de ; de will also be used to perform the actual multiplication |
| 22 | + push hl |
| 23 | + push iy |
| 24 | + push bc |
| 25 | + |
| 26 | + ; bc = a[0], a[1] |
| 27 | + ld a, l ; a = b[0] |
| 28 | + ld iy, (ix - 5) ; iy = b[1], b[2] |
| 29 | + |
| 30 | + ; or a, a ; carry is already cleared |
| 31 | + sbc hl, hl |
| 32 | + push hl ; upper bytes of sum at -15 |
| 33 | + ; Stack Use: |
| 34 | + ; ix-1 : deu b[5] |
| 35 | + ; ix-2 : d b[4] |
| 36 | + ; ix-3 : e b[3] |
| 37 | + ; ix-4 : hlu b[2] |
| 38 | + ; ix-5 : h b[1] |
| 39 | + ; ix-6 : l b[0] |
| 40 | + ; ix-7 : iyu a[5] |
| 41 | + ; ix-8 : iyh a[4] |
| 42 | + ; ix-9 : iyl a[3] |
| 43 | + ; ix-10 : bcu a[2] |
| 44 | + ; ix-11 : b a[1] |
| 45 | + ; ix-12 : c a[0] |
| 46 | + ; ix-13 : sum[5] |
| 47 | + ; ix-14 : sum[4] |
| 48 | + ; ix-15 : sum[3] |
| 49 | + ; ix-16 : sum[2] |
| 50 | + ; ix-17 : sum[1] |
| 51 | + ; ix-18 : sum[0] |
| 52 | + |
| 53 | + ; ====================================================================== |
| 54 | + ; sum[0-1] |
| 55 | + |
| 56 | + ; a[0]*b[0] |
| 57 | + ld d, c ; d = a[0] |
| 58 | + ld e, a ; e = b[0] |
| 59 | + mlt de |
| 60 | + push de ; lower bytes of sum at -18 |
| 61 | + |
| 62 | + ; ====================================================================== |
| 63 | + ; sum[1-2] |
| 64 | + ld l, d ; hl will store current partial sum |
| 65 | + |
| 66 | + ; a[1]*b[0] |
| 67 | + ld d, b ; d = a[1] |
| 68 | + ld e, a ; e = b[0] |
| 69 | + mlt de |
| 70 | + add hl, de |
| 71 | + |
| 72 | + ; a[0]*b[1] |
| 73 | + ld d, c ; d = a[0] |
| 74 | + ld e, iyl ; e = b[1] |
| 75 | + mlt de |
| 76 | + add hl, de |
| 77 | + |
| 78 | + ld (ix - 17), hl |
| 79 | + |
| 80 | + ; ====================================================================== |
| 81 | + ; sum[2-3] |
| 82 | + ld hl, (ix - 16) ; hl will store current partial sum |
| 83 | + |
| 84 | + ; a[0]*b[2] |
| 85 | + ld d, c ; d = a[0] |
| 86 | + ld e, iyh ; e = b[2] |
| 87 | + mlt de |
| 88 | + add hl, de |
| 89 | + |
| 90 | + ; a[1]*b[1] |
| 91 | + ld d, b ; d = a[1] |
| 92 | + ld e, iyl ; e = b[1] |
| 93 | + mlt de |
| 94 | + add hl, de |
| 95 | + |
| 96 | + ; a[2]*b[0] |
| 97 | + ld d, (ix - 10) ; d = a[2] |
| 98 | + ld e, a ; e = b[0] |
| 99 | + mlt de |
| 100 | + add hl, de |
| 101 | + |
| 102 | + ld (ix - 16), hl |
| 103 | + |
| 104 | + ; ====================================================================== |
| 105 | + ; sum[3-4] |
| 106 | + ld hl, (ix - 15) ; hl will store current partial sum |
| 107 | + |
| 108 | + ; a[1]*b[2] |
| 109 | + ld d, b ; d = a[1] |
| 110 | + ld e, iyh ; e = b[2] |
| 111 | + mlt de |
| 112 | + add hl, de |
| 113 | + |
| 114 | + ; a[2]*b[1] |
| 115 | + ld d, (ix - 10) ; d = a[2] |
| 116 | + ld e, iyl ; e = b[1] |
| 117 | + mlt de |
| 118 | + add hl, de |
| 119 | + |
| 120 | + ld (ix - 15), hl |
| 121 | + |
| 122 | + ; ====================================================================== |
| 123 | + ; sum[4-5] |
| 124 | + ld hl, (ix - 14) ; hl will store current partial sum |
| 125 | + |
| 126 | + ; a[2]*b[2] |
| 127 | + ld d, (ix - 10) ; d = a[2] |
| 128 | + ld e, iyh ; e = b[2] |
| 129 | + mlt de |
| 130 | + add hl, de |
| 131 | + |
| 132 | + ld (ix - 14), l |
| 133 | + ld (ix - 13), h |
| 134 | + |
| 135 | + ; clean up stack and restore registers |
| 136 | + pop de |
| 137 | + pop hl |
| 138 | + pop bc |
| 139 | + pop iy |
| 140 | + |
| 141 | + ld sp, ix |
| 142 | + pop ix |
| 143 | + pop af |
| 144 | + pop de |
| 145 | + ret |
0 commit comments