1 | ; $Id: ASMBitFirstSetU64.asm 106061 2024-09-16 14:03:52Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; IPRT - ASMBitFirstSetU64().
|
---|
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 first 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 first 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) ASMBitFirstSetU64(uint64_t u64);
|
---|
55 | ;
|
---|
56 | RT_BEGINPROC ASMBitFirstSetU64
|
---|
57 | %if ARCH_BITS == 16
|
---|
58 | CPU 8086
|
---|
59 | push bp
|
---|
60 | mov bp, sp
|
---|
61 |
|
---|
62 | ; 15:0
|
---|
63 | mov ax, 1
|
---|
64 | mov cx, [bp + 2 + 2 + 0]
|
---|
65 | test cx, cx
|
---|
66 | jnz .next_bit
|
---|
67 |
|
---|
68 | ; 31:16
|
---|
69 | mov al, 16
|
---|
70 | or cx, [bp + 2 + 2 + 2]
|
---|
71 | jnz .next_bit
|
---|
72 |
|
---|
73 | ; 47:32
|
---|
74 | mov al, 32
|
---|
75 | or cx, [bp + 2 + 2 + 4]
|
---|
76 | jnz .next_bit
|
---|
77 |
|
---|
78 | ; 63:48
|
---|
79 | mov al, 48
|
---|
80 | or cx, [bp + 2 + 2 + 6]
|
---|
81 | jz .return_zero
|
---|
82 |
|
---|
83 | ; find the bit that was set.
|
---|
84 | .next_bit:
|
---|
85 | shr cx, 1
|
---|
86 | jc .return
|
---|
87 | inc 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 | bsf rax, rsi
|
---|
100 | %else
|
---|
101 | bsf rax, rcx
|
---|
102 | %endif
|
---|
103 | jz .return_zero
|
---|
104 | inc eax
|
---|
105 | ret
|
---|
106 |
|
---|
107 | %elif ARCH_BITS == 32
|
---|
108 | ; Check the first dword then the 2nd one.
|
---|
109 | bsf eax, dword [esp + 4 + 0]
|
---|
110 | jnz .check_2nd_dword
|
---|
111 | inc eax
|
---|
112 | ret
|
---|
113 | .check_2nd_dword:
|
---|
114 | bsf eax, dword [esp + 4 + 4]
|
---|
115 | jz .return_zero
|
---|
116 | add eax, 32
|
---|
117 | ret
|
---|
118 | %else
|
---|
119 | %error "Missing or invalid ARCH_BITS."
|
---|
120 | %endif
|
---|
121 | .return_zero:
|
---|
122 | xor eax, eax
|
---|
123 | ret
|
---|
124 | %endif
|
---|
125 | ENDPROC ASMBitFirstSetU64
|
---|
126 |
|
---|