1 | /* $Id: ASMBitFirstClear-generic.cpp 87192 2021-01-07 20:43:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASMBitFirstClear - generic C implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2021 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. 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 | DECLASM(int32_t) ASMBitFirstClear(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 |
|
---|
42 | while (cBits >= sizeof(size_t) * 8)
|
---|
43 | {
|
---|
44 | size_t u = *pu;
|
---|
45 | if (u == ~(size_t)0)
|
---|
46 | { }
|
---|
47 | else
|
---|
48 | {
|
---|
49 | size_t const iBaseBit = ((uintptr_t)pu - (uintptr_t)pvBitmap) * 8;
|
---|
50 | #if ARCH_BITS == 32
|
---|
51 | return iBaseBit + ASMBitFirstSetU32(~RT_LE2H_U32(u)) - 1;
|
---|
52 | #elif ARCH_BITS == 64
|
---|
53 | return iBaseBit + ASMBitFirstSetU64(~RT_LE2H_U64(u)) - 1;
|
---|
54 | #else
|
---|
55 | # error "ARCH_BITS or RT_BIG_ENDIAN/RT_LITTLE_ENDIAN is not supported" /** @todo figure out bitmaps on bigendian systems! */
|
---|
56 | #endif
|
---|
57 | }
|
---|
58 |
|
---|
59 | pu++;
|
---|
60 | cBits -= sizeof(size_t) * 8;
|
---|
61 | }
|
---|
62 |
|
---|
63 | #if ARCH_BITS > 32
|
---|
64 | if (cBits >= 32)
|
---|
65 | {
|
---|
66 | uint32_t u32 = *(const volatile uint32_t RT_FAR *)pu;
|
---|
67 | if (u32 != UINT32_MAX)
|
---|
68 | {
|
---|
69 | size_t const iBaseBit = ((uintptr_t)pu - (uintptr_t)pvBitmap) * 8;
|
---|
70 | return iBaseBit + ASMBitFirstSetU32(~RT_LE2H_U32(u32)) - 1;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | return -1;
|
---|
76 | }
|
---|
77 |
|
---|