1 | ; $Id: ASMBitLastSetU64.asm 106061 2024-09-16 14:03:52Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; IPRT - ASMBitLastSetU64().
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2006-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 |
|
---|
38 | ;*******************************************************************************
|
---|
39 | ;* Header Files *
|
---|
40 | ;*******************************************************************************
|
---|
41 | %include "iprt/asmdefs.mac"
|
---|
42 |
|
---|
43 | BEGINCODE
|
---|
44 |
|
---|
45 | ;;
|
---|
46 | ; Finds the last bit which is set in the given 64-bit integer.
|
---|
47 | ;
|
---|
48 | ; Bits are numbered from 1 (least significant) to 64.
|
---|
49 | ;
|
---|
50 | ; @returns (xAX) index [1..64] of the last set bit.
|
---|
51 | ; @returns (xAX) 0 if all bits are cleared.
|
---|
52 | ; @param msc:rcx gcc:rdi x86:stack u64 Integer to search for set bits.
|
---|
53 | ;
|
---|
54 | ; @cproto DECLASM(unsigned) ASMBitLastSetU64(uint64_t u64);
|
---|
55 | ;
|
---|
56 | RT_BEGINPROC ASMBitLastSetU64
|
---|
57 | %if ARCH_BITS == 16
|
---|
58 | CPU 8086
|
---|
59 | push bp
|
---|
60 | mov bp, sp
|
---|
61 |
|
---|
62 | ; 63:48
|
---|
63 | mov ax, 64
|
---|
64 | mov cx, [bp + 2 + 2 + 6]
|
---|
65 | test cx, cx
|
---|
66 | jnz .next_bit
|
---|
67 |
|
---|
68 | ; 47:32
|
---|
69 | mov al, 48
|
---|
70 | or cx, [bp + 2 + 2 + 4]
|
---|
71 | jnz .next_bit
|
---|
72 |
|
---|
73 | ; 31:16
|
---|
74 | mov al, 32
|
---|
75 | or cx, [bp + 2 + 2 + 2]
|
---|
76 | jnz .next_bit
|
---|
77 |
|
---|
78 | ; 16:0
|
---|
79 | mov al, 16
|
---|
80 | or cx, [bp + 2 + 2 + 0]
|
---|
81 | jz .return_zero
|
---|
82 |
|
---|
83 | ; find the bit that was set.
|
---|
84 | .next_bit:
|
---|
85 | shl cx, 1
|
---|
86 | jc .return
|
---|
87 | dec ax
|
---|
88 | jmp .next_bit
|
---|
89 |
|
---|
90 | .return_zero:
|
---|
91 | xor ax, ax
|
---|
92 | .return:
|
---|
93 | pop bp
|
---|
94 | ret
|
---|
95 |
|
---|
96 | %else
|
---|
97 | %if ARCH_BITS == 64
|
---|
98 | %ifdef ASM_CALL64_GCC
|
---|
99 | bsr rax, rsi
|
---|
100 | %else
|
---|
101 | bsr rax, rcx
|
---|
102 | %endif
|
---|
103 | jz .return_zero
|
---|
104 | inc eax
|
---|
105 | .return:
|
---|
106 | ret
|
---|
107 |
|
---|
108 | %elif ARCH_BITS == 32
|
---|
109 | ; Check the 2nd dword then the first one.
|
---|
110 | bsr eax, dword [esp + 4 + 4]
|
---|
111 | jz .check_1st_dword
|
---|
112 | add eax, 32
|
---|
113 | ret
|
---|
114 |
|
---|
115 | .check_1st_dword:
|
---|
116 | bsr eax, dword [esp + 4 + 0]
|
---|
117 | jz .return_zero
|
---|
118 | inc eax
|
---|
119 | ret
|
---|
120 |
|
---|
121 | %else
|
---|
122 | %error "Missing or invalid ARCH_BITS."
|
---|
123 | %endif
|
---|
124 |
|
---|
125 | .return_zero:
|
---|
126 | xor eax, eax
|
---|
127 | ret
|
---|
128 | %endif
|
---|
129 | ENDPROC ASMBitLastSetU64
|
---|
130 |
|
---|