1 | /* $Id: ASMBitNextSet-generic.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASMBitNextSet - generic C implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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/asm.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | RTDECL(int) ASMBitNextSet(const volatile void RT_FAR *pvBitmap, uint32_t cBits, uint32_t iBitPrev) RT_NOTHROW_DEF
|
---|
38 | {
|
---|
39 | const volatile uint32_t RT_FAR *pau32Bitmap = (const volatile uint32_t RT_FAR *)pvBitmap;
|
---|
40 | int iBit = ++iBitPrev & 31;
|
---|
41 | Assert(!(cBits & 31));
|
---|
42 | Assert(!((uintptr_t)pvBitmap & 3));
|
---|
43 | if (iBit)
|
---|
44 | {
|
---|
45 | /*
|
---|
46 | * Inspect the 32-bit word containing the unaligned bit.
|
---|
47 | */
|
---|
48 | uint32_t u32 = RT_LE2H_U32(pau32Bitmap[iBitPrev / 32]) >> iBit;
|
---|
49 | if (u32)
|
---|
50 | return iBitPrev + ASMBitFirstSetU32(u32) - 1;
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Skip ahead and see if there is anything left to search.
|
---|
54 | */
|
---|
55 | iBitPrev |= 31;
|
---|
56 | iBitPrev++;
|
---|
57 | if (cBits <= (uint32_t)iBitPrev)
|
---|
58 | return -1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * 32-bit aligned search, let ASMBitFirstSet do the dirty work.
|
---|
63 | */
|
---|
64 | iBit = ASMBitFirstSet(&pau32Bitmap[iBitPrev / 32], cBits - iBitPrev);
|
---|
65 | if (iBit >= 0)
|
---|
66 | iBit += iBitPrev;
|
---|
67 | return iBit;
|
---|
68 | }
|
---|
69 |
|
---|