5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - CPU Set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/*
1b396f7f5a76d66e62fc9d8e66dcbe8e7bf72039vboxsync * Copyright (C) 2008-2011 Oracle Corporation
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * available from http://www.virtualbox.org. This file is free software;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * you can redistribute it and/or modify it under the terms of the GNU
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * General Public License (GPL) as published by the Free Software
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * The contents of this file may alternatively be used under the terms
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * of the Common Development and Distribution License Version 1.0
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * VirtualBox OSE distribution, in which case the provisions of the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * CDDL are applicable instead of those of the GPL.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * You may elect to license modified versions of this file under the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * terms and conditions of either the GPL or the CDDL or both.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#ifndef ___iprt_cpuset_h
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#define ___iprt_cpuset_h
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#include <iprt/types.h>
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#include <iprt/mp.h> /* RTMpCpuIdToSetIndex */
0aaf889969ebdaba8a310db13adcec8c10174796vboxsync#include <iprt/asm.h>
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_BEGIN
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
f20f327b65009074292a4b9ad44a02b6bfb2de8avboxsync/** @defgroup grp_rt_cpuset RTCpuSet - CPU Set
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @ingroup grp_rt
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5e5603ae40c7a0a884fe91e269b7d6d6c0ba56f5vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Clear all CPUs.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync * @returns pSet.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsyncDECLINLINE(PRTCPUSET) RTCpuSetEmpty(PRTCPUSET pSet)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync unsigned i;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync for (i = 0; i < RT_ELEMENTS(pSet->bmSet); i++)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync pSet->bmSet[i] = 0;
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync return pSet;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Set all CPUs.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync * @returns pSet.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsyncDECLINLINE(PRTCPUSET) RTCpuSetFill(PRTCPUSET pSet)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync unsigned i;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync for (i = 0; i < RT_ELEMENTS(pSet->bmSet); i++)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync pSet->bmSet[i] = UINT64_MAX;
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync return pSet;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync/**
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync * Copies one set to another.
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync *
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync * @param pDst Pointer to the destination set.
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync * @param pSrc Pointer to the source set.
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync */
1401c2ea86fc9cda6ec820758493d69dca61affevboxsyncDECLINLINE(void) RTCpuSetCopy(PRTCPUSET pDst, PRTCPUSET pSrc)
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync{
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync unsigned i;
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync for (i = 0; i < RT_ELEMENTS(pDst->bmSet); i++)
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync pDst->bmSet[i] = pSrc->bmSet[i];
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync}
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync
1401c2ea86fc9cda6ec820758493d69dca61affevboxsync
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync/**
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync * ANDs the given CPU set with another.
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync *
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync * @returns pSet.
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync * @param pSet Pointer to the set.
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync * @param pAndMaskSet Pointer to the AND-mask set.
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync */
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsyncDECLINLINE(PRTCPUSET) RTCpuSetAnd(PRTCPUSET pSet, PRTCPUSET pAndMaskSet)
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync{
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync unsigned i;
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync for (i = 0; i < RT_ELEMENTS(pSet->bmSet); i++)
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync ASMAtomicAndU64((volatile uint64_t *)&pSet->bmSet[i], pAndMaskSet->bmSet[i]);
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync return pSet;
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync}
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync * Adds a CPU given by its identifier to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns 0 on success, -1 if idCpu isn't valid.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU to add.
0aaf889969ebdaba8a310db13adcec8c10174796vboxsync * @remarks The modification is atomic.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(int) RTCpuSetAdd(PRTCPUSET pSet, RTCPUID idCpu)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync if (RT_UNLIKELY(iCpu < 0))
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return -1;
0aaf889969ebdaba8a310db13adcec8c10174796vboxsync ASMAtomicBitSet(pSet, iCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return 0;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync/**
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * Adds a CPU given by its identifier to the set.
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync *
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * @returns 0 on success, -1 if iCpu isn't valid.
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * @param pSet Pointer to the set.
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * @param iCpu The index of the CPU to add.
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * @remarks The modification is atomic.
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync */
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsyncDECLINLINE(int) RTCpuSetAddByIndex(PRTCPUSET pSet, int iCpu)
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync{
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync if (RT_UNLIKELY((unsigned)iCpu >= RTCPUSET_MAX_CPUS))
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync return -1;
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync ASMAtomicBitSet(pSet, iCpu);
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync return 0;
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync}
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync * Removes a CPU given by its identifier from the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns 0 on success, -1 if idCpu isn't valid.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU to delete.
0aaf889969ebdaba8a310db13adcec8c10174796vboxsync * @remarks The modification is atomic.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(int) RTCpuSetDel(PRTCPUSET pSet, RTCPUID idCpu)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync if (RT_UNLIKELY(iCpu < 0))
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return -1;
0aaf889969ebdaba8a310db13adcec8c10174796vboxsync ASMAtomicBitClear(pSet, iCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return 0;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
87b188a7236861157e1541c94df304f356ec8870vboxsync/**
87b188a7236861157e1541c94df304f356ec8870vboxsync * Removes a CPU given by its index from the set.
87b188a7236861157e1541c94df304f356ec8870vboxsync *
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * @returns 0 on success, -1 if iCpu isn't valid.
87b188a7236861157e1541c94df304f356ec8870vboxsync * @param pSet Pointer to the set.
87b188a7236861157e1541c94df304f356ec8870vboxsync * @param iCpu The index of the CPU to delete.
87b188a7236861157e1541c94df304f356ec8870vboxsync * @remarks The modification is atomic.
87b188a7236861157e1541c94df304f356ec8870vboxsync */
87b188a7236861157e1541c94df304f356ec8870vboxsyncDECLINLINE(int) RTCpuSetDelByIndex(PRTCPUSET pSet, int iCpu)
87b188a7236861157e1541c94df304f356ec8870vboxsync{
87b188a7236861157e1541c94df304f356ec8870vboxsync if (RT_UNLIKELY((unsigned)iCpu >= RTCPUSET_MAX_CPUS))
87b188a7236861157e1541c94df304f356ec8870vboxsync return -1;
87b188a7236861157e1541c94df304f356ec8870vboxsync ASMAtomicBitClear(pSet, iCpu);
87b188a7236861157e1541c94df304f356ec8870vboxsync return 0;
87b188a7236861157e1541c94df304f356ec8870vboxsync}
87b188a7236861157e1541c94df304f356ec8870vboxsync
87b188a7236861157e1541c94df304f356ec8870vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync * Checks if a CPU given by its identifier is a member of the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns true / false accordingly.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU to look for.
0aaf889969ebdaba8a310db13adcec8c10174796vboxsync * @remarks The test is atomic.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
8e4e0ad3b6c8d4f700608694f39fd724d64279e9vboxsyncDECLINLINE(bool) RTCpuSetIsMember(PCRTCPUSET pSet, RTCPUID idCpu)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync if (RT_UNLIKELY(iCpu < 0))
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return false;
0aaf889969ebdaba8a310db13adcec8c10174796vboxsync return ASMBitTest((volatile void *)pSet, iCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync/**
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync * Checks if a CPU given by its index is a member of the set.
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync *
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync * @returns true / false accordingly.
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync * @param pSet Pointer to the set.
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync * @param iCpu The index of the CPU in the set.
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync * @remarks The test is atomic.
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync */
28848b6caea6907ede2609a18eee2a9eabb01d04vboxsyncDECLINLINE(bool) RTCpuSetIsMemberByIndex(PCRTCPUSET pSet, int iCpu)
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync{
28848b6caea6907ede2609a18eee2a9eabb01d04vboxsync if (RT_UNLIKELY((unsigned)iCpu >= RTCPUSET_MAX_CPUS))
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync return false;
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync return ASMBitTest((volatile void *)pSet, iCpu);
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync}
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync
7a21f97c0af14dfd4b7d825737b2571b45a2e18dvboxsync
67c73271505adae0686ddbc74eaeef778ef16792vboxsync/**
67c73271505adae0686ddbc74eaeef778ef16792vboxsync * Checks if the two sets match or not.
67c73271505adae0686ddbc74eaeef778ef16792vboxsync *
67c73271505adae0686ddbc74eaeef778ef16792vboxsync * @returns true / false accordingly.
67c73271505adae0686ddbc74eaeef778ef16792vboxsync * @param pSet1 The first set.
67c73271505adae0686ddbc74eaeef778ef16792vboxsync * @param pSet2 The second set.
67c73271505adae0686ddbc74eaeef778ef16792vboxsync */
67c73271505adae0686ddbc74eaeef778ef16792vboxsyncDECLINLINE(bool) RTCpuSetIsEqual(PCRTCPUSET pSet1, PCRTCPUSET pSet2)
67c73271505adae0686ddbc74eaeef778ef16792vboxsync{
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync unsigned i;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync for (i = 0; i < RT_ELEMENTS(pSet1->bmSet); i++)
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync if (pSet1->bmSet[i] != pSet2->bmSet[i])
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync return false;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync return true;
67c73271505adae0686ddbc74eaeef778ef16792vboxsync}
67c73271505adae0686ddbc74eaeef778ef16792vboxsync
67c73271505adae0686ddbc74eaeef778ef16792vboxsync
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync/**
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync * Checks if the CPU set is empty or not.
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync *
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync * @returns true / false accordingly.
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync * @param pSet Pointer to the set.
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync */
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsyncDECLINLINE(bool) RTCpuSetIsEmpty(PRTCPUSET pSet)
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync{
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync unsigned i;
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync for (i = 0; i < RT_ELEMENTS(pSet->bmSet); i++)
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync if (pSet->bmSet[i])
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync return false;
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync return true;
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync}
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync
ea9e88f255a0e6538865665661f6b5d364eb4f45vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Converts the CPU set to a 64-bit mask.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns The mask.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync * @remarks Use with extreme care as it may lose information!
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(uint64_t) RTCpuSetToU64(PCRTCPUSET pSet)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync return pSet->bmSet[0];
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Initializes the CPU set from a 64-bit mask.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param fMask The mask.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsyncDECLINLINE(PRTCPUSET) RTCpuSetFromU64(PRTCPUSET pSet, uint64_t fMask)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync unsigned i;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync pSet->bmSet[0] = fMask;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync for (i = 1; i < RT_ELEMENTS(pSet->bmSet); i++)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync pSet->bmSet[i] = 0;
1b396f7f5a76d66e62fc9d8e66dcbe8e7bf72039vboxsync
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync return pSet;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync * Count the CPUs in the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync * @returns CPU count.
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
259148a88d85228a72494b78a84aead87869c37avboxsyncDECLINLINE(int) RTCpuSetCount(PCRTCPUSET pSet)
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync{
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync int cCpus = 0;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync unsigned i;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync for (i = 0; i < RT_ELEMENTS(pSet->bmSet); i++)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync {
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync uint64_t u64 = pSet->bmSet[i];
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync if (u64 != 0)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync {
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync unsigned iCpu = 64;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync while (iCpu-- > 0)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync {
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync if (u64 & 1)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync cCpus++;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync u64 >>= 1;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync }
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync }
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync }
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync return cCpus;
d23a94a28e59f64d02905f20bb8f3b23f1c9eb17vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
259148a88d85228a72494b78a84aead87869c37avboxsync/**
259148a88d85228a72494b78a84aead87869c37avboxsync * Get the highest set index.
259148a88d85228a72494b78a84aead87869c37avboxsync *
259148a88d85228a72494b78a84aead87869c37avboxsync * @returns The higest set index, -1 if all bits are clear.
259148a88d85228a72494b78a84aead87869c37avboxsync * @param pSet Pointer to the set.
259148a88d85228a72494b78a84aead87869c37avboxsync */
259148a88d85228a72494b78a84aead87869c37avboxsyncDECLINLINE(int) RTCpuLastIndex(PCRTCPUSET pSet)
259148a88d85228a72494b78a84aead87869c37avboxsync{
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync unsigned i = RT_ELEMENTS(pSet->bmSet);
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync while (i-- > 0)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync {
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync uint64_t u64 = pSet->bmSet[i];
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync if (u64)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync {
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync /* There are more efficient ways to do this in asm.h... */
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync unsigned iBit;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync for (iBit = 63; iBit > 0; iBit--)
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync {
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync if (u64 & RT_BIT_64(63))
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync break;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync u64 <<= 1;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync }
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync return i * 64 + iBit;
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync }
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync }
c2ac210bd84591123bb8803712887e2b016cb78fvboxsync return 0;
259148a88d85228a72494b78a84aead87869c37avboxsync}
259148a88d85228a72494b78a84aead87869c37avboxsync
259148a88d85228a72494b78a84aead87869c37avboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/** @} */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_END
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#endif
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync