hc
2024-03-26 e0728245c89800c2038c23308f2d88969d5b41c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/** @file
  Implementation of TestAndClearBit using compare-exchange primitive
 
  Copyright (C) 2015, Linaro Ltd.
  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
#include <Base.h>
#include <Library/SynchronizationLib.h>
 
INT32
EFIAPI
TestAndClearBit (
  IN INT32            Bit,
  IN VOID             *Address
  )
{
  UINT16    Word, Read;
  UINT16    Mask;
 
  //
  // Calculate the effective address relative to 'Address' based on the
  // higher order bits of 'Bit'. Use signed shift instead of division to
  // ensure we round towards -Inf, and end up with a positive shift in
  // 'Bit', even if 'Bit' itself is negative.
  //
  Address = (VOID*)((UINT8*) Address + ((Bit >> 4) * sizeof(UINT16)));
  Mask = 1U << (Bit & 15);
 
  for (Word = *(UINT16 *) Address; Word & Mask; Word = Read) {
    Read = InterlockedCompareExchange16 (Address, Word, Word & ~Mask);
    if (Read == Word) {
      return 1;
    }
  }
  return 0;
}