react-native-windows 0.74.11 → 0.75.0-preview.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.flowconfig +8 -2
- package/Common/Common.vcxproj +8 -0
- package/Common/Utilities.cpp +2 -2
- package/Directory.Build.targets +5 -0
- package/Folly/Folly.vcxproj +10 -2
- package/Folly/Folly.vcxproj.filters +0 -7
- package/Folly/TEMP_UntilFollyUpdate/ConstexprMath.h +970 -0
- package/Folly/TEMP_UntilFollyUpdate/json.cpp +1110 -0
- package/Folly/TEMP_UntilFollyUpdate/lang/ToAscii.cpp +4 -2
- package/Libraries/Animated/AnimatedImplementation.js +2 -0
- package/Libraries/Animated/NativeAnimatedHelper.js +4 -0
- package/Libraries/Animated/createAnimatedComponent.js +10 -4
- package/Libraries/Animated/useAnimatedProps.js +56 -28
- package/Libraries/BatchedBridge/MessageQueue.js +1 -0
- package/Libraries/Components/Button.js +10 -5
- package/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js +11 -2
- package/Libraries/Components/Flyout/Flyout.js +10 -10
- package/Libraries/Components/Flyout/FlyoutNativeComponent.js +8 -7
- package/Libraries/Components/Pressable/Pressable.js +13 -6
- package/Libraries/Components/Pressable/Pressable.windows.js +13 -6
- package/Libraries/Components/ScrollView/AndroidHorizontalScrollViewNativeComponent.js +4 -0
- package/Libraries/Components/ScrollView/ScrollView.js +109 -29
- package/Libraries/Components/ScrollView/ScrollViewNativeComponent.js +6 -0
- package/Libraries/Components/ScrollView/ScrollViewNativeComponent.windows.js +6 -0
- package/Libraries/Components/ScrollView/ScrollViewStickyHeader.js +13 -1
- package/Libraries/Components/StatusBar/StatusBar.js +1 -21
- package/Libraries/Components/Switch/Switch.windows.js +2 -0
- package/Libraries/Components/TextInput/AndroidTextInputNativeComponent.js +0 -15
- package/Libraries/Components/TextInput/InputAccessoryView.js +10 -1
- package/Libraries/Components/TextInput/RCTTextInputViewConfig.js +0 -12
- package/Libraries/Components/TextInput/TextInput.d.ts +0 -19
- package/Libraries/Components/TextInput/TextInput.js +14 -70
- package/Libraries/Components/TextInput/TextInput.windows.js +15 -72
- package/Libraries/Components/TextInput/WindowsTextInputNativeComponent.js +27 -12
- package/Libraries/Components/Touchable/Touchable.js +2 -2
- package/Libraries/Components/Touchable/TouchableHighlight.d.ts +4 -10
- package/Libraries/Components/Touchable/TouchableHighlight.js +3 -1
- package/Libraries/Components/Touchable/TouchableHighlight.windows.js +3 -1
- package/Libraries/Components/Touchable/TouchableOpacity.d.ts +4 -32
- package/Libraries/Components/Touchable/TouchableOpacity.js +3 -1
- package/Libraries/Components/Touchable/TouchableOpacity.windows.js +3 -1
- package/Libraries/Components/Touchable/TouchableWithoutFeedback.d.ts +8 -0
- package/Libraries/Components/Touchable/TouchableWithoutFeedback.js +117 -111
- package/Libraries/Components/Touchable/TouchableWithoutFeedback.windows.js +129 -110
- package/Libraries/Components/View/ReactNativeStyleAttributes.js +6 -0
- package/Libraries/Components/View/ReactNativeViewAttributes.js +1 -0
- package/Libraries/Components/View/ReactNativeViewAttributes.windows.js +1 -0
- package/Libraries/Components/View/View.js +0 -11
- package/Libraries/Components/View/View.windows.js +0 -11
- package/Libraries/Components/View/ViewAccessibility.js +4 -4
- package/Libraries/Components/View/ViewAccessibility.windows.js +4 -4
- package/Libraries/Components/View/ViewPropTypes.d.ts +21 -59
- package/Libraries/Components/View/ViewPropTypes.js +7 -0
- package/Libraries/Components/View/ViewPropTypes.windows.js +7 -0
- package/Libraries/Core/Devtools/loadBundleFromServer.js +3 -3
- package/Libraries/Core/Devtools/loadBundleFromServer.windows.js +153 -0
- package/Libraries/Core/Devtools/parseErrorStack.js +5 -5
- package/Libraries/Core/Devtools/parseHermesStack.js +22 -16
- package/Libraries/Core/ErrorHandlers.js +116 -0
- package/Libraries/Core/ExceptionsManager.js +2 -2
- package/Libraries/Core/ReactNativeVersion.js +3 -3
- package/Libraries/Core/setUpDeveloperTools.js +3 -1
- package/Libraries/Core/setUpPerformance.js +6 -4
- package/Libraries/Core/setUpReactDevTools.js +70 -10
- package/Libraries/Core/setUpTimers.js +50 -31
- package/Libraries/Debugging/DebuggingOverlayRegistry.js +1 -1
- package/Libraries/Image/Image.android.js +23 -13
- package/Libraries/Image/Image.d.ts +14 -15
- package/Libraries/Image/Image.ios.js +21 -11
- package/Libraries/Image/Image.windows.js +21 -11
- package/Libraries/Image/ImageProps.js +16 -5
- package/Libraries/Image/ImageTypes.flow.js +7 -2
- package/Libraries/Image/ImageUtils.js +1 -0
- package/Libraries/Image/ImageViewNativeComponent.js +2 -1
- package/Libraries/Inspector/ElementBox.js +6 -3
- package/Libraries/Inspector/ElementProperties.js +1 -1
- package/Libraries/Interaction/TouchHistoryMath.js +4 -4
- package/Libraries/IntersectionObserver/IntersectionObserverManager.js +6 -26
- package/Libraries/JSInspector/NetworkAgent.js +1 -1
- package/Libraries/LogBox/Data/LogBoxData.js +39 -29
- package/Libraries/LogBox/Data/LogBoxLog.js +114 -2
- package/Libraries/LogBox/Data/parseLogBoxLog.js +168 -53
- package/Libraries/LogBox/LogBox.js +29 -12
- package/Libraries/LogBox/LogBoxNotificationContainer.js +4 -0
- package/Libraries/LogBox/UI/LogBoxInspector.js +8 -70
- package/Libraries/LogBox/UI/LogBoxInspectorBody.js +87 -0
- package/Libraries/LogBox/UI/LogBoxInspectorFooter.js +6 -42
- package/Libraries/LogBox/UI/LogBoxInspectorFooterButton.js +58 -0
- package/Libraries/LogBox/UI/LogBoxInspectorHeader.js +5 -66
- package/Libraries/LogBox/UI/LogBoxInspectorHeader.windows.js +5 -66
- package/Libraries/LogBox/UI/LogBoxInspectorHeaderButton.js +76 -0
- package/Libraries/LogBox/UI/LogBoxInspectorReactFrames.js +8 -5
- package/Libraries/LogBox/UI/LogBoxInspectorReactFrames.windows.js +8 -5
- package/Libraries/LogBox/UI/LogBoxNotification.js +13 -152
- package/Libraries/LogBox/UI/LogBoxNotificationCountBadge.js +63 -0
- package/Libraries/LogBox/UI/LogBoxNotificationDismissButton.js +67 -0
- package/Libraries/LogBox/UI/LogBoxNotificationMessage.js +57 -0
- package/Libraries/NativeComponent/BaseViewConfig.android.js +5 -0
- package/Libraries/NativeComponent/BaseViewConfig.ios.js +5 -0
- package/Libraries/NativeComponent/BaseViewConfig.windows.js +5 -0
- package/Libraries/NativeComponent/NativeComponentRegistry.js +12 -5
- package/Libraries/NativeComponent/StaticViewConfigValidator.js +3 -0
- package/Libraries/Network/XMLHttpRequest.js +5 -1
- package/Libraries/NewAppScreen/components/LearnMoreLinks.js +3 -3
- package/Libraries/Pressability/Pressability.js +3 -51
- package/Libraries/Pressability/Pressability.windows.js +3 -51
- package/Libraries/ReactNative/AppRegistry.d.ts +4 -0
- package/Libraries/ReactNative/AppRegistry.js +2 -4
- package/Libraries/ReactNative/BridgelessUIManager.js +1 -21
- package/Libraries/ReactNative/FabricUIManager.js +0 -51
- package/Libraries/ReactNative/ReactFabricPublicInstance/warnForStyleProps.js +1 -0
- package/Libraries/ReactNative/RendererImplementation.js +20 -2
- package/Libraries/ReactNative/UIManager.d.ts +0 -21
- package/Libraries/ReactNative/UIManagerProperties.js +0 -3
- package/Libraries/ReactNative/__mocks__/FabricUIManager.js +5 -341
- package/Libraries/ReactNative/getNativeComponentAttributes.js +8 -8
- package/Libraries/Renderer/implementations/ReactFabric-dev.js +15682 -27088
- package/Libraries/Renderer/implementations/ReactFabric-prod.js +5082 -4381
- package/Libraries/Renderer/implementations/ReactFabric-profiling.js +3480 -2571
- package/Libraries/Renderer/implementations/ReactNativeRenderer-dev.js +15943 -27543
- package/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js +5303 -4606
- package/Libraries/Renderer/implementations/ReactNativeRenderer-profiling.js +3450 -2572
- package/Libraries/Renderer/shims/ReactFabric.js +2 -2
- package/Libraries/Renderer/shims/ReactFeatureFlags.js +2 -2
- package/Libraries/Renderer/shims/ReactNative.js +2 -3
- package/Libraries/Renderer/shims/ReactNativeTypes.js +24 -3
- package/Libraries/Renderer/shims/ReactNativeViewConfigRegistry.js +2 -2
- package/Libraries/Renderer/shims/createReactNativeComponentClass.js +2 -2
- package/Libraries/Share/Share.d.ts +16 -10
- package/Libraries/Share/Share.js +14 -15
- package/Libraries/StyleSheet/StyleSheet.d.ts +1 -1
- package/Libraries/StyleSheet/StyleSheet.js +3 -10
- package/Libraries/StyleSheet/StyleSheetTypes.d.ts +21 -21
- package/Libraries/StyleSheet/StyleSheetTypes.js +24 -18
- package/Libraries/StyleSheet/flattenStyle.js +1 -0
- package/Libraries/StyleSheet/processFilter.js +132 -0
- package/Libraries/StyleSheet/processTransform.js +18 -3
- package/Libraries/Text/Text.js +151 -128
- package/Libraries/Text/Text.windows.js +144 -127
- package/Libraries/Text/TextNativeComponent.js +5 -4
- package/Libraries/Text/TextProps.js +6 -6
- package/Libraries/Text/TextProps.windows.js +6 -6
- package/Libraries/TurboModule/TurboModuleRegistry.js +2 -1
- package/Libraries/Types/CodegenTypes.js +3 -0
- package/Libraries/Utilities/{LoadingView.android.js → DevLoadingView.js} +33 -11
- package/Libraries/Utilities/Dimensions.js +1 -0
- package/Libraries/Utilities/HMRClient.js +36 -8
- package/Libraries/Utilities/HMRClientProdShim.js +1 -0
- package/Libraries/Utilities/NativePlatformConstantsWin.js +2 -2
- package/Libraries/Utilities/Platform.android.js +4 -4
- package/Libraries/Utilities/RCTLog.js +1 -0
- package/Libraries/Utilities/ReactNativeTestTools.js +12 -24
- package/Libraries/Utilities/verifyComponentAttributeEquivalence.js +11 -6
- package/Libraries/__tests__/ViewWindows-test.js +6 -6
- package/Libraries/promiseRejectionTrackingOptions.js +1 -0
- package/Microsoft.ReactNative/Base/FollyIncludes.h +1 -1
- package/Microsoft.ReactNative/ComponentView.idl +0 -17
- package/Microsoft.ReactNative/Fabric/AbiState.cpp +3 -45
- package/Microsoft.ReactNative/Fabric/AbiState.h +0 -6
- package/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.cpp +1 -4
- package/Microsoft.ReactNative/Fabric/Composition/ImageComponentView.cpp +10 -6
- package/Microsoft.ReactNative/Fabric/Composition/ImageComponentView.h +4 -4
- package/Microsoft.ReactNative/Fabric/Composition/ParagraphComponentView.cpp +0 -10
- package/Microsoft.ReactNative/Fabric/Composition/ParagraphComponentView.h +0 -3
- package/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.cpp +29 -45
- package/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.h +1 -3
- package/Microsoft.ReactNative/Fabric/Composition/ScrollViewComponentView.cpp +1 -1
- package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp +7 -12
- package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputShadowNode.cpp +1 -2
- package/Microsoft.ReactNative/Fabric/Composition/UriImageManager.cpp +9 -3
- package/Microsoft.ReactNative/Fabric/Composition/UriImageManager.h +1 -1
- package/Microsoft.ReactNative/Fabric/FabricUIManagerModule.cpp +11 -18
- package/Microsoft.ReactNative/Fabric/FabricUIManagerModule.h +3 -5
- package/Microsoft.ReactNative/Fabric/WindowsImageManager.cpp +23 -16
- package/Microsoft.ReactNative/Fabric/WindowsImageManager.h +5 -2
- package/Microsoft.ReactNative/Fabric/platform/react/renderer/components/view/HostPlatformViewEventEmitter.cpp +2 -10
- package/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/TextLayoutManager.cpp +107 -36
- package/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/TextLayoutManager.h +18 -12
- package/Microsoft.ReactNative/Modules/DevSettingsModule.cpp +4 -0
- package/Microsoft.ReactNative/Modules/DevSettingsModule.h +1 -0
- package/Microsoft.ReactNative/Modules/ImageViewManagerModule.cpp +1 -2
- package/Microsoft.ReactNative/Modules/NativeUIManager.cpp +7 -4
- package/Microsoft.ReactNative/Modules/NativeUIManager.h +1 -1
- package/Microsoft.ReactNative/Modules/PlatformConstantsWinModule.cpp +2 -2
- package/Microsoft.ReactNative/Modules/PlatformConstantsWinModule.h +3 -3
- package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp +11 -17
- package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.h +1 -1
- package/Microsoft.ReactNative/Timer.idl +1 -3
- package/Microsoft.ReactNative/Utils/BatchingEventEmitter.cpp +1 -1
- package/Microsoft.ReactNative/Utils/LocalBundleReader.cpp +2 -3
- package/Microsoft.ReactNative/Utils/ValueUtils.cpp +1 -1
- package/Microsoft.ReactNative/Views/DynamicAutomationPeer.cpp +2 -2
- package/Microsoft.ReactNative/Views/TextInputViewManager.cpp +1 -1
- package/Microsoft.ReactNative/packages.lock.json +45 -72
- package/Microsoft.ReactNative.Cxx/JSI/LongLivedJsiValue.h +1 -1
- package/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems +5 -0
- package/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems.filters +1 -0
- package/Microsoft.ReactNative.Cxx/TurboModuleProvider.cpp +14 -6
- package/Microsoft.ReactNative.Cxx/TurboModuleProvider.h +2 -2
- package/Microsoft.ReactNative.Managed/Microsoft.ReactNative.Managed.csproj +1 -1
- package/Microsoft.ReactNative.Managed/packages.lock.json +3 -3
- package/PropertySheets/ARM64EC.props +13 -0
- package/PropertySheets/Application/ARM64EC.props +13 -0
- package/PropertySheets/DynamicLibrary/ARM64EC.props +13 -0
- package/PropertySheets/External/Microsoft.ReactNative.WindowsSdk.Default.props +4 -4
- package/PropertySheets/Generated/PackageVersion.g.props +4 -4
- package/PropertySheets/JSEngine.props +2 -0
- package/PropertySheets/StaticLibrary/ARM64EC.props +13 -0
- package/README.md +21 -21
- package/ReactCommon/ReactCommon.vcxproj +11 -1
- package/ReactCommon/ReactCommon.vcxproj.filters +19 -12
- package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSExecutor.cpp +67 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSExecutor.h +180 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/NativeToJsBridge.cpp +351 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/jsi/jsi/test/testlib.cpp +6 -3
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/bridging/Base.h +152 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/bridging/Bridging.h +2 -1
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/bridging/CallbackWrapper.h +13 -15
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/bridging/EventEmitter.h +137 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/bridging/Function.h +283 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModule.cpp +7 -10
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModule.h +162 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModuleUtils.h +2 -4
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/scrollview/ScrollViewProps.cpp +33 -8
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/runtime/JSRuntimeFactory.cpp +45 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/runtime/JSRuntimeFactory.h +67 -0
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/runtime/ReactInstance.cpp +104 -74
- package/Scripts/StripAdditionalPlatformsFromNuspec.ps1 +1 -1
- package/Scripts/Tfs/Layout-MSRN-Headers.ps1 +4 -0
- package/Scripts/rnw-dependencies.ps1 +36 -23
- package/Shared/HermesRuntimeHolder.cpp +19 -1
- package/Shared/HermesRuntimeHolder.h +8 -1
- package/Shared/HermesSamplingProfiler.cpp +1 -2
- package/Shared/Networking/WinRTWebSocketResource.cpp +3 -3
- package/Shared/OInstance.cpp +1 -2
- package/Shared/Shared.vcxitems +21 -15
- package/Shared/Shared.vcxitems.filters +8 -3
- package/Shared/Threading/BatchingQueueThread.cpp +6 -2
- package/Shared/Threading/BatchingQueueThread.h +2 -2
- package/Shared/Utils/CppWinrtLessExceptions.h +2 -2
- package/codegen/NativeAppStateSpec.g.h +8 -8
- package/codegen/NativeBlobModuleSpec.g.h +0 -10
- package/codegen/NativeDOMSpec.g.h +136 -0
- package/codegen/NativeDevSettingsSpec.g.h +11 -5
- package/codegen/NativeIdleCallbacksSpec.g.h +64 -0
- package/codegen/NativeIntersectionObserverSpec.g.h +14 -14
- package/codegen/NativeMicrotasksSpec.g.h +34 -0
- package/codegen/NativePerformanceSpec.g.h +35 -7
- package/codegen/NativePlatformConstantsWindowsSpec.g.h +81 -0
- package/codegen/NativePushNotificationManagerIOSSpec.g.h +15 -19
- package/codegen/NativeReactNativeFeatureFlagsSpec.g.h +111 -39
- package/codegen/NativeUIManagerSpec.g.h +5 -17
- package/codegen/react/components/rnwcore/Props.cpp +1 -1
- package/codegen/react/components/rnwcore/Props.h +16 -12
- package/codegen/react/components/rnwcore/States.h +0 -26
- package/codegen/rnwcoreJSI-generated.cpp +688 -463
- package/codegen/rnwcoreJSI.h +1374 -2867
- package/fmt/TEMP_UntilFmtUpdate/core.h +2925 -0
- package/fmt/fmt.vcxproj +10 -2
- package/jest/mockComponent.js +7 -0
- package/jest/renderer.js +25 -14
- package/jest/setup.js +19 -13
- package/package.json +29 -27
- package/rn-get-polyfills.js +1 -0
- package/src/private/core/composeStyles.js +27 -0
- package/src/private/featureflags/ReactNativeFeatureFlags.js +93 -33
- package/src/private/featureflags/ReactNativeFeatureFlagsBase.js +23 -4
- package/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js +56 -0
- package/src/private/fusebox/setUpFuseboxReactDevToolsDispatcher.js +108 -0
- package/src/private/specs/modules/NativeBlobModule.js +4 -2
- package/src/private/specs/modules/NativeDevSettings.js +1 -0
- package/src/private/specs/modules/NativePushNotificationManagerIOS.js +0 -4
- package/src/private/specs/modules/NativeUIManager.js +0 -7
- package/src/private/webapis/dom/geometry/DOMRectReadOnly.js +24 -24
- package/src/private/webapis/dom/nodes/ReactNativeElement.js +11 -14
- package/src/private/webapis/dom/nodes/ReadOnlyCharacterData.js +2 -3
- package/src/private/webapis/dom/nodes/ReadOnlyElement.js +24 -54
- package/src/private/webapis/dom/nodes/ReadOnlyNode.js +5 -13
- package/src/private/webapis/dom/nodes/specs/NativeDOM.js +468 -0
- package/src/private/webapis/dom/nodes/specs/__mocks__/NativeDOMMock.js +413 -0
- package/src/private/webapis/dom/oldstylecollections/DOMRectList.js +4 -4
- package/src/private/webapis/dom/oldstylecollections/HTMLCollection.js +4 -4
- package/src/private/webapis/dom/oldstylecollections/NodeList.js +5 -5
- package/src/private/webapis/idlecallbacks/specs/NativeIdleCallbacks.js +34 -0
- package/src/private/webapis/microtasks/specs/NativeMicrotasks.js +21 -0
- package/src/private/webapis/performance/EventCounts.js +1 -1
- package/src/private/webapis/performance/MemoryInfo.js +9 -9
- package/src/private/webapis/performance/Performance.js +10 -56
- package/src/private/webapis/performance/PerformanceObserver.js +30 -22
- package/src/private/webapis/performance/RawPerformanceEntry.js +2 -7
- package/src/private/webapis/performance/ReactNativeStartupTiming.js +18 -18
- package/src/private/webapis/performance/UserTiming.js +63 -0
- package/src/private/webapis/performance/{NativePerformance.js → specs/NativePerformance.js} +3 -2
- package/src/private/webapis/performance/{NativePerformanceObserver.js → specs/NativePerformanceObserver.js} +2 -2
- package/src/private/webapis/performance/{__mocks__ → specs/__mocks__}/NativePerformance.js +1 -1
- package/src/private/webapis/performance/{__mocks__ → specs/__mocks__}/NativePerformanceObserver.js +3 -4
- package/stubs/glog/logging.h +1 -0
- package/template/cpp-lib/proj/MyLib.vcxproj +1 -1
- package/template/cs-lib/proj/MyLib.csproj +1 -1
- package/template/metro.config.js +13 -2
- package/templates/cpp-app/template.config.js +1 -1
- package/templates/cpp-lib/example/metro.config.js +2 -2
- package/templates/cpp-lib/template.config.js +1 -1
- package/templates/old/generateWrapper.js +4 -1
- package/types/modules/globals.d.ts +4 -0
- package/Libraries/Lists/FlatList.windows.js +0 -717
- package/Libraries/NativeModules/specs/NativeAnimationsDebugModule.js +0 -13
- package/Libraries/Utilities/LoadingView.ios.js +0 -50
- package/Libraries/Utilities/LoadingView.js +0 -16
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp +0 -178
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/utils/jsi-utils.cpp +0 -39
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/utils/jsi-utils.h +0 -31
- package/ReactCommon/TEMP_UntilReactCommonUpdate/yoga/yoga/YGEnums.h +0 -137
- package/ReactCommon/TEMP_UntilReactCommonUpdate/yoga/yoga/algorithm/CalculateLayout.cpp +0 -2377
- package/ReactCommon/TEMP_UntilReactCommonUpdate/yoga/yoga/config/Config.cpp +0 -140
- package/ReactCommon/TEMP_UntilReactCommonUpdate/yoga/yoga/config/Config.h +0 -92
- package/ReactCommon/TEMP_UntilReactCommonUpdate/yoga/yoga/enums/ExperimentalFeature.h +0 -40
- package/ReactCommon/TEMP_UntilReactCommonUpdate/yoga/yoga/node/LayoutResults.cpp +0 -48
- package/ReactCommon/TEMP_UntilReactCommonUpdate/yoga/yoga/node/LayoutResults.h +0 -122
- package/ReactCommon/TEMP_UntilReactCommonUpdate/yoga/yoga/node/Node.cpp +0 -366
- package/codegen/NativeAnimationsDebugModuleSpec.g.h +0 -40
- package/codegen/NativePlatformConstantsWinSpec.g.h +0 -81
- package/jest/ReactNativeInternalFeatureFlagsMock.js +0 -13
- package/src/private/featureflags/NativeReactNativeFeatureFlags.js +0 -44
- package/src/private/featureflags/__tests__/ReactNativeFeatureFlags-test.js +0 -92
- package/src/private/specs/modules/NativeAnimationsDebugModule.js +0 -20
- package/src/private/webapis/dom/oldstylecollections/__tests__/DOMRectList-test.js +0 -85
- package/src/private/webapis/dom/oldstylecollections/__tests__/HTMLCollection-test.js +0 -80
- package/src/private/webapis/dom/oldstylecollections/__tests__/NodeList-test.js +0 -161
- package/src/private/webapis/performance/__tests__/EventCounts-test.js +0 -116
- package/src/private/webapis/performance/__tests__/NativePerformanceMock-test.js +0 -82
- package/src/private/webapis/performance/__tests__/NativePerformanceObserverMock-test.js +0 -108
- package/src/private/webapis/performance/__tests__/Performance-test.js +0 -117
- package/src/private/webapis/performance/__tests__/PerformanceObserver-test.js +0 -208
- package/template/metro.devMode.config.js +0 -56
- /package/src/private/specs/modules/{NativePlatformConstantsWin.js → NativePlatformConstantsWindows.js} +0 -0
|
@@ -0,0 +1,970 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#pragma once
|
|
18
|
+
|
|
19
|
+
#include <cassert>
|
|
20
|
+
#include <cstddef>
|
|
21
|
+
#include <cstdint>
|
|
22
|
+
#include <functional>
|
|
23
|
+
#include <limits>
|
|
24
|
+
#include <type_traits>
|
|
25
|
+
|
|
26
|
+
#include <folly/Portability.h>
|
|
27
|
+
|
|
28
|
+
namespace folly {
|
|
29
|
+
|
|
30
|
+
/// numbers
|
|
31
|
+
///
|
|
32
|
+
/// mimic: std::numbers, C++20 (partial)
|
|
33
|
+
namespace numbers {
|
|
34
|
+
|
|
35
|
+
namespace detail {
|
|
36
|
+
template <typename T>
|
|
37
|
+
using enable_if_floating_t =
|
|
38
|
+
std::enable_if_t<std::is_floating_point<T>::value, T>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// e_v
|
|
42
|
+
///
|
|
43
|
+
/// mimic: std::numbers::e_v, C++20
|
|
44
|
+
template <typename T>
|
|
45
|
+
FOLLY_INLINE_VARIABLE constexpr T e_v = detail::enable_if_floating_t<T>(
|
|
46
|
+
2.71828182845904523536028747135266249775724709369995L);
|
|
47
|
+
|
|
48
|
+
/// ln2_v
|
|
49
|
+
///
|
|
50
|
+
/// mimic: std::numbers::ln2_v, C++20
|
|
51
|
+
template <typename T>
|
|
52
|
+
FOLLY_INLINE_VARIABLE constexpr T ln2_v = detail::enable_if_floating_t<T>(
|
|
53
|
+
0.69314718055994530941723212145817656807550013436025L);
|
|
54
|
+
|
|
55
|
+
/// e
|
|
56
|
+
///
|
|
57
|
+
/// mimic: std::numbers::e, C++20
|
|
58
|
+
FOLLY_INLINE_VARIABLE constexpr double e = e_v<double>;
|
|
59
|
+
|
|
60
|
+
/// ln2
|
|
61
|
+
///
|
|
62
|
+
/// mimic: std::numbers::ln2, C++20
|
|
63
|
+
FOLLY_INLINE_VARIABLE constexpr double ln2 = ln2_v<double>;
|
|
64
|
+
|
|
65
|
+
} // namespace numbers
|
|
66
|
+
|
|
67
|
+
/// floating_point_integral_constant
|
|
68
|
+
///
|
|
69
|
+
/// Like std::integral_constant but for floating-point types holding integral
|
|
70
|
+
/// values representable in an integral type.
|
|
71
|
+
template <typename T, typename S, S Value>
|
|
72
|
+
struct floating_point_integral_constant {
|
|
73
|
+
using value_type = T;
|
|
74
|
+
static constexpr value_type value = static_cast<value_type>(Value);
|
|
75
|
+
constexpr operator value_type() const noexcept { return value; }
|
|
76
|
+
constexpr value_type operator()() const noexcept { return value; }
|
|
77
|
+
};
|
|
78
|
+
#if FOLLY_CPLUSPLUS < 201703L
|
|
79
|
+
template <typename T, typename S, S Value>
|
|
80
|
+
constexpr typename floating_point_integral_constant<T, S, Value>::value_type
|
|
81
|
+
floating_point_integral_constant<T, S, Value>::value;
|
|
82
|
+
#endif
|
|
83
|
+
|
|
84
|
+
// ----
|
|
85
|
+
|
|
86
|
+
namespace detail {
|
|
87
|
+
|
|
88
|
+
template <typename T>
|
|
89
|
+
constexpr size_t constexpr_iterated_squares_desc_size_(T const base) {
|
|
90
|
+
using lim = std::numeric_limits<T>;
|
|
91
|
+
size_t s = 1;
|
|
92
|
+
auto r = base;
|
|
93
|
+
while (r <= lim::max() / r) {
|
|
94
|
+
++s;
|
|
95
|
+
r *= r;
|
|
96
|
+
}
|
|
97
|
+
return s;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
} // namespace detail
|
|
101
|
+
|
|
102
|
+
/// constexpr_iterated_squares_desc_size_v
|
|
103
|
+
///
|
|
104
|
+
/// Effectively calculates: floor(log(max_exponent)/log(base))
|
|
105
|
+
///
|
|
106
|
+
/// For use with constexpr_iterated_squares_desc below.
|
|
107
|
+
template <typename Base>
|
|
108
|
+
FOLLY_INLINE_VARIABLE constexpr size_t constexpr_iterated_squares_desc_size_v =
|
|
109
|
+
detail::constexpr_iterated_squares_desc_size_(Base::value);
|
|
110
|
+
|
|
111
|
+
/// constexpr_iterated_squares_desc
|
|
112
|
+
///
|
|
113
|
+
/// A constexpr scaling array of integer powers-of-powers-of-two, descending,
|
|
114
|
+
/// with the associated powers-of-two.
|
|
115
|
+
///
|
|
116
|
+
/// scaling = [..., {8, b^8}, {4, b^4}, {2, b^2}, {1, b^1}] for b = base
|
|
117
|
+
///
|
|
118
|
+
/// Includes select constexpr scaling algorithms based on the scaling array.
|
|
119
|
+
///
|
|
120
|
+
/// The scaling array and the scaling algorithms are general-purpose, if niche.
|
|
121
|
+
/// They may be used by other constexpr math functions (floating-point) either
|
|
122
|
+
/// to improve runtime performance or to improve numerical approximations.
|
|
123
|
+
///
|
|
124
|
+
/// Some compilers fail to support passing some types as non-type template
|
|
125
|
+
/// params. In particular, long double is not universally supported. Therefore,
|
|
126
|
+
/// this utility takes its base as a type rather than as a value. For floating-
|
|
127
|
+
/// point integral bases, that is, bases of floating-point type but of integral
|
|
128
|
+
/// value, floating_point_integral_constant is the easiest parameterization.
|
|
129
|
+
template <typename T, std::size_t Size>
|
|
130
|
+
struct constexpr_iterated_squares_desc {
|
|
131
|
+
static_assert(Size > 0, "requires non-zero size");
|
|
132
|
+
|
|
133
|
+
using size_type = decltype(Size);
|
|
134
|
+
using base_type = T;
|
|
135
|
+
|
|
136
|
+
struct item_type {
|
|
137
|
+
size_type power;
|
|
138
|
+
base_type scale;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
static constexpr size_type size = Size;
|
|
142
|
+
base_type base;
|
|
143
|
+
item_type scaling[size];
|
|
144
|
+
|
|
145
|
+
private:
|
|
146
|
+
using lim = std::numeric_limits<base_type>;
|
|
147
|
+
|
|
148
|
+
static_assert(
|
|
149
|
+
lim::max_exponent < std::numeric_limits<size_type>::max(),
|
|
150
|
+
"size_type too small for base_type");
|
|
151
|
+
|
|
152
|
+
public:
|
|
153
|
+
explicit constexpr constexpr_iterated_squares_desc(base_type r) noexcept
|
|
154
|
+
: base{r}, scaling{} {
|
|
155
|
+
assert(size <= detail::constexpr_iterated_squares_desc_size_(base));
|
|
156
|
+
size_type i = 0;
|
|
157
|
+
size_type p = 1;
|
|
158
|
+
while (true) { // a for-loop might cause multiplication overflow below
|
|
159
|
+
scaling[size - 1 - i] = {p, r};
|
|
160
|
+
if (++i == size) {
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
p *= 2;
|
|
164
|
+
r *= r;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/// shrink
|
|
169
|
+
///
|
|
170
|
+
/// Returns scaling params of the form:
|
|
171
|
+
/// item_type{power, scale} with scale = base ^ power
|
|
172
|
+
/// With power the smallest nonnegative integer such that:
|
|
173
|
+
/// abs(num) / scale <= max
|
|
174
|
+
constexpr item_type shrink(base_type const num, base_type const max) const {
|
|
175
|
+
assert(max > base_type(0));
|
|
176
|
+
auto const rmax = max / base;
|
|
177
|
+
auto const snum = num < base_type(0) ? -num : num;
|
|
178
|
+
auto power = size_type(0);
|
|
179
|
+
auto scale = base_type(1);
|
|
180
|
+
if (!(snum / scale <= max)) {
|
|
181
|
+
for (auto const& i : scaling) {
|
|
182
|
+
auto const next = scale * i.scale;
|
|
183
|
+
auto const div = snum / next;
|
|
184
|
+
if (div <= rmax) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
power += i.power;
|
|
188
|
+
scale = next;
|
|
189
|
+
if (div <= max) {
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
assert(snum / scale <= max);
|
|
195
|
+
return {power, scale};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/// growth
|
|
199
|
+
///
|
|
200
|
+
/// Returns scaling params of the form:
|
|
201
|
+
/// item_type{power, scale} with scale = base ^ power
|
|
202
|
+
/// With power the smallest nonnegative integer such that:
|
|
203
|
+
/// abs(num) * scale >= min
|
|
204
|
+
constexpr item_type growth(base_type const num, base_type const min) const {
|
|
205
|
+
assert(min > base_type(0));
|
|
206
|
+
auto const rmin = min * base;
|
|
207
|
+
auto const snum = num < base_type(0) ? -num : num;
|
|
208
|
+
auto power = size_type(0);
|
|
209
|
+
auto scale = base_type(1);
|
|
210
|
+
if (!(snum * scale >= min)) {
|
|
211
|
+
for (auto const& i : scaling) {
|
|
212
|
+
auto const next = scale * i.scale;
|
|
213
|
+
auto const mul = snum * next;
|
|
214
|
+
if (mul >= rmin) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
power += i.power;
|
|
218
|
+
scale = next;
|
|
219
|
+
if (mul >= min) {
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
assert(snum * scale >= min);
|
|
225
|
+
return {power, scale};
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
#if FOLLY_CPLUSPLUS < 201703L
|
|
229
|
+
template <typename T, std::size_t Size>
|
|
230
|
+
constexpr typename constexpr_iterated_squares_desc<T, Size>::size_type
|
|
231
|
+
constexpr_iterated_squares_desc<T, Size>::size;
|
|
232
|
+
#endif
|
|
233
|
+
|
|
234
|
+
/// constexpr_iterated_squares_desc_v
|
|
235
|
+
///
|
|
236
|
+
/// An instance of constexpr_iterated_squares_desc of max size with the given
|
|
237
|
+
/// base.
|
|
238
|
+
template <typename Base>
|
|
239
|
+
FOLLY_INLINE_VARIABLE constexpr auto constexpr_iterated_squares_desc_v =
|
|
240
|
+
constexpr_iterated_squares_desc<
|
|
241
|
+
typename Base::value_type,
|
|
242
|
+
constexpr_iterated_squares_desc_size_v<Base>>{Base::value};
|
|
243
|
+
|
|
244
|
+
/// constexpr_iterated_squares_desc_2_v
|
|
245
|
+
///
|
|
246
|
+
/// An alias for constexpr_iterated_squares_desc_v with base 2, which is the
|
|
247
|
+
/// most common base to use with iterated-squares.
|
|
248
|
+
template <typename T>
|
|
249
|
+
constexpr auto& constexpr_iterated_squares_desc_2_v =
|
|
250
|
+
constexpr_iterated_squares_desc_v<
|
|
251
|
+
floating_point_integral_constant<T, int, 2>>;
|
|
252
|
+
|
|
253
|
+
// TLDR: Prefer using operator< for ordering. And when
|
|
254
|
+
// a and b are equivalent objects, we return b to make
|
|
255
|
+
// sorting stable.
|
|
256
|
+
// See http://stepanovpapers.com/notes.pdf for details.
|
|
257
|
+
template <typename T, typename... Ts>
|
|
258
|
+
constexpr T constexpr_max(T a, Ts... ts) {
|
|
259
|
+
T list[] = {ts..., a}; // 0-length arrays are illegal
|
|
260
|
+
// [Windows #12703 - Fix folly CodeQL issues]
|
|
261
|
+
for (size_t i = 0; i < sizeof...(Ts); ++i) {
|
|
262
|
+
a = list[i] < a ? a : list[i];
|
|
263
|
+
}
|
|
264
|
+
return a;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// When a and b are equivalent objects, we return a to
|
|
268
|
+
// make sorting stable.
|
|
269
|
+
template <typename T, typename... Ts>
|
|
270
|
+
constexpr T constexpr_min(T a, Ts... ts) {
|
|
271
|
+
T list[] = {ts..., a}; // 0-length arrays are illegal
|
|
272
|
+
// [Windows #12703 - Fix folly CodeQL issues]
|
|
273
|
+
for (size_t i = 0; i < sizeof...(Ts); ++i) {
|
|
274
|
+
a = list[i] < a ? list[i] : a;
|
|
275
|
+
}
|
|
276
|
+
return a;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
template <typename T, typename Less>
|
|
280
|
+
constexpr T const& constexpr_clamp(
|
|
281
|
+
T const& v, T const& lo, T const& hi, Less less) {
|
|
282
|
+
T const& a = less(v, lo) ? lo : v;
|
|
283
|
+
T const& b = less(hi, a) ? hi : a;
|
|
284
|
+
return b;
|
|
285
|
+
}
|
|
286
|
+
template <typename T>
|
|
287
|
+
constexpr T const& constexpr_clamp(T const& v, T const& lo, T const& hi) {
|
|
288
|
+
return constexpr_clamp(v, lo, hi, std::less<T>{});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
template <typename T>
|
|
292
|
+
constexpr bool constexpr_isnan(T const t) {
|
|
293
|
+
return t != t; // NOLINT
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
namespace detail {
|
|
297
|
+
|
|
298
|
+
template <typename T, typename = void>
|
|
299
|
+
struct constexpr_abs_helper {};
|
|
300
|
+
|
|
301
|
+
template <typename T>
|
|
302
|
+
struct constexpr_abs_helper<
|
|
303
|
+
T,
|
|
304
|
+
typename std::enable_if<std::is_floating_point<T>::value>::type> {
|
|
305
|
+
static constexpr T go(T t) { return t < static_cast<T>(0) ? -t : t; }
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
template <typename T>
|
|
309
|
+
struct constexpr_abs_helper<
|
|
310
|
+
T,
|
|
311
|
+
typename std::enable_if<
|
|
312
|
+
std::is_integral<T>::value && !std::is_same<T, bool>::value &&
|
|
313
|
+
std::is_unsigned<T>::value>::type> {
|
|
314
|
+
static constexpr T go(T t) { return t; }
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
template <typename T>
|
|
318
|
+
struct constexpr_abs_helper<
|
|
319
|
+
T,
|
|
320
|
+
typename std::enable_if<
|
|
321
|
+
std::is_integral<T>::value && !std::is_same<T, bool>::value &&
|
|
322
|
+
std::is_signed<T>::value>::type> {
|
|
323
|
+
static constexpr typename std::make_unsigned<T>::type go(T t) {
|
|
324
|
+
return typename std::make_unsigned<T>::type(t < static_cast<T>(0) ? -t : t);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
} // namespace detail
|
|
329
|
+
|
|
330
|
+
template <typename T>
|
|
331
|
+
constexpr auto constexpr_abs(T t)
|
|
332
|
+
-> decltype(detail::constexpr_abs_helper<T>::go(t)) {
|
|
333
|
+
return detail::constexpr_abs_helper<T>::go(t);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
namespace detail {
|
|
337
|
+
|
|
338
|
+
template <typename T>
|
|
339
|
+
constexpr T constexpr_log2_(T a, T e) {
|
|
340
|
+
return e == T(1) ? a : constexpr_log2_(a + T(1), e / T(2));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
template <typename T>
|
|
344
|
+
constexpr T constexpr_log2_ceil_(T l2, T t) {
|
|
345
|
+
return l2 + T(T(1) << l2 < t ? 1 : 0);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
} // namespace detail
|
|
349
|
+
|
|
350
|
+
template <typename T>
|
|
351
|
+
constexpr T constexpr_log2(T t) {
|
|
352
|
+
return detail::constexpr_log2_(T(0), t);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
template <typename T>
|
|
356
|
+
constexpr T constexpr_log2_ceil(T t) {
|
|
357
|
+
return detail::constexpr_log2_ceil_(constexpr_log2(t), t);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/// constexpr_trunc
|
|
361
|
+
///
|
|
362
|
+
/// mimic: std::trunc (C++23)
|
|
363
|
+
template <
|
|
364
|
+
typename T,
|
|
365
|
+
std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
|
|
366
|
+
constexpr T constexpr_trunc(T const t) {
|
|
367
|
+
using lim = std::numeric_limits<T>;
|
|
368
|
+
using int_type = std::uintmax_t;
|
|
369
|
+
using int_lim = std::numeric_limits<int_type>;
|
|
370
|
+
static_assert(lim::radix == 2, "non-binary radix");
|
|
371
|
+
static_assert(lim::digits <= int_lim::digits, "overwide mantissa");
|
|
372
|
+
constexpr auto bound = static_cast<T>(std::uintmax_t(1) << (lim::digits - 1));
|
|
373
|
+
auto const neg = !constexpr_isnan(t) && t < T(0);
|
|
374
|
+
auto const s = neg ? -t : t;
|
|
375
|
+
if (constexpr_isnan(t) || t == T(0) || !(s < bound)) {
|
|
376
|
+
return t;
|
|
377
|
+
}
|
|
378
|
+
if (s < T(1)) {
|
|
379
|
+
return neg ? -T(0) : T(0);
|
|
380
|
+
}
|
|
381
|
+
auto const r = static_cast<T>(static_cast<int_type>(s));
|
|
382
|
+
return neg ? -r : r;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
template <typename T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
|
|
386
|
+
constexpr T constexpr_trunc(T const t) {
|
|
387
|
+
return t;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/// constexpr_round
|
|
391
|
+
///
|
|
392
|
+
/// mimic: std::round (C++23)
|
|
393
|
+
template <typename T>
|
|
394
|
+
constexpr T constexpr_round(T const t) {
|
|
395
|
+
constexpr auto half = T(1) / T(2);
|
|
396
|
+
auto const same = constexpr_isnan(t) || t == T(0);
|
|
397
|
+
return same ? t : constexpr_trunc(t < T(0) ? t - half : t + half);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/// constexpr_floor
|
|
401
|
+
///
|
|
402
|
+
/// mimic: std::floor (C++23)
|
|
403
|
+
template <typename T>
|
|
404
|
+
constexpr T constexpr_floor(T const t) {
|
|
405
|
+
auto const s = constexpr_trunc(t);
|
|
406
|
+
return t < s ? s - T(1) : s;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/// constexpr_ceil
|
|
410
|
+
///
|
|
411
|
+
/// mimic: std::ceil (C++23)
|
|
412
|
+
template <typename T>
|
|
413
|
+
constexpr T constexpr_ceil(T const t) {
|
|
414
|
+
auto const s = constexpr_trunc(t);
|
|
415
|
+
return s < t ? s + T(1) : s;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/// constexpr_ceil
|
|
419
|
+
///
|
|
420
|
+
/// The least integer at least t that round divides.
|
|
421
|
+
template <typename T>
|
|
422
|
+
constexpr T constexpr_ceil(T t, T round) {
|
|
423
|
+
return round == T(0)
|
|
424
|
+
? t
|
|
425
|
+
: ((t + (t < T(0) ? T(0) : round - T(1))) / round) * round;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/// constexpr_mult
|
|
429
|
+
///
|
|
430
|
+
/// Multiply two values, allowing for constexpr floating-pooint overflow to
|
|
431
|
+
/// infinity.
|
|
432
|
+
template <typename T>
|
|
433
|
+
constexpr T constexpr_mult(T const a, T const b) {
|
|
434
|
+
using lim = std::numeric_limits<T>;
|
|
435
|
+
if (constexpr_isnan(a) || constexpr_isnan(b)) {
|
|
436
|
+
return constexpr_isnan(a) ? a : b;
|
|
437
|
+
}
|
|
438
|
+
if (std::is_floating_point<T>::value) {
|
|
439
|
+
constexpr auto inf = lim::infinity();
|
|
440
|
+
auto const ax = constexpr_abs(a);
|
|
441
|
+
auto const bx = constexpr_abs(b);
|
|
442
|
+
if ((ax == T(0) && bx == inf) || (bx == T(0) && ax == inf)) {
|
|
443
|
+
return lim::quiet_NaN();
|
|
444
|
+
}
|
|
445
|
+
// floating-point multiplication overflow, ie where multiplication of two
|
|
446
|
+
// finite values overflows to infinity of either sign, is not constexpr per
|
|
447
|
+
// gcc
|
|
448
|
+
// floating-point division overflow, ie where division of two finite values
|
|
449
|
+
// overflows to infinity of either sign, is not constexpr per gcc
|
|
450
|
+
// floating-point division by zero is not constexpr per any compiler, but we
|
|
451
|
+
// use it in the checks for the other two conditions
|
|
452
|
+
if (ax != inf && bx != inf && T(1) < bx && lim::max() / bx < ax) {
|
|
453
|
+
auto const a_neg = static_cast<bool>(a < T(0));
|
|
454
|
+
auto const b_neg = static_cast<bool>(b < T(0));
|
|
455
|
+
auto const sign = a_neg == b_neg ? T(1) : T(-1);
|
|
456
|
+
return sign * inf;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return a * b;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
namespace detail {
|
|
463
|
+
|
|
464
|
+
template <
|
|
465
|
+
typename T,
|
|
466
|
+
typename E,
|
|
467
|
+
std::enable_if_t<std::is_signed<E>::value, int> = 1>
|
|
468
|
+
constexpr T constexpr_ipow(T const base, E const exp) {
|
|
469
|
+
if (std::is_floating_point<T>::value) {
|
|
470
|
+
if (exp < E(0)) {
|
|
471
|
+
return T(1) / constexpr_ipow(base, -exp);
|
|
472
|
+
}
|
|
473
|
+
if (exp == E(0)) {
|
|
474
|
+
return T(1);
|
|
475
|
+
}
|
|
476
|
+
if (constexpr_isnan(base)) {
|
|
477
|
+
return base;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
assert(!(exp < E(0)) && "negative exponent with integral base");
|
|
481
|
+
if (exp == E(0)) {
|
|
482
|
+
return T(1);
|
|
483
|
+
}
|
|
484
|
+
if (exp == E(1)) {
|
|
485
|
+
return base;
|
|
486
|
+
}
|
|
487
|
+
auto const hexp = constexpr_trunc(exp / E(2));
|
|
488
|
+
auto const div = constexpr_ipow(base, hexp);
|
|
489
|
+
auto const rem = hexp * E(2) == exp ? T(1) : base;
|
|
490
|
+
return constexpr_mult(constexpr_mult(div, div), rem);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
template <
|
|
494
|
+
typename T,
|
|
495
|
+
typename E,
|
|
496
|
+
std::enable_if_t<std::is_unsigned<E>::value, int> = 1>
|
|
497
|
+
constexpr T constexpr_ipow(T const base, E const exp) {
|
|
498
|
+
if (std::is_floating_point<T>::value) {
|
|
499
|
+
if (exp == E(0)) {
|
|
500
|
+
return T(1);
|
|
501
|
+
}
|
|
502
|
+
if (constexpr_isnan(base)) {
|
|
503
|
+
return base;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (exp == E(0)) {
|
|
507
|
+
return T(1);
|
|
508
|
+
}
|
|
509
|
+
if (exp == E(1)) {
|
|
510
|
+
return base;
|
|
511
|
+
}
|
|
512
|
+
auto const hexp = constexpr_trunc(exp / E(2));
|
|
513
|
+
auto const div = constexpr_ipow(base, hexp);
|
|
514
|
+
auto const rem = hexp * E(2) == exp ? T(1) : base;
|
|
515
|
+
return constexpr_mult(constexpr_mult(div, div), rem);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
} // namespace detail
|
|
519
|
+
|
|
520
|
+
/// constexpr_exp
|
|
521
|
+
///
|
|
522
|
+
/// Calculates an approximation of the mathematical function exp(num). Usable in
|
|
523
|
+
/// constant evaluations. Like std::exp, which becomes constexpr in C++26.
|
|
524
|
+
///
|
|
525
|
+
/// The integer overload uses iterated squaring and multiplication. The
|
|
526
|
+
/// floating-point overlaod naively evaluates the taylor series of exp(num)
|
|
527
|
+
/// until approximate convergence.
|
|
528
|
+
///
|
|
529
|
+
/// mimic: std::exp (C++23, C++26)
|
|
530
|
+
template <
|
|
531
|
+
typename T,
|
|
532
|
+
typename N,
|
|
533
|
+
std::enable_if_t<
|
|
534
|
+
std::is_floating_point<T>::value && std::is_integral<N>::value &&
|
|
535
|
+
!std::is_same<N, bool>::value,
|
|
536
|
+
int> = 0>
|
|
537
|
+
constexpr T constexpr_exp(N const power) {
|
|
538
|
+
auto const npower = constexpr_abs(power);
|
|
539
|
+
auto const result = detail::constexpr_ipow(numbers::e_v<T>, npower);
|
|
540
|
+
return power < N(0) ? T(1) / result : result;
|
|
541
|
+
}
|
|
542
|
+
template <
|
|
543
|
+
typename N,
|
|
544
|
+
std::enable_if_t<
|
|
545
|
+
std::is_integral<N>::value && !std::is_same<N, bool>::value,
|
|
546
|
+
int> = 0>
|
|
547
|
+
constexpr double constexpr_exp(N const power) {
|
|
548
|
+
return constexpr_exp<double>(power);
|
|
549
|
+
}
|
|
550
|
+
template <
|
|
551
|
+
typename T,
|
|
552
|
+
std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
|
|
553
|
+
constexpr T constexpr_exp(T const power) {
|
|
554
|
+
using lim = std::numeric_limits<T>;
|
|
555
|
+
|
|
556
|
+
// edge cases
|
|
557
|
+
if (constexpr_isnan(power)) {
|
|
558
|
+
return power;
|
|
559
|
+
}
|
|
560
|
+
if (power == -lim::infinity()) {
|
|
561
|
+
return +T(0);
|
|
562
|
+
}
|
|
563
|
+
if (power == +lim::infinity()) {
|
|
564
|
+
return power;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// convergence works better with positive powers since signs do not alternate
|
|
568
|
+
auto const abspower = constexpr_abs(power);
|
|
569
|
+
// convergence must short-circuit when terms grow to floating-point infinity
|
|
570
|
+
auto const bound = T(1) < abspower ? lim::max() / abspower : lim::infinity();
|
|
571
|
+
|
|
572
|
+
// term #index = power * coeff
|
|
573
|
+
auto index = size_t(0);
|
|
574
|
+
auto term = T(1);
|
|
575
|
+
// result = sum of terms
|
|
576
|
+
auto result = T(1);
|
|
577
|
+
// sum the terms until ~convergence
|
|
578
|
+
while (!(constexpr_abs(term) < lim::epsilon())) {
|
|
579
|
+
if (bound < term) {
|
|
580
|
+
return power < T(0) ? T(0) : lim::infinity();
|
|
581
|
+
}
|
|
582
|
+
index += 1;
|
|
583
|
+
term = term * abspower / index;
|
|
584
|
+
result += term;
|
|
585
|
+
}
|
|
586
|
+
return power < T(0) ? T(1) / result : result;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/// constexpr_log
|
|
590
|
+
///
|
|
591
|
+
/// Calculates an approximation of the natural logarithm ln(num).
|
|
592
|
+
///
|
|
593
|
+
/// The implementation uses a quickly-converging, high-precision iterative
|
|
594
|
+
/// technique as described in:
|
|
595
|
+
/// https://en.wikipedia.org/wiki/Natural_logarithm#High_precision
|
|
596
|
+
///
|
|
597
|
+
/// The technique works best with numbers that are close enough to 1, so the
|
|
598
|
+
/// implementation uses a quick shrink/growth technique as described in:
|
|
599
|
+
/// https://en.wikipedia.org/wiki/Natural_logarithm#Efficient_computation
|
|
600
|
+
template <
|
|
601
|
+
typename T,
|
|
602
|
+
std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
|
|
603
|
+
constexpr T constexpr_log(T const num) {
|
|
604
|
+
using lim = std::numeric_limits<T>;
|
|
605
|
+
constexpr auto& isq = constexpr_iterated_squares_desc_2_v<T>;
|
|
606
|
+
|
|
607
|
+
// edge cases
|
|
608
|
+
if (constexpr_isnan(num)) {
|
|
609
|
+
return num;
|
|
610
|
+
}
|
|
611
|
+
if (num < T(0)) {
|
|
612
|
+
return lim::quiet_NaN();
|
|
613
|
+
}
|
|
614
|
+
if (num == T(0)) {
|
|
615
|
+
return -lim::infinity();
|
|
616
|
+
}
|
|
617
|
+
if (num == lim::infinity()) {
|
|
618
|
+
return num;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// compression
|
|
622
|
+
auto const shrink = isq.shrink(num, isq.base);
|
|
623
|
+
auto const growth = isq.growth(num, T(1));
|
|
624
|
+
auto const scaled = num * growth.scale / shrink.scale;
|
|
625
|
+
assert(scaled <= isq.base);
|
|
626
|
+
assert(scaled >= T(1));
|
|
627
|
+
|
|
628
|
+
auto sum = T(0);
|
|
629
|
+
auto delta = T(2);
|
|
630
|
+
while (constexpr_abs(delta) >= lim::epsilon()) {
|
|
631
|
+
auto expterm = constexpr_exp(sum);
|
|
632
|
+
delta = T(2) * (scaled - expterm) / (scaled + expterm);
|
|
633
|
+
sum += delta;
|
|
634
|
+
}
|
|
635
|
+
auto const ln2 = numbers::ln2_v<T>;
|
|
636
|
+
return sum - growth.power * ln2 + shrink.power * ln2;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/// constexpr_pow
|
|
640
|
+
///
|
|
641
|
+
/// Calculates an approximation of the value of base raised to the exponent exp.
|
|
642
|
+
///
|
|
643
|
+
/// The implementation uses iterated squaring and multiplication for the integer
|
|
644
|
+
/// part of the exponent and uses the identity x^y = exp(y * log(x)) for the
|
|
645
|
+
/// fractional part of the exponent.
|
|
646
|
+
///
|
|
647
|
+
/// Notes:
|
|
648
|
+
/// * Forbids base of +0 or -0 with finite non-positive exponent: in part since
|
|
649
|
+
/// the plausible infinite result would be sensitive to the sign of the zero;
|
|
650
|
+
/// and in part since std::pow would be required or permitted to raise error
|
|
651
|
+
/// div-by-zero.
|
|
652
|
+
/// * Forbids finite negative base with finite non-integer exponent: in part
|
|
653
|
+
/// since std::pow would be required to raise error invalid.
|
|
654
|
+
///
|
|
655
|
+
/// mimic: std::pow (C++26)
|
|
656
|
+
template <
|
|
657
|
+
typename T,
|
|
658
|
+
typename E,
|
|
659
|
+
std::enable_if_t<
|
|
660
|
+
std::is_integral<E>::value && !std::is_same<E, bool>::value,
|
|
661
|
+
int> = 0>
|
|
662
|
+
constexpr T constexpr_pow(T const base, E const exp) {
|
|
663
|
+
return detail::constexpr_ipow(base, exp);
|
|
664
|
+
}
|
|
665
|
+
template <
|
|
666
|
+
typename T,
|
|
667
|
+
std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
|
|
668
|
+
constexpr T constexpr_pow(T const base, T const exp) {
|
|
669
|
+
using lim = std::numeric_limits<T>;
|
|
670
|
+
|
|
671
|
+
// edge cases
|
|
672
|
+
if (exp == T(0)) {
|
|
673
|
+
return T(1);
|
|
674
|
+
}
|
|
675
|
+
if (constexpr_isnan(base)) {
|
|
676
|
+
return base;
|
|
677
|
+
}
|
|
678
|
+
if (exp == lim::infinity() || exp == -lim::infinity()) {
|
|
679
|
+
auto const abase = constexpr_abs(base);
|
|
680
|
+
if (abase < T(1)) {
|
|
681
|
+
return exp == lim::infinity() ? T(0) : lim::infinity();
|
|
682
|
+
}
|
|
683
|
+
if (T(1) < abase) {
|
|
684
|
+
return exp == lim::infinity() ? lim::infinity() : T(0);
|
|
685
|
+
}
|
|
686
|
+
return T(1);
|
|
687
|
+
}
|
|
688
|
+
if (base == T(1)) {
|
|
689
|
+
return base;
|
|
690
|
+
}
|
|
691
|
+
if (constexpr_isnan(exp)) {
|
|
692
|
+
return exp;
|
|
693
|
+
}
|
|
694
|
+
assert(base != T(0) || exp > T(0)); // error div-by-zero
|
|
695
|
+
if (base == lim::infinity()) {
|
|
696
|
+
return exp < T(0) ? T(0) : lim::infinity();
|
|
697
|
+
}
|
|
698
|
+
if (base == -lim::infinity()) {
|
|
699
|
+
auto const oddi = //
|
|
700
|
+
exp == constexpr_trunc(exp) &&
|
|
701
|
+
exp != constexpr_trunc(exp / T(2)) * T(2);
|
|
702
|
+
return (oddi ? -T(1) : T(1)) * (exp < T(0) ? T(0) : lim::infinity());
|
|
703
|
+
}
|
|
704
|
+
if (base == T(0)) {
|
|
705
|
+
auto const oddi = //
|
|
706
|
+
exp == constexpr_trunc(exp) &&
|
|
707
|
+
exp != constexpr_trunc(exp / T(2)) * T(2);
|
|
708
|
+
return oddi ? base : T(0);
|
|
709
|
+
}
|
|
710
|
+
if (exp < T(0)) {
|
|
711
|
+
return T(1) / constexpr_pow(base, -exp);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// as an identity: x^y = exp(y * log(x)); but calculation is imprecise ... so,
|
|
715
|
+
// for better precision, split the calculation into its integral-power and its
|
|
716
|
+
// fractional-power components
|
|
717
|
+
// as a cost, the complexity of constexpr_ipow here is logarithmic in y, i.e.,
|
|
718
|
+
// linear in the logarithm of y, which can be prohibitive
|
|
719
|
+
auto const exp_trunc = constexpr_trunc(exp);
|
|
720
|
+
assert(T(0) < base || exp == exp_trunc); // error invalid
|
|
721
|
+
auto const exp_fract = exp - exp_trunc;
|
|
722
|
+
auto const anyi = exp_fract == T(0);
|
|
723
|
+
return constexpr_mult(
|
|
724
|
+
detail::constexpr_ipow(base, exp_trunc),
|
|
725
|
+
anyi ? T(1) : constexpr_exp(exp_fract * constexpr_log(base)));
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/// constexpr_find_last_set
|
|
729
|
+
///
|
|
730
|
+
/// Return the 1-based index of the most significant bit which is set.
|
|
731
|
+
/// For x > 0, constexpr_find_last_set(x) == 1 + floor(log2(x)).
|
|
732
|
+
template <typename T>
|
|
733
|
+
constexpr std::size_t constexpr_find_last_set(T const t) {
|
|
734
|
+
using U = std::make_unsigned_t<T>;
|
|
735
|
+
return t == T(0) ? 0 : 1 + constexpr_log2(static_cast<U>(t));
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
namespace detail {
|
|
739
|
+
template <typename U>
|
|
740
|
+
constexpr std::size_t constexpr_find_first_set_(
|
|
741
|
+
std::size_t s, std::size_t a, U const u) {
|
|
742
|
+
return s == 0 ? a
|
|
743
|
+
: constexpr_find_first_set_(
|
|
744
|
+
s / 2, a + s * bool((u >> a) % (U(1) << s) == U(0)), u);
|
|
745
|
+
}
|
|
746
|
+
} // namespace detail
|
|
747
|
+
|
|
748
|
+
/// constexpr_find_first_set
|
|
749
|
+
///
|
|
750
|
+
/// Return the 1-based index of the least significant bit which is set.
|
|
751
|
+
/// For x > 0, the exponent in the largest power of two which does not divide x.
|
|
752
|
+
template <typename T>
|
|
753
|
+
constexpr std::size_t constexpr_find_first_set(T t) {
|
|
754
|
+
using U = std::make_unsigned_t<T>;
|
|
755
|
+
using size = std::integral_constant<std::size_t, sizeof(T) * 4>;
|
|
756
|
+
return t == T(0)
|
|
757
|
+
? 0
|
|
758
|
+
: 1 + detail::constexpr_find_first_set_(size{}, 0, static_cast<U>(t));
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
template <typename T>
|
|
762
|
+
constexpr T constexpr_add_overflow_clamped(T a, T b) {
|
|
763
|
+
using L = std::numeric_limits<T>;
|
|
764
|
+
using M = std::intmax_t;
|
|
765
|
+
static_assert(
|
|
766
|
+
!std::is_integral<T>::value || sizeof(T) <= sizeof(M),
|
|
767
|
+
"Integral type too large!");
|
|
768
|
+
// clang-format off
|
|
769
|
+
return
|
|
770
|
+
// don't do anything special for non-integral types.
|
|
771
|
+
!std::is_integral<T>::value ? a + b :
|
|
772
|
+
// for narrow integral types, just convert to intmax_t.
|
|
773
|
+
sizeof(T) < sizeof(M)
|
|
774
|
+
? T(constexpr_clamp(
|
|
775
|
+
static_cast<M>(a) + static_cast<M>(b),
|
|
776
|
+
static_cast<M>(L::min()),
|
|
777
|
+
static_cast<M>(L::max()))) :
|
|
778
|
+
// when a >= 0, cannot add more than `MAX - a` onto a.
|
|
779
|
+
!(a < 0) ? a + constexpr_min(b, T(L::max() - a)) :
|
|
780
|
+
// a < 0 && b >= 0, `a + b` will always be in valid range of type T.
|
|
781
|
+
!(b < 0) ? a + b :
|
|
782
|
+
// a < 0 && b < 0, keep the result >= MIN.
|
|
783
|
+
a + constexpr_max(b, T(L::min() - a));
|
|
784
|
+
// clang-format on
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
template <typename T>
|
|
788
|
+
constexpr T constexpr_sub_overflow_clamped(T a, T b) {
|
|
789
|
+
using L = std::numeric_limits<T>;
|
|
790
|
+
using M = std::intmax_t;
|
|
791
|
+
static_assert(
|
|
792
|
+
!std::is_integral<T>::value || sizeof(T) <= sizeof(M),
|
|
793
|
+
"Integral type too large!");
|
|
794
|
+
// clang-format off
|
|
795
|
+
return
|
|
796
|
+
// don't do anything special for non-integral types.
|
|
797
|
+
!std::is_integral<T>::value ? a - b :
|
|
798
|
+
// for unsigned type, keep result >= 0.
|
|
799
|
+
std::is_unsigned<T>::value ? (a < b ? 0 : a - b) :
|
|
800
|
+
// for narrow signed integral types, just convert to intmax_t.
|
|
801
|
+
sizeof(T) < sizeof(M)
|
|
802
|
+
? T(constexpr_clamp(
|
|
803
|
+
static_cast<M>(a) - static_cast<M>(b),
|
|
804
|
+
static_cast<M>(L::min()),
|
|
805
|
+
static_cast<M>(L::max()))) :
|
|
806
|
+
// (a >= 0 && b >= 0) || (a < 0 && b < 0), `a - b` will always be valid.
|
|
807
|
+
(a < 0) == (b < 0) ? a - b :
|
|
808
|
+
// MIN < b, so `-b` should be in valid range (-MAX <= -b <= MAX),
|
|
809
|
+
// convert subtraction to addition.
|
|
810
|
+
L::min() < b ? constexpr_add_overflow_clamped(a, T(-b)) :
|
|
811
|
+
// -b = -MIN = (MAX + 1) and a <= -1, result is in valid range.
|
|
812
|
+
a < 0 ? a - b :
|
|
813
|
+
// -b = -MIN = (MAX + 1) and a >= 0, result > MAX.
|
|
814
|
+
L::max();
|
|
815
|
+
// clang-format on
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// clamp_cast<> provides sane numeric conversions from float point numbers to
|
|
819
|
+
// integral numbers, and between different types of integral numbers. It helps
|
|
820
|
+
// to avoid unexpected bugs introduced by bad conversion, and undefined behavior
|
|
821
|
+
// like overflow when casting float point numbers to integral numbers.
|
|
822
|
+
//
|
|
823
|
+
// When doing clamp_cast<Dst>(value), if `value` is in valid range of Dst,
|
|
824
|
+
// it will give correct result in Dst, equal to `value`.
|
|
825
|
+
//
|
|
826
|
+
// If `value` is outside the representable range of Dst, it will be clamped to
|
|
827
|
+
// MAX or MIN in Dst, instead of being undefined behavior.
|
|
828
|
+
//
|
|
829
|
+
// Float NaNs are converted to 0 in integral type.
|
|
830
|
+
//
|
|
831
|
+
// Here's some comparison with static_cast<>:
|
|
832
|
+
// (with FB-internal gcc-5-glibc-2.23 toolchain)
|
|
833
|
+
//
|
|
834
|
+
// static_cast<int32_t>(NaN) = 6
|
|
835
|
+
// clamp_cast<int32_t>(NaN) = 0
|
|
836
|
+
//
|
|
837
|
+
// static_cast<int32_t>(9999999999.0f) = -348639895
|
|
838
|
+
// clamp_cast<int32_t>(9999999999.0f) = 2147483647
|
|
839
|
+
//
|
|
840
|
+
// static_cast<int32_t>(2147483647.0f) = -348639895
|
|
841
|
+
// clamp_cast<int32_t>(2147483647.0f) = 2147483647
|
|
842
|
+
//
|
|
843
|
+
// static_cast<uint32_t>(4294967295.0f) = 0
|
|
844
|
+
// clamp_cast<uint32_t>(4294967295.0f) = 4294967295
|
|
845
|
+
//
|
|
846
|
+
// static_cast<uint32_t>(-1) = 4294967295
|
|
847
|
+
// clamp_cast<uint32_t>(-1) = 0
|
|
848
|
+
//
|
|
849
|
+
// static_cast<int16_t>(32768u) = -32768
|
|
850
|
+
// clamp_cast<int16_t>(32768u) = 32767
|
|
851
|
+
|
|
852
|
+
template <typename Dst, typename Src>
|
|
853
|
+
constexpr typename std::enable_if<std::is_integral<Src>::value, Dst>::type
|
|
854
|
+
constexpr_clamp_cast(Src src) {
|
|
855
|
+
static_assert(
|
|
856
|
+
std::is_integral<Dst>::value && sizeof(Dst) <= sizeof(int64_t),
|
|
857
|
+
"constexpr_clamp_cast can only cast into integral type (up to 64bit)");
|
|
858
|
+
|
|
859
|
+
using L = std::numeric_limits<Dst>;
|
|
860
|
+
// clang-format off
|
|
861
|
+
return
|
|
862
|
+
// Check if Src and Dst have same signedness.
|
|
863
|
+
std::is_signed<Src>::value == std::is_signed<Dst>::value
|
|
864
|
+
? (
|
|
865
|
+
// Src and Dst have same signedness. If sizeof(Src) <= sizeof(Dst),
|
|
866
|
+
// we can safely convert Src to Dst without any loss of accuracy.
|
|
867
|
+
sizeof(Src) <= sizeof(Dst) ? Dst(src) :
|
|
868
|
+
// If Src is larger in size, we need to clamp it to valid range in Dst.
|
|
869
|
+
Dst(constexpr_clamp(src, Src(L::min()), Src(L::max()))))
|
|
870
|
+
// Src and Dst have different signedness.
|
|
871
|
+
// Check if it's signed -> unsigend cast.
|
|
872
|
+
: std::is_signed<Src>::value && std::is_unsigned<Dst>::value
|
|
873
|
+
? (
|
|
874
|
+
// If src < 0, the result should be 0.
|
|
875
|
+
src < 0 ? Dst(0) :
|
|
876
|
+
// Otherwise, src >= 0. If src can fit into Dst, we can safely cast it
|
|
877
|
+
// without loss of accuracy.
|
|
878
|
+
sizeof(Src) <= sizeof(Dst) ? Dst(src) :
|
|
879
|
+
// If Src is larger in size than Dst, we need to ensure the result is
|
|
880
|
+
// at most Dst MAX.
|
|
881
|
+
Dst(constexpr_min(src, Src(L::max()))))
|
|
882
|
+
// It's unsigned -> signed cast.
|
|
883
|
+
: (
|
|
884
|
+
// Since Src is unsigned, and Dst is signed, Src can fit into Dst only
|
|
885
|
+
// when sizeof(Src) < sizeof(Dst).
|
|
886
|
+
sizeof(Src) < sizeof(Dst) ? Dst(src) :
|
|
887
|
+
// If Src does not fit into Dst, we need to ensure the result is at most
|
|
888
|
+
// Dst MAX.
|
|
889
|
+
Dst(constexpr_min(src, Src(L::max()))));
|
|
890
|
+
// clang-format on
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
namespace detail {
|
|
894
|
+
// Upper/lower bound values that could be accurately represented in both
|
|
895
|
+
// integral and float point types.
|
|
896
|
+
constexpr double kClampCastLowerBoundDoubleToInt64F = -9223372036854774784.0;
|
|
897
|
+
constexpr double kClampCastUpperBoundDoubleToInt64F = 9223372036854774784.0;
|
|
898
|
+
constexpr double kClampCastUpperBoundDoubleToUInt64F = 18446744073709549568.0;
|
|
899
|
+
|
|
900
|
+
constexpr float kClampCastLowerBoundFloatToInt32F = -2147483520.0f;
|
|
901
|
+
constexpr float kClampCastUpperBoundFloatToInt32F = 2147483520.0f;
|
|
902
|
+
constexpr float kClampCastUpperBoundFloatToUInt32F = 4294967040.0f;
|
|
903
|
+
|
|
904
|
+
// This works the same as constexpr_clamp, but the comparison are done in Src
|
|
905
|
+
// to prevent any implicit promotions.
|
|
906
|
+
template <typename D, typename S>
|
|
907
|
+
constexpr D constexpr_clamp_cast_helper(S src, S sl, S su, D dl, D du) {
|
|
908
|
+
return src < sl ? dl : (src > su ? du : D(src));
|
|
909
|
+
}
|
|
910
|
+
} // namespace detail
|
|
911
|
+
|
|
912
|
+
template <typename Dst, typename Src>
|
|
913
|
+
constexpr typename std::enable_if<std::is_floating_point<Src>::value, Dst>::type
|
|
914
|
+
constexpr_clamp_cast(Src src) {
|
|
915
|
+
static_assert(
|
|
916
|
+
std::is_integral<Dst>::value && sizeof(Dst) <= sizeof(int64_t),
|
|
917
|
+
"constexpr_clamp_cast can only cast into integral type (up to 64bit)");
|
|
918
|
+
|
|
919
|
+
using L = std::numeric_limits<Dst>;
|
|
920
|
+
// clang-format off
|
|
921
|
+
return
|
|
922
|
+
// Special case: cast NaN into 0.
|
|
923
|
+
constexpr_isnan(src) ? Dst(0) :
|
|
924
|
+
// using `sizeof(Src) > sizeof(Dst)` as a heuristic that Dst can be
|
|
925
|
+
// represented in Src without loss of accuracy.
|
|
926
|
+
// see: https://en.wikipedia.org/wiki/Floating-point_arithmetic
|
|
927
|
+
sizeof(Src) > sizeof(Dst) ?
|
|
928
|
+
detail::constexpr_clamp_cast_helper(
|
|
929
|
+
src, Src(L::min()), Src(L::max()), L::min(), L::max()) :
|
|
930
|
+
// sizeof(Src) < sizeof(Dst) only happens when doing cast of
|
|
931
|
+
// 32bit float -> u/int64_t.
|
|
932
|
+
// Losslessly promote float into double, change into double -> u/int64_t.
|
|
933
|
+
sizeof(Src) < sizeof(Dst) ? (
|
|
934
|
+
src >= 0.0
|
|
935
|
+
? constexpr_clamp_cast<Dst>(
|
|
936
|
+
constexpr_clamp_cast<std::uint64_t>(double(src)))
|
|
937
|
+
: constexpr_clamp_cast<Dst>(
|
|
938
|
+
constexpr_clamp_cast<std::int64_t>(double(src)))) :
|
|
939
|
+
// The following are for sizeof(Src) == sizeof(Dst).
|
|
940
|
+
std::is_same<Src, double>::value && std::is_same<Dst, int64_t>::value ?
|
|
941
|
+
detail::constexpr_clamp_cast_helper(
|
|
942
|
+
double(src),
|
|
943
|
+
detail::kClampCastLowerBoundDoubleToInt64F,
|
|
944
|
+
detail::kClampCastUpperBoundDoubleToInt64F,
|
|
945
|
+
L::min(),
|
|
946
|
+
L::max()) :
|
|
947
|
+
std::is_same<Src, double>::value && std::is_same<Dst, uint64_t>::value ?
|
|
948
|
+
detail::constexpr_clamp_cast_helper(
|
|
949
|
+
double(src),
|
|
950
|
+
0.0,
|
|
951
|
+
detail::kClampCastUpperBoundDoubleToUInt64F,
|
|
952
|
+
L::min(),
|
|
953
|
+
L::max()) :
|
|
954
|
+
std::is_same<Src, float>::value && std::is_same<Dst, int32_t>::value ?
|
|
955
|
+
detail::constexpr_clamp_cast_helper(
|
|
956
|
+
float(src),
|
|
957
|
+
detail::kClampCastLowerBoundFloatToInt32F,
|
|
958
|
+
detail::kClampCastUpperBoundFloatToInt32F,
|
|
959
|
+
L::min(),
|
|
960
|
+
L::max()) :
|
|
961
|
+
detail::constexpr_clamp_cast_helper(
|
|
962
|
+
float(src),
|
|
963
|
+
0.0f,
|
|
964
|
+
detail::kClampCastUpperBoundFloatToUInt32F,
|
|
965
|
+
L::min(),
|
|
966
|
+
L::max());
|
|
967
|
+
// clang-format on
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
} // namespace folly
|