UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MicrosoftPlatformMath.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
8
12struct FMicrosoftPlatformMathBase : public TUnrealPlatformMathSSE4Base<FGenericPlatformMath>
13{
14#if PLATFORM_ENABLE_VECTORINTRINSICS
15 static UE_FORCEINLINE_HINT bool IsNaN( float A ) { return _isnan(A) != 0; }
16 static UE_FORCEINLINE_HINT bool IsNaN(double A) { return _isnan(A) != 0; }
17 static UE_FORCEINLINE_HINT bool IsFinite( float A ) { return _finite(A) != 0; }
18 static UE_FORCEINLINE_HINT bool IsFinite(double A) { return _finite(A) != 0; }
19
20 #pragma intrinsic(_BitScanReverse)
21
22 static inline uint32 FloorLog2(uint32 Value)
23 {
24 // Use BSR to return the log2 of the integer
25 // return 0 if value is 0
26 unsigned long BitIndex;
27 return _BitScanReverse(&BitIndex, Value) ? BitIndex : 0;
28 }
29
30 static inline uint32 FloorLog2NonZero(uint32 Value)
31 {
32 unsigned long BitIndex = 0;
33 _BitScanReverse(&BitIndex, Value);
34 return BitIndex;
35 }
36
37 static constexpr inline uint8 CountLeadingZeros8(uint8 Value)
38 {
39 // return 8 if value was 0
40
42 {
44 }
45 else
46 {
47 unsigned long BitIndex;
48 _BitScanReverse(&BitIndex, uint32(Value) * 2 + 1);
49 return uint8(8 - BitIndex);
50 }
51 }
52
53 static constexpr inline uint32 CountTrailingZeros(uint32 Value)
54 {
55
57 {
59 }
60 else
61 {
62 // return 32 if value was 0
63 unsigned long BitIndex; // 0-based, where the LSB is 0 and MSB is 31
64 return _BitScanForward(&BitIndex, Value) ? BitIndex : 32;
65 }
66 }
67
68 static inline uint32 CeilLogTwo( uint32 Arg )
69 {
70 // if Arg is 0, change it to 1 so that we return 0
71 Arg = Arg ? Arg : 1;
72 return 32 - CountLeadingZeros(Arg - 1);
73 }
74
75 static UE_FORCEINLINE_HINT uint32 RoundUpToPowerOfTwo(uint32 Arg)
76 {
77 return 1u << CeilLogTwo(Arg);
78 }
79
80 static UE_FORCEINLINE_HINT uint64 RoundUpToPowerOfTwo64(uint64 Arg)
81 {
82 return uint64(1) << CeilLogTwo64(Arg);
83 }
84
85 #pragma intrinsic(_BitScanReverse64)
86
87 static inline uint64 FloorLog2_64(uint64 Value)
88 {
89 unsigned long BitIndex;
90 return _BitScanReverse64(&BitIndex, Value) ? BitIndex : 0;
91 }
92
93 static inline uint64 FloorLog2NonZero_64(uint64 Value)
94 {
95 unsigned long BitIndex = 0;
96 _BitScanReverse64(&BitIndex, Value);
97 return BitIndex;
98 }
99
100 static inline uint64 CeilLogTwo64(uint64 Arg)
101 {
102 // if Arg is 0, change it to 1 so that we return 0
103 Arg = Arg ? Arg : 1;
104 return 64 - CountLeadingZeros64(Arg - 1);
105 }
106
107 static constexpr inline uint64 CountLeadingZeros64(uint64 Value)
108 {
109
111 {
113 }
114 else
115 {
116 //https://godbolt.org/z/Ejh5G4vPK
117 // return 64 if value if was 0
118 unsigned long BitIndex;
119 if (!_BitScanReverse64(&BitIndex, Value)) BitIndex = -1;
120 return 63 - BitIndex;
121 }
122 }
123
124 static constexpr inline uint64 CountTrailingZeros64(uint64 Value)
125 {
126
128 {
130 }
131 else
132 {
133 // return 64 if Value is 0
134 unsigned long BitIndex; // 0-based, where the LSB is 0 and MSB is 63
135 return _BitScanForward64(&BitIndex, Value) ? BitIndex : 64;
136 }
137 }
138
139 static constexpr inline uint32 CountLeadingZeros(uint32 Value)
140 {
141
143 {
145 }
146 else
147 {
148 // return 32 if value is zero
149 unsigned long BitIndex;
150 _BitScanReverse64(&BitIndex, uint64(Value) * 2 + 1);
151 return 32 - BitIndex;
152 }
153 }
154
155#if PLATFORM_ENABLE_POPCNT_INTRINSIC
156 static UE_FORCEINLINE_HINT int32 CountBits(uint64 Bits)
157 {
158 return _mm_popcnt_u64(Bits);
159 }
160#endif
161
162#endif
163};
#define UE_IF_CONSTEVAL
Definition Platform.h:786
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
static constexpr uint64 CountTrailingZeros64(uint64 Value)
Definition GenericPlatformMath.h:775
static constexpr uint32 CountTrailingZeros(uint32 Value)
Definition GenericPlatformMath.h:747
static constexpr uint64 CountLeadingZeros64(uint64 Value)
Definition GenericPlatformMath.h:734
static constexpr uint32 CountLeadingZeros(uint32 Value)
Definition GenericPlatformMath.h:721
static constexpr uint8 CountLeadingZeros8(uint8 Value)
Definition GenericPlatformMath.h:708
Definition MicrosoftPlatformMath.h:13
Definition UnrealPlatformMathSSE4.h:70