ASMBitFirstClear.asm revision 4d939f327b6fe2b8201ab00e2d7e7d12b6d47d23
af062818b47340eef15700d2f0211576ba3506eevboxsync;; @file
af062818b47340eef15700d2f0211576ba3506eevboxsync; IPRT - ASMBitFirstClear().
af062818b47340eef15700d2f0211576ba3506eevboxsync;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync;
af062818b47340eef15700d2f0211576ba3506eevboxsync; Copyright (C) 2006-2007 Sun Microsystems, Inc.
af062818b47340eef15700d2f0211576ba3506eevboxsync;
af062818b47340eef15700d2f0211576ba3506eevboxsync; This file is part of VirtualBox Open Source Edition (OSE), as
af062818b47340eef15700d2f0211576ba3506eevboxsync; available from http://www.virtualbox.org. This file is free software;
af062818b47340eef15700d2f0211576ba3506eevboxsync; you can redistribute it and/or modify it under the terms of the GNU
af062818b47340eef15700d2f0211576ba3506eevboxsync; General Public License (GPL) as published by the Free Software
af062818b47340eef15700d2f0211576ba3506eevboxsync; Foundation, in version 2 as it comes in the "COPYING" file of the
af062818b47340eef15700d2f0211576ba3506eevboxsync; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
af062818b47340eef15700d2f0211576ba3506eevboxsync; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
af062818b47340eef15700d2f0211576ba3506eevboxsync;
af062818b47340eef15700d2f0211576ba3506eevboxsync; The contents of this file may alternatively be used under the terms
af062818b47340eef15700d2f0211576ba3506eevboxsync; of the Common Development and Distribution License Version 1.0
af062818b47340eef15700d2f0211576ba3506eevboxsync; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync; VirtualBox OSE distribution, in which case the provisions of the
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync; CDDL are applicable instead of those of the GPL.
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync;
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync; You may elect to license modified versions of this file under the
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync; terms and conditions of either the GPL or the CDDL or both.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync;
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync; Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync; Clara, CA 95054 USA or visit http://www.sun.com if you need
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync; additional information or have any questions.
af062818b47340eef15700d2f0211576ba3506eevboxsync;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync;*******************************************************************************
af062818b47340eef15700d2f0211576ba3506eevboxsync;* Header Files *
af062818b47340eef15700d2f0211576ba3506eevboxsync;*******************************************************************************
af062818b47340eef15700d2f0211576ba3506eevboxsync%include "iprt/asmdefs.mac"
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncBEGINCODE
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync;;
af062818b47340eef15700d2f0211576ba3506eevboxsync; Finds the first clear bit in a bitmap.
af062818b47340eef15700d2f0211576ba3506eevboxsync;
af062818b47340eef15700d2f0211576ba3506eevboxsync; @returns eax Index of the first zero bit.
af062818b47340eef15700d2f0211576ba3506eevboxsync; @returns eax -1 if no clear bit was found.
af062818b47340eef15700d2f0211576ba3506eevboxsync; @param rcx pvBitmap Pointer to the bitmap.
af062818b47340eef15700d2f0211576ba3506eevboxsync; @param edx cBits The number of bits in the bitmap. Multiple of 32.
af062818b47340eef15700d2f0211576ba3506eevboxsync;
af062818b47340eef15700d2f0211576ba3506eevboxsyncBEGINPROC_EXPORTED ASMBitFirstClear
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync ;if (cBits)
af062818b47340eef15700d2f0211576ba3506eevboxsync or edx, edx
af062818b47340eef15700d2f0211576ba3506eevboxsync jz short .failed
af062818b47340eef15700d2f0211576ba3506eevboxsync ;{
af062818b47340eef15700d2f0211576ba3506eevboxsync push rdi
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync ; asm {...}
af062818b47340eef15700d2f0211576ba3506eevboxsync mov rdi, rcx ; rdi = start of scasd
af062818b47340eef15700d2f0211576ba3506eevboxsync mov ecx, edx
af062818b47340eef15700d2f0211576ba3506eevboxsync add ecx, 31 ; 32 bit aligned
af062818b47340eef15700d2f0211576ba3506eevboxsync shr ecx, 5 ; number of dwords to scan.
af062818b47340eef15700d2f0211576ba3506eevboxsync mov rdx, rdi ; rdx = saved pvBitmap
af062818b47340eef15700d2f0211576ba3506eevboxsync mov eax, 0ffffffffh
af062818b47340eef15700d2f0211576ba3506eevboxsync repe scasd ; Scan for the first dword with any clear bit.
je .failed_restore
; find the bit in question
lea rdi, [rdi - 4] ; one step back.
xor eax, [rdi] ; eax = NOT [rdi]
sub rdi, rdx
shl edi, 3 ; calc bit offset.
mov ecx, 0ffffffffh
bsf ecx, eax
add ecx, edi
mov eax, ecx
; return success
pop rdi
ret
; failure
;}
;return -1;
.failed_restore:
pop rdi
ret
.failed:
mov eax, 0ffffffffh
ret
ENDPROC ASMBitFirstClear