1 | ; $Id: ASMBitFirstClear.asm 96407 2022-08-22 17:43:14Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; IPRT - ASMBitFirstClear().
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2006-2022 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 clear bit in a bitmap.
|
---|
47 | ;
|
---|
48 | ; @returns (32/64:eax, 16:ax+dx) Index of the first zero bit.
|
---|
49 | ; @returns (32/64:eax, 16:ax+dx) -1 if no clear bit was found.
|
---|
50 | ; @param msc:rcx gcc:rdi pvBitmap Pointer to the bitmap.
|
---|
51 | ; @param msc:edx gcc:rsi cBits The number of bits in the bitmap. Multiple of 32.
|
---|
52 | ;
|
---|
53 | RT_BEGINPROC ASMBitFirstClear
|
---|
54 | ;
|
---|
55 | ; if (cBits)
|
---|
56 | ; Put cBits in ecx first.
|
---|
57 | ;
|
---|
58 | %if ARCH_BITS == 64
|
---|
59 | %ifdef ASM_CALL64_GCC
|
---|
60 | mov ecx, esi
|
---|
61 | %else
|
---|
62 | xchg rcx, rdx ; rdx=pvDst, ecx=cBits
|
---|
63 | %endif
|
---|
64 | %elif ARCH_BITS == 32
|
---|
65 | mov ecx, [esp + 4 + 4]
|
---|
66 | %elif ARCH_BITS == 16
|
---|
67 | push bp
|
---|
68 | mov bp, sp
|
---|
69 | mov ecx, [bp + 4 + 4]
|
---|
70 | %endif
|
---|
71 | or ecx, ecx
|
---|
72 | jz short .failed
|
---|
73 | ;{
|
---|
74 | push xDI
|
---|
75 |
|
---|
76 | ; asm {...}
|
---|
77 | %if ARCH_BITS == 64
|
---|
78 | %ifdef ASM_CALL64_GCC
|
---|
79 | ; rdi = start of scasd - already done
|
---|
80 | %else
|
---|
81 | mov rdi, rdx ; rdi = start of scasd (Note! xchg rdx,rcx above)
|
---|
82 | %endif
|
---|
83 | %elif ARCH_BITS == 32
|
---|
84 | mov edi, [esp + 8]
|
---|
85 | %elif ARCH_BITS == 16
|
---|
86 | mov ax, [bp + 4 + 2]
|
---|
87 | mov di, [bp + 4]
|
---|
88 | mov es, ax ; es is volatile, no need to save.
|
---|
89 | %endif
|
---|
90 | add ecx, 31 ; 32 bit aligned
|
---|
91 | shr ecx, 5 ; number of dwords to scan.
|
---|
92 | mov xDX, xDI ; xDX = saved pvBitmap
|
---|
93 | mov eax, 0ffffffffh
|
---|
94 | repe scasd ; Scan for the first dword with any clear bit.
|
---|
95 | je .failed_restore
|
---|
96 |
|
---|
97 | ; find the bit in question
|
---|
98 | sub xDI, 4 ; one step back.
|
---|
99 | %if ARCH_BITS == 16
|
---|
100 | movzx edi, di
|
---|
101 | xor eax, [es:xDI] ; eax = NOT [rdi]
|
---|
102 | %else
|
---|
103 | xor eax, [xDI] ; eax = NOT [rdi]
|
---|
104 | %endif
|
---|
105 | sub xDI, xDX
|
---|
106 | shl edi, 3 ; calc bit offset.
|
---|
107 |
|
---|
108 | bsf ecx, eax
|
---|
109 | jz .failed_restore ; race paranoia
|
---|
110 | add ecx, edi
|
---|
111 | mov eax, ecx
|
---|
112 |
|
---|
113 | ; return success
|
---|
114 | pop xDI
|
---|
115 | %if ARCH_BITS == 16
|
---|
116 | mov edx, eax
|
---|
117 | shr edx, 16
|
---|
118 | leave
|
---|
119 | %endif
|
---|
120 | ret
|
---|
121 |
|
---|
122 | ; failure
|
---|
123 | ;}
|
---|
124 | ;return -1;
|
---|
125 | .failed_restore:
|
---|
126 | pop xDI
|
---|
127 | .failed:
|
---|
128 | %if ARCH_BITS != 16
|
---|
129 | mov eax, 0ffffffffh
|
---|
130 | %else
|
---|
131 | mov ax, 0ffffh
|
---|
132 | mov dx, ax
|
---|
133 | leave
|
---|
134 | %endif
|
---|
135 | ret
|
---|
136 | ENDPROC ASMBitFirstClear
|
---|
137 |
|
---|