1 | ; $Id: ASMBitFirstSet.asm 82968 2020-02-04 10:35:17Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; IPRT - ASMBitFirstSet().
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | ; available from http://www.virtualbox.org. This file is free software;
|
---|
11 | ; you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | ; General Public License (GPL) as published by the Free Software
|
---|
13 | ; Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | ; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | ; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | ;
|
---|
17 | ; The contents of this file may alternatively be used under the terms
|
---|
18 | ; of the Common Development and Distribution License Version 1.0
|
---|
19 | ; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | ; VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | ; CDDL are applicable instead of those of the GPL.
|
---|
22 | ;
|
---|
23 | ; You may elect to license modified versions of this file under the
|
---|
24 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | ;
|
---|
26 |
|
---|
27 |
|
---|
28 | ;*******************************************************************************
|
---|
29 | ;* Header Files *
|
---|
30 | ;*******************************************************************************
|
---|
31 | %include "iprt/asmdefs.mac"
|
---|
32 |
|
---|
33 | BEGINCODE
|
---|
34 |
|
---|
35 | ;;
|
---|
36 | ; Finds the first set bit in a bitmap.
|
---|
37 | ;
|
---|
38 | ; @returns (32/64:eax, 16:ax+dx) Index of the first zero bit.
|
---|
39 | ; @returns (32/64:eax, 16:ax+dx) -1 if no set bit was found.
|
---|
40 | ; @param msc:rcx gcc:rdi pvBitmap Pointer to the bitmap.
|
---|
41 | ; @param msc:edx gcc:rsi cBits The number of bits in the bitmap. Multiple of 32.
|
---|
42 | ;
|
---|
43 | BEGINPROC_EXPORTED ASMBitFirstSet
|
---|
44 | ;
|
---|
45 | ; if (cBits)
|
---|
46 | ; Put cBits in ecx first.
|
---|
47 | ;
|
---|
48 | %if ARCH_BITS == 64
|
---|
49 | %ifdef ASM_CALL64_GCC
|
---|
50 | mov ecx, esi
|
---|
51 | %else
|
---|
52 | xchg rcx, rdx ; rdx=pvDst, ecx=cBits
|
---|
53 | %endif
|
---|
54 | %elif ARCH_BITS == 32
|
---|
55 | mov ecx, [esp + 4 + 4]
|
---|
56 | %elif ARCH_BITS == 16
|
---|
57 | push bp
|
---|
58 | mov bp, sp
|
---|
59 | mov ecx, [bp + 4 + 4]
|
---|
60 | %endif
|
---|
61 | or ecx, ecx
|
---|
62 | jz short .failed
|
---|
63 | ;{
|
---|
64 | push xDI
|
---|
65 |
|
---|
66 | ; asm {...}
|
---|
67 | %if ARCH_BITS == 64
|
---|
68 | %ifdef ASM_CALL64_GCC
|
---|
69 | ; rdi = start of scasd - already done
|
---|
70 | %else
|
---|
71 | mov rdi, rdx ; rdi = start of scasd (Note! xchg rdx,rcx above)
|
---|
72 | %endif
|
---|
73 | %elif ARCH_BITS == 32
|
---|
74 | mov edi, [esp + 4]
|
---|
75 | %elif ARCH_BITS == 16
|
---|
76 | mov ax, [bp + 4 + 2]
|
---|
77 | mov di, [bp + 4]
|
---|
78 | mov es, ax ; es is volatile, no need to save.
|
---|
79 | %endif
|
---|
80 | add ecx, 31 ; 32 bit aligned
|
---|
81 | shr ecx, 5 ; number of dwords to scan.
|
---|
82 | mov xDX, xDI ; xDX = saved pvBitmap
|
---|
83 | xor eax, eax
|
---|
84 | repe scasd ; Scan for the first dword with any bit set.
|
---|
85 | je .failed_restore
|
---|
86 |
|
---|
87 | ; find the bit in question
|
---|
88 | sub xDI, 4 ; one step back.
|
---|
89 | %if ARCH_BITS == 16
|
---|
90 | movzx edi, di
|
---|
91 | mov eax, [es:xDI]
|
---|
92 | %else
|
---|
93 | mov eax, [xDI]
|
---|
94 | %endif
|
---|
95 | sub xDI, xDX
|
---|
96 | shl edi, 3 ; calc bit offset.
|
---|
97 |
|
---|
98 | bsf ecx, eax
|
---|
99 | jz .failed_restore ; race paranoia
|
---|
100 | add ecx, edi
|
---|
101 | mov eax, ecx
|
---|
102 |
|
---|
103 | ; return success
|
---|
104 | pop xDI
|
---|
105 | %if ARCH_BITS == 16
|
---|
106 | mov edx, eax
|
---|
107 | shr edx, 16
|
---|
108 | leave
|
---|
109 | %endif
|
---|
110 | ret
|
---|
111 |
|
---|
112 | ; failure
|
---|
113 | ;}
|
---|
114 | ;return -1;
|
---|
115 | .failed_restore:
|
---|
116 | pop xDI
|
---|
117 | .failed:
|
---|
118 | %if ARCH_BITS != 16
|
---|
119 | mov eax, 0ffffffffh
|
---|
120 | %else
|
---|
121 | mov ax, 0ffffh
|
---|
122 | mov dx, ax
|
---|
123 | leave
|
---|
124 | %endif
|
---|
125 | ret
|
---|
126 | ENDPROC ASMBitFirstSet
|
---|
127 |
|
---|