1 | /* $Id: ASMBitFirstSet-generic.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASMBitFirstSet - 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(int32_t) ASMBitFirstSet(const volatile void RT_FAR *pvBitmap, uint32_t cBits) RT_NOTHROW_DEF
|
---|
38 | {
|
---|
39 | const volatile size_t RT_FAR *pu = (const volatile size_t RT_FAR *)pvBitmap;
|
---|
40 | Assert(!(cBits & 31));
|
---|
41 | Assert(!((uintptr_t)pvBitmap & 3));
|
---|
42 |
|
---|
43 | #if ARCH_BITS > 32
|
---|
44 | /* Deal with misaligned bitmaps (happens all the time via ASMBitNextClear()). */
|
---|
45 | if (!((uintptr_t)pvBitmap & 7) && cBits >= 32)
|
---|
46 | {
|
---|
47 | uint32_t u32 = *(const volatile uint32_t RT_FAR *)pu;
|
---|
48 | if (u32 != 0)
|
---|
49 | {
|
---|
50 | size_t const iBaseBit = ((uintptr_t)pu - (uintptr_t)pvBitmap) * 8;
|
---|
51 | return iBaseBit + ASMBitFirstSetU32(RT_LE2H_U32(u32)) - 1;
|
---|
52 | }
|
---|
53 | pu = (const volatile size_t RT_FAR *)((uintptr_t)pu + sizeof(uint32_t));
|
---|
54 | cBits -= 32;
|
---|
55 | }
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | /* Main search loop: */
|
---|
59 | while (cBits >= sizeof(size_t) * 8)
|
---|
60 | {
|
---|
61 | size_t u = *pu;
|
---|
62 | if (u == 0)
|
---|
63 | { }
|
---|
64 | else
|
---|
65 | {
|
---|
66 | size_t const iBaseBit = ((uintptr_t)pu - (uintptr_t)pvBitmap) * 8;
|
---|
67 | #if ARCH_BITS == 32
|
---|
68 | return iBaseBit + ASMBitFirstSetU32(RT_LE2H_U32(u)) - 1;
|
---|
69 | #elif ARCH_BITS == 64
|
---|
70 | return iBaseBit + ASMBitFirstSetU64(RT_LE2H_U64(u)) - 1;
|
---|
71 | #else
|
---|
72 | # error "ARCH_BITS is not supported"
|
---|
73 | #endif
|
---|
74 | }
|
---|
75 |
|
---|
76 | pu++;
|
---|
77 | cBits -= sizeof(size_t) * 8;
|
---|
78 | }
|
---|
79 |
|
---|
80 | #if ARCH_BITS > 32
|
---|
81 | /* Final 32-bit item (unlikely)? */
|
---|
82 | if (cBits < 32)
|
---|
83 | { }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | uint32_t u32 = *(const volatile uint32_t RT_FAR *)pu;
|
---|
87 | if (u32 != 0)
|
---|
88 | {
|
---|
89 | size_t const iBaseBit = ((uintptr_t)pu - (uintptr_t)pvBitmap) * 8;
|
---|
90 | return iBaseBit + ASMBitFirstSetU32(RT_LE2H_U32(u32)) - 1;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | return -1;
|
---|
96 | }
|
---|
97 |
|
---|