[58755] | 1 | ; $Id: ASMBitFirstSet.asm 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
[8255] | 2 | ;; @file
|
---|
[58755] | 3 | ; IPRT - ASMBitFirstSet().
|
---|
[8255] | 4 | ;
|
---|
| 5 |
|
---|
| 6 | ;
|
---|
[98103] | 7 | ; Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[5885] | 8 | ;
|
---|
[96407] | 9 | ; This file is part of VirtualBox base platform packages, as
|
---|
| 10 | ; available from https://www.virtualbox.org.
|
---|
[5999] | 11 | ;
|
---|
[96407] | 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 | ;
|
---|
[5999] | 25 | ; The contents of this file may alternatively be used under the terms
|
---|
| 26 | ; of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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 | ;
|
---|
[96407] | 34 | ; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
| 35 | ;
|
---|
[8255] | 36 |
|
---|
| 37 |
|
---|
| 38 | ;*******************************************************************************
|
---|
| 39 | ;* Header Files *
|
---|
| 40 | ;*******************************************************************************
|
---|
| 41 | %include "iprt/asmdefs.mac"
|
---|
| 42 |
|
---|
| 43 | BEGINCODE
|
---|
| 44 |
|
---|
| 45 | ;;
|
---|
[58753] | 46 | ; Finds the first set bit in a bitmap.
|
---|
[8255] | 47 | ;
|
---|
[58746] | 48 | ; @returns (32/64:eax, 16:ax+dx) Index of the first zero bit.
|
---|
[58753] | 49 | ; @returns (32/64:eax, 16:ax+dx) -1 if no set bit was found.
|
---|
[58746] | 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.
|
---|
[8255] | 52 | ;
|
---|
[92799] | 53 | RT_BEGINPROC ASMBitFirstSet
|
---|
[58746] | 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
|
---|
[58762] | 62 | xchg rcx, rdx ; rdx=pvDst, ecx=cBits
|
---|
[58746] | 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
|
---|
[8255] | 72 | jz short .failed
|
---|
| 73 | ;{
|
---|
[58746] | 74 | push xDI
|
---|
[8255] | 75 |
|
---|
| 76 | ; asm {...}
|
---|
[58746] | 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 + 4]
|
---|
| 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
|
---|
[8255] | 90 | add ecx, 31 ; 32 bit aligned
|
---|
| 91 | shr ecx, 5 ; number of dwords to scan.
|
---|
[58746] | 92 | mov xDX, xDI ; xDX = saved pvBitmap
|
---|
[58753] | 93 | xor eax, eax
|
---|
| 94 | repe scasd ; Scan for the first dword with any bit set.
|
---|
[8255] | 95 | je .failed_restore
|
---|
| 96 |
|
---|
| 97 | ; find the bit in question
|
---|
[58746] | 98 | sub xDI, 4 ; one step back.
|
---|
| 99 | %if ARCH_BITS == 16
|
---|
| 100 | movzx edi, di
|
---|
[58756] | 101 | mov eax, [es:xDI]
|
---|
[58746] | 102 | %else
|
---|
[58756] | 103 | mov eax, [xDI]
|
---|
[58746] | 104 | %endif
|
---|
| 105 | sub xDI, xDX
|
---|
[8255] | 106 | shl edi, 3 ; calc bit offset.
|
---|
| 107 |
|
---|
| 108 | bsf ecx, eax
|
---|
[58753] | 109 | jz .failed_restore ; race paranoia
|
---|
[8255] | 110 | add ecx, edi
|
---|
| 111 | mov eax, ecx
|
---|
| 112 |
|
---|
| 113 | ; return success
|
---|
[58746] | 114 | pop xDI
|
---|
| 115 | %if ARCH_BITS == 16
|
---|
[58753] | 116 | mov edx, eax
|
---|
| 117 | shr edx, 16
|
---|
[58746] | 118 | leave
|
---|
| 119 | %endif
|
---|
[8255] | 120 | ret
|
---|
| 121 |
|
---|
| 122 | ; failure
|
---|
| 123 | ;}
|
---|
| 124 | ;return -1;
|
---|
| 125 | .failed_restore:
|
---|
[58746] | 126 | pop xDI
|
---|
[8255] | 127 | .failed:
|
---|
[58746] | 128 | %if ARCH_BITS != 16
|
---|
[8255] | 129 | mov eax, 0ffffffffh
|
---|
[58746] | 130 | %else
|
---|
| 131 | mov ax, 0ffffh
|
---|
| 132 | mov dx, ax
|
---|
| 133 | leave
|
---|
| 134 | %endif
|
---|
[8255] | 135 | ret
|
---|
[58753] | 136 | ENDPROC ASMBitFirstSet
|
---|
[8255] | 137 |
|
---|