1 | ; $Id: bs3-wc32-U8M.asm 106061 2024-09-16 14:03:52Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - 32-bit Watcom C/C++, 64-bit integer multiplication.
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2024 Oracle and/or its affiliates.
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox base platform packages, as
|
---|
10 | ; available from https://www.virtualbox.org.
|
---|
11 | ;
|
---|
12 | ; This program is free software; you can redistribute it and/or
|
---|
13 | ; modify it under the terms of the GNU General Public License
|
---|
14 | ; as published by the Free Software Foundation, in version 3 of the
|
---|
15 | ; License.
|
---|
16 | ;
|
---|
17 | ; This program is distributed in the hope that it will be useful, but
|
---|
18 | ; WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | ; General Public License for more details.
|
---|
21 | ;
|
---|
22 | ; You should have received a copy of the GNU General Public License
|
---|
23 | ; along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | ;
|
---|
25 | ; The contents of this file may alternatively be used under the terms
|
---|
26 | ; of the Common Development and Distribution License Version 1.0
|
---|
27 | ; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | ; in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | ; CDDL are applicable instead of those of the GPL.
|
---|
30 | ;
|
---|
31 | ; You may elect to license modified versions of this file under the
|
---|
32 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | ;
|
---|
34 | ; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | ;
|
---|
36 |
|
---|
37 | %include "bs3kit-template-header.mac"
|
---|
38 |
|
---|
39 |
|
---|
40 | ;;
|
---|
41 | ; 64-bit unsigned & signed integer multiplication.
|
---|
42 | ;
|
---|
43 | ; @returns EDX:EAX as the product.
|
---|
44 | ; @param EDX:EAX Factor 1 - edx=F1H, eax=F1L.
|
---|
45 | ; @param ECX:EBX Factor 2 - ecx=F2H, ebx=F2L.
|
---|
46 | ;
|
---|
47 | global $??I8M
|
---|
48 | $??I8M:
|
---|
49 | global $??U8M
|
---|
50 | $??U8M:
|
---|
51 | ;
|
---|
52 | ; If both the high dwords are zero, we can get away with
|
---|
53 | ; a simple 32-bit multiplication.
|
---|
54 | ;
|
---|
55 | test ecx, ecx
|
---|
56 | jnz .big
|
---|
57 | test edx, edx
|
---|
58 | jnz .big
|
---|
59 | mul ebx
|
---|
60 | ret
|
---|
61 |
|
---|
62 | .big:
|
---|
63 | ;
|
---|
64 | ; Imagine we use 4294967296-base (2^32), so each factor has two
|
---|
65 | ; digits H and L, thus we have: F1H:F1L * F2H:F1L which we can
|
---|
66 | ; multipy like we learned in primary school. Since the result
|
---|
67 | ; is limited to 64-bit, we can skip F1H*F2H and discard the
|
---|
68 | ; high 32-bit in F1L*F2H and F1H*F2L.
|
---|
69 | ; result = ((F1L*F2H) << 32)
|
---|
70 | ; + ((F1H*F2L) << 32)
|
---|
71 | ; + (F1L*F2L);
|
---|
72 | ;
|
---|
73 | push ecx ; Preserve ECX just to be nice.
|
---|
74 | push eax ; Stash F1L for later.
|
---|
75 | push edx ; Stash F1H for later.
|
---|
76 |
|
---|
77 | ; ECX = F1L*F2H
|
---|
78 | mul ecx
|
---|
79 | mov ecx, eax
|
---|
80 |
|
---|
81 | ; ECX += F1H * F2L
|
---|
82 | pop eax
|
---|
83 | mul ebx
|
---|
84 | add ecx, eax
|
---|
85 |
|
---|
86 | ; EDX:EAX = F1L * F2L
|
---|
87 | pop eax
|
---|
88 | mul ebx
|
---|
89 | add edx, ecx
|
---|
90 |
|
---|
91 | pop ecx
|
---|
92 | ret
|
---|
93 |
|
---|