react-native-windows 0.84.0-preview.9 → 0.85.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.
Files changed (487) hide show
  1. package/.flowconfig +1 -5
  2. package/Common/unicode.cpp +36 -0
  3. package/Common/unicode.h +8 -0
  4. package/Directory.Build.props +1 -16
  5. package/Directory.Build.targets +12 -4
  6. package/Folly/ThreadNameStub.cpp +4 -6
  7. package/Libraries/ActionSheetIOS/ActionSheetIOS.js +3 -3
  8. package/Libraries/Animated/Animated.d.ts +8 -1
  9. package/Libraries/Animated/AnimatedEvent.js +4 -4
  10. package/Libraries/Animated/AnimatedImplementation.js +2 -2
  11. package/Libraries/Animated/animations/Animation.js +3 -3
  12. package/Libraries/Animated/animations/DecayAnimation.js +4 -4
  13. package/Libraries/Animated/animations/SpringAnimation.js +5 -5
  14. package/Libraries/Animated/animations/TimingAnimation.js +5 -5
  15. package/Libraries/Animated/createAnimatedComponent.js +7 -7
  16. package/Libraries/Animated/nodes/AnimatedColor.js +2 -2
  17. package/Libraries/Animated/nodes/AnimatedInterpolation.js +12 -12
  18. package/Libraries/Animated/nodes/AnimatedNode.js +3 -3
  19. package/Libraries/Animated/nodes/AnimatedObject.js +3 -3
  20. package/Libraries/Animated/nodes/AnimatedProps.js +6 -6
  21. package/Libraries/Animated/nodes/AnimatedStyle.js +7 -7
  22. package/Libraries/Animated/nodes/AnimatedTransform.js +13 -13
  23. package/Libraries/Animated/nodes/AnimatedValue.js +1 -1
  24. package/Libraries/Animated/nodes/AnimatedValueXY.js +1 -1
  25. package/Libraries/Animated/nodes/AnimatedWithChildren.js +1 -1
  26. package/Libraries/Animated/useAnimatedColor.d.ts +15 -0
  27. package/Libraries/Animated/useAnimatedColor.js +25 -0
  28. package/Libraries/Animated/useAnimatedValueXY.d.ts +15 -0
  29. package/Libraries/Animated/useAnimatedValueXY.js +26 -0
  30. package/Libraries/AppState/AppState.js +1 -1
  31. package/Libraries/BatchedBridge/MessageQueue.js +3 -3
  32. package/Libraries/BatchedBridge/NativeModules.js +12 -10
  33. package/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js +30 -26
  34. package/Libraries/Components/AccessibilityInfo/AccessibilityInfo.windows.js +34 -30
  35. package/Libraries/Components/ActivityIndicator/ActivityIndicator.js +2 -2
  36. package/Libraries/Components/Button.js +2 -2
  37. package/Libraries/Components/Button.windows.js +2 -2
  38. package/Libraries/Components/DrawerAndroid/DrawerLayoutAndroidTypes.js +2 -2
  39. package/Libraries/Components/Keyboard/Keyboard.js +6 -6
  40. package/Libraries/Components/Keyboard/KeyboardAvoidingView.js +1 -1
  41. package/Libraries/Components/LayoutConformance/LayoutConformance.js +1 -1
  42. package/Libraries/Components/LayoutConformance/LayoutConformanceNativeComponent.js +1 -1
  43. package/Libraries/Components/Pressable/Pressable.d.ts +5 -0
  44. package/Libraries/Components/Pressable/Pressable.js +3 -3
  45. package/Libraries/Components/Pressable/Pressable.windows.js +3 -3
  46. package/Libraries/Components/Pressable/useAndroidRippleForView.js +4 -4
  47. package/Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.js +1 -1
  48. package/Libraries/Components/ProgressBarAndroid/ProgressBarAndroidTypes.js +3 -3
  49. package/Libraries/Components/RefreshControl/RefreshControl.js +5 -5
  50. package/Libraries/Components/RefreshControl/RefreshControl.windows.js +5 -5
  51. package/Libraries/Components/ScrollView/AndroidHorizontalScrollViewNativeComponent.js +8 -12
  52. package/Libraries/Components/ScrollView/ScrollView.d.ts +9 -0
  53. package/Libraries/Components/ScrollView/ScrollView.js +19 -9
  54. package/Libraries/Components/ScrollView/ScrollView.windows.js +19 -9
  55. package/Libraries/Components/ScrollView/ScrollViewNativeComponent.js +8 -18
  56. package/Libraries/Components/ScrollView/ScrollViewNativeComponent.windows.js +8 -18
  57. package/Libraries/Components/ScrollView/ScrollViewNativeComponentType.js +4 -3
  58. package/Libraries/Components/ScrollView/ScrollViewStickyHeader.js +1 -1
  59. package/Libraries/Components/StaticRenderer.js +1 -1
  60. package/Libraries/Components/StatusBar/StatusBar.js +8 -8
  61. package/Libraries/Components/Switch/Switch.js +3 -3
  62. package/Libraries/Components/Switch/Switch.windows.js +3 -3
  63. package/Libraries/Components/TextInput/AndroidTextInputNativeComponent.js +33 -48
  64. package/Libraries/Components/TextInput/InputAccessoryView.js +1 -1
  65. package/Libraries/Components/TextInput/RCTTextInputViewConfig.js +6 -11
  66. package/Libraries/Components/TextInput/TextInput.flow.js +21 -20
  67. package/Libraries/Components/TextInput/TextInput.flow.windows.js +23 -22
  68. package/Libraries/Components/TextInput/TextInput.js +8 -4
  69. package/Libraries/Components/TextInput/TextInput.windows.js +3 -4
  70. package/Libraries/Components/TextInput/WindowsTextInputNativeComponent.js +33 -48
  71. package/Libraries/Components/Touchable/PooledClass.js +1 -1
  72. package/Libraries/Components/Touchable/Touchable.js +2 -2
  73. package/Libraries/Components/Touchable/Touchable.windows.js +2 -2
  74. package/Libraries/Components/Touchable/TouchableBounce.js +4 -4
  75. package/Libraries/Components/Touchable/TouchableBounce.windows.js +4 -4
  76. package/Libraries/Components/Touchable/TouchableHighlight.js +8 -8
  77. package/Libraries/Components/Touchable/TouchableHighlight.windows.js +9 -9
  78. package/Libraries/Components/Touchable/TouchableNativeFeedback.js +7 -7
  79. package/Libraries/Components/Touchable/TouchableNativeFeedback.windows.js +7 -7
  80. package/Libraries/Components/Touchable/TouchableOpacity.js +4 -4
  81. package/Libraries/Components/Touchable/TouchableOpacity.windows.js +4 -4
  82. package/Libraries/Components/Touchable/TouchableWithoutFeedback.js +1 -1
  83. package/Libraries/Components/Touchable/TouchableWithoutFeedback.windows.js +1 -1
  84. package/Libraries/Components/UnimplementedViews/UnimplementedView.js +1 -1
  85. package/Libraries/Components/View/ReactNativeStyleAttributes.js +68 -31
  86. package/Libraries/Components/View/View.windows.js +1 -1
  87. package/Libraries/Components/View/ViewAccessibility.js +7 -7
  88. package/Libraries/Components/View/ViewAccessibility.windows.js +7 -7
  89. package/Libraries/Components/View/ViewPropTypes.js +14 -14
  90. package/Libraries/Components/View/ViewPropTypes.windows.js +18 -16
  91. package/Libraries/Core/Devtools/parseHermesStack.js +8 -8
  92. package/Libraries/Core/Devtools/symbolicateStackTrace.js +2 -2
  93. package/Libraries/Core/ExceptionsManager.js +2 -2
  94. package/Libraries/Core/ExtendedError.js +3 -3
  95. package/Libraries/Core/RawEventEmitter.js +1 -1
  96. package/Libraries/Core/ReactNativeVersion.js +2 -2
  97. package/Libraries/Core/Timers/JSTimers.js +3 -2
  98. package/Libraries/Core/setUpDeveloperTools.js +1 -1
  99. package/Libraries/Core/setUpErrorHandling.js +4 -2
  100. package/Libraries/Core/setUpReactDevTools.js +23 -6
  101. package/Libraries/Core/setUpSegmentFetcher.js +1 -1
  102. package/Libraries/Debugging/DebuggingOverlay.js +2 -2
  103. package/Libraries/EventEmitter/NativeEventEmitter.js +7 -9
  104. package/Libraries/EventEmitter/RCTDeviceEventEmitter.js +1 -1
  105. package/Libraries/Image/AssetSourceResolver.js +1 -1
  106. package/Libraries/Image/AssetSourceResolver.windows.js +1 -1
  107. package/Libraries/Image/Image.android.js +172 -306
  108. package/Libraries/Image/ImageProps.js +9 -11
  109. package/Libraries/Image/ImageSource.js +2 -2
  110. package/Libraries/Image/ImageSourceUtils.js +8 -4
  111. package/Libraries/Image/ImageTypes.flow.js +2 -2
  112. package/Libraries/Image/ImageViewNativeComponent.js +7 -16
  113. package/Libraries/Image/nativeImageSource.js +1 -1
  114. package/Libraries/Interaction/InteractionManager.js +5 -4
  115. package/Libraries/Interaction/PanResponder.js +1 -1
  116. package/Libraries/LayoutAnimation/LayoutAnimation.js +2 -2
  117. package/Libraries/Linking/Linking.js +1 -1
  118. package/Libraries/Lists/FlatList.js +3 -3
  119. package/Libraries/Lists/SectionList.js +1 -1
  120. package/Libraries/Lists/SectionListModern.js +2 -2
  121. package/Libraries/LogBox/Data/LogBoxData.js +38 -24
  122. package/Libraries/LogBox/Data/LogBoxLog.js +43 -112
  123. package/Libraries/LogBox/Data/parseLogBoxLog.js +21 -117
  124. package/Libraries/LogBox/LogBox.js +42 -19
  125. package/Libraries/LogBox/LogBoxInspectorContainer.js +2 -2
  126. package/Libraries/LogBox/LogBoxNotificationContainer.js +2 -2
  127. package/Libraries/LogBox/UI/LogBoxButton.js +14 -16
  128. package/Libraries/LogBox/UI/LogBoxInspector.js +2 -2
  129. package/Libraries/LogBox/UI/LogBoxInspectorCodeFrame.js +6 -9
  130. package/Libraries/LogBox/UI/LogBoxInspectorCodeFrame.windows.js +6 -9
  131. package/Libraries/LogBox/UI/LogBoxInspectorFooter.js +1 -1
  132. package/Libraries/LogBox/UI/LogBoxInspectorFooterButton.js +1 -1
  133. package/Libraries/LogBox/UI/LogBoxInspectorHeader.js +1 -1
  134. package/Libraries/LogBox/UI/LogBoxInspectorHeader.windows.js +1 -1
  135. package/Libraries/LogBox/UI/LogBoxInspectorHeaderButton.js +1 -1
  136. package/Libraries/LogBox/UI/LogBoxInspectorMessageHeader.js +10 -15
  137. package/Libraries/LogBox/UI/LogBoxInspectorReactFrames.js +25 -18
  138. package/Libraries/LogBox/UI/LogBoxInspectorReactFrames.windows.js +15 -16
  139. package/Libraries/LogBox/UI/LogBoxInspectorSection.js +5 -7
  140. package/Libraries/LogBox/UI/LogBoxInspectorSourceMapStatus.js +8 -10
  141. package/Libraries/LogBox/UI/LogBoxInspectorStackFrame.js +8 -6
  142. package/Libraries/LogBox/UI/LogBoxInspectorStackFrame.windows.js +3 -5
  143. package/Libraries/LogBox/UI/LogBoxInspectorStackFrames.js +17 -29
  144. package/Libraries/LogBox/UI/LogBoxMessage.js +28 -37
  145. package/Libraries/LogBox/UI/LogBoxNotification.js +1 -1
  146. package/Libraries/Modal/Modal.d.ts +4 -0
  147. package/Libraries/Modal/Modal.js +3 -3
  148. package/Libraries/Modal/Modal.windows.js +7 -3
  149. package/Libraries/NativeComponent/BaseViewConfig.android.js +28 -52
  150. package/Libraries/NativeComponent/BaseViewConfig.ios.js +19 -21
  151. package/Libraries/NativeComponent/BaseViewConfig.windows.js +26 -17
  152. package/Libraries/Network/RCTNetworking.android.js +1 -1
  153. package/Libraries/Network/RCTNetworking.ios.js +1 -1
  154. package/Libraries/Network/RCTNetworking.js.flow +1 -1
  155. package/Libraries/Network/RCTNetworking.windows.js +1 -1
  156. package/Libraries/Network/RCTNetworkingEventDefinitions.flow.js +1 -1
  157. package/Libraries/Network/XMLHttpRequest.js +1 -1
  158. package/Libraries/PermissionsAndroid/PermissionsAndroid.js +3 -3
  159. package/Libraries/Pressability/Pressability.js +5 -5
  160. package/Libraries/Pressability/Pressability.windows.js +5 -5
  161. package/Libraries/Pressability/PressabilityDebug.js +1 -1
  162. package/Libraries/Pressability/PressabilityPerformanceEventEmitter.js +1 -1
  163. package/Libraries/Pressability/usePressability.js +3 -14
  164. package/Libraries/PushNotificationIOS/PushNotificationIOS.js +2 -2
  165. package/Libraries/ReactNative/AppContainer-dev.js +18 -1
  166. package/Libraries/ReactNative/AppContainer.js +1 -1
  167. package/Libraries/ReactNative/AppRegistry.flow.js +2 -2
  168. package/Libraries/ReactNative/AppRegistryImpl.js +2 -2
  169. package/Libraries/ReactNative/FabricUIManager.js +1 -1
  170. package/Libraries/ReactNative/RendererImplementation.js +4 -4
  171. package/Libraries/ReactNative/UIManagerProperties.js +1 -1
  172. package/Libraries/Renderer/implementations/ReactNativeRenderer-dev.js +3 -2
  173. package/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js +3 -2
  174. package/Libraries/Renderer/implementations/ReactNativeRenderer-profiling.js +3 -2
  175. package/Libraries/Renderer/shims/ReactFabric.js +1 -3
  176. package/Libraries/Renderer/shims/ReactFeatureFlags.js +1 -3
  177. package/Libraries/Renderer/shims/ReactNative.js +3 -2
  178. package/Libraries/Renderer/shims/ReactNativeTypes.js +1 -3
  179. package/Libraries/Renderer/shims/ReactNativeTypes.windows.js +1 -3
  180. package/Libraries/Renderer/shims/ReactNativeViewConfigRegistry.js +1 -3
  181. package/Libraries/Renderer/shims/createReactNativeComponentClass.js +1 -3
  182. package/Libraries/StyleSheet/PointPropType.js +1 -1
  183. package/Libraries/StyleSheet/Rect.js +1 -1
  184. package/Libraries/StyleSheet/StyleSheet.d.ts +0 -5
  185. package/Libraries/StyleSheet/StyleSheet.js +1 -1
  186. package/Libraries/StyleSheet/StyleSheet.js.flow +1 -1
  187. package/Libraries/StyleSheet/StyleSheetExports.js +16 -25
  188. package/Libraries/StyleSheet/StyleSheetExports.js.flow +2 -7
  189. package/Libraries/StyleSheet/StyleSheetTypes.js +33 -35
  190. package/Libraries/StyleSheet/private/_StyleSheetTypesOverrides.js +5 -5
  191. package/Libraries/StyleSheet/private/_TransformStyle.js +7 -7
  192. package/Libraries/StyleSheet/processBackgroundImage.js +7 -7
  193. package/Libraries/StyleSheet/processBackgroundPosition.js +4 -4
  194. package/Libraries/StyleSheet/processBackgroundRepeat.js +4 -4
  195. package/Libraries/StyleSheet/processBackgroundSize.js +4 -4
  196. package/Libraries/StyleSheet/processBoxShadow.js +44 -4
  197. package/Libraries/StyleSheet/processColorArray.js +2 -2
  198. package/Libraries/StyleSheet/processFilter.js +2 -2
  199. package/Libraries/Text/Text.js +295 -627
  200. package/Libraries/Text/Text.windows.js +342 -717
  201. package/Libraries/Text/TextNativeComponent.js +14 -7
  202. package/Libraries/Text/TextProps.js +4 -4
  203. package/Libraries/Text/TextProps.windows.js +4 -4
  204. package/Libraries/Types/CodegenTypes.js +1 -1
  205. package/Libraries/Types/CoreEventTypes.js +21 -21
  206. package/Libraries/Types/CoreEventTypes.windows.js +21 -21
  207. package/Libraries/Types/ReactDevToolsTypes.js +2 -2
  208. package/Libraries/Utilities/Appearance.js +6 -1
  209. package/Libraries/Utilities/Dimensions.js +1 -1
  210. package/Libraries/Utilities/HMRClient.js +32 -5
  211. package/Libraries/Utilities/IPerformanceLogger.js +4 -4
  212. package/Libraries/Utilities/codegenNativeCommands.js +4 -4
  213. package/Libraries/Utilities/codegenNativeComponent.d.ts +2 -0
  214. package/Libraries/Utilities/codegenNativeComponent.js +2 -2
  215. package/Libraries/Utilities/logError.js +1 -1
  216. package/Libraries/Utilities/stringifySafe.js +3 -3
  217. package/Libraries/Utilities/useMergeRefs.js +2 -2
  218. package/Libraries/vendor/core/ErrorUtils.js +4 -4
  219. package/Libraries/vendor/emitter/EventEmitter.js +16 -20
  220. package/Microsoft.ReactNative/CallInvoker.cpp +14 -4
  221. package/Microsoft.ReactNative/Fabric/AbiComponentDescriptor.cpp +1 -1
  222. package/Microsoft.ReactNative/Fabric/AbiPortalShadowNode.cpp +2 -2
  223. package/Microsoft.ReactNative/Fabric/AbiViewComponentDescriptor.h +1 -1
  224. package/Microsoft.ReactNative/Fabric/Composition/BorderPrimitive.cpp +73 -97
  225. package/Microsoft.ReactNative/Fabric/Composition/BorderPrimitive.h +4 -1
  226. package/Microsoft.ReactNative/Fabric/Composition/CompositionContextHelper.cpp +0 -1
  227. package/Microsoft.ReactNative/Fabric/Composition/CompositionDynamicAutomationProvider.cpp +5 -0
  228. package/Microsoft.ReactNative/Fabric/Composition/CompositionEventHandler.cpp +94 -26
  229. package/Microsoft.ReactNative/Fabric/Composition/CompositionEventHandler.h +11 -0
  230. package/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.cpp +135 -18
  231. package/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.h +10 -0
  232. package/Microsoft.ReactNative/Fabric/Composition/ImageComponentView.cpp +7 -2
  233. package/Microsoft.ReactNative/Fabric/Composition/ImageComponentView.h +2 -0
  234. package/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp +63 -15
  235. package/Microsoft.ReactNative/Fabric/Composition/ParagraphComponentView.cpp +9 -5
  236. package/Microsoft.ReactNative/Fabric/Composition/ScrollViewComponentView.cpp +28 -6
  237. package/Microsoft.ReactNative/Fabric/Composition/TextDrawing.cpp +1 -1
  238. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp +1 -1
  239. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputState.cpp +1 -1
  240. package/Microsoft.ReactNative/Fabric/Composition/TooltipService.cpp +80 -0
  241. package/Microsoft.ReactNative/Fabric/Composition/TooltipService.h +17 -0
  242. package/Microsoft.ReactNative/Fabric/FabricUIManagerModule.cpp +11 -0
  243. package/Microsoft.ReactNative/Fabric/FabricUIManagerModule.h +1 -0
  244. package/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/HostPlatformColor.h +3 -0
  245. package/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/PlatformColorUtils.cpp +0 -3
  246. package/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp +3 -8
  247. package/Microsoft.ReactNative/IReactPackageBuilder.idl +6 -0
  248. package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +4 -63
  249. package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj.filters +1 -26
  250. package/Microsoft.ReactNative/Modules/AlertModule.cpp +2 -1
  251. package/Microsoft.ReactNative/Modules/AppStateModule.cpp +2 -17
  252. package/Microsoft.ReactNative/Modules/AppStateModule.h +0 -4
  253. package/Microsoft.ReactNative/Modules/AppThemeModuleUwp.cpp +0 -2
  254. package/Microsoft.ReactNative/Modules/AppearanceModule.cpp +16 -15
  255. package/Microsoft.ReactNative/Modules/AppearanceModule.h +5 -1
  256. package/Microsoft.ReactNative/Modules/DeviceInfoModule.cpp +2 -7
  257. package/Microsoft.ReactNative/Modules/DeviceInfoModule.h +0 -1
  258. package/Microsoft.ReactNative/Modules/I18nManagerModule.h +4 -4
  259. package/Microsoft.ReactNative/QuirkSettings.cpp +0 -133
  260. package/Microsoft.ReactNative/QuirkSettings.h +0 -43
  261. package/Microsoft.ReactNative/QuirkSettings.idl +0 -41
  262. package/Microsoft.ReactNative/ReactHost/CrashManager.cpp +0 -4
  263. package/Microsoft.ReactNative/ReactHost/MsoReactContext.cpp +0 -2
  264. package/Microsoft.ReactNative/ReactHost/React.h +0 -5
  265. package/Microsoft.ReactNative/ReactHost/ReactHost.cpp +0 -1
  266. package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp +3 -26
  267. package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.h +0 -5
  268. package/Microsoft.ReactNative/ReactHost/React_Win.h +0 -2
  269. package/Microsoft.ReactNative/ReactNativeAppBuilder.idl +3 -0
  270. package/Microsoft.ReactNative/ReactPackageBuilder.cpp +7 -0
  271. package/Microsoft.ReactNative/ReactPackageBuilder.h +1 -0
  272. package/Microsoft.ReactNative/RedBox.cpp +0 -9
  273. package/Microsoft.ReactNative/RedBoxHandler.cpp +0 -4
  274. package/Microsoft.ReactNative/TurboModulesProvider.cpp +5 -10
  275. package/Microsoft.ReactNative/TurboModulesProvider.h +2 -0
  276. package/Microsoft.ReactNative/Utils/LocalBundleReader.cpp +33 -25
  277. package/Microsoft.ReactNative/Utils/LocalBundleReader.h +5 -3
  278. package/Microsoft.ReactNative/Utils/ValueUtils.cpp +1 -1
  279. package/Microsoft.ReactNative.Cxx/CppWinRTIncludes.h +0 -7
  280. package/Microsoft.ReactNative.Cxx/DesktopWindowBridge.h +0 -22
  281. package/Microsoft.ReactNative.Cxx/ModuleRegistration.cpp +8 -2
  282. package/Microsoft.ReactNative.Cxx/ModuleRegistration.h +36 -20
  283. package/Microsoft.ReactNative.Cxx/NamespaceRedirect.h +0 -3
  284. package/Microsoft.ReactNative.Cxx/NativeModules.h +8 -2
  285. package/Microsoft.ReactNative.Cxx/XamlUtils.h +0 -9
  286. package/Microsoft.ReactNative.Managed/Microsoft.ReactNative.Managed.csproj +3 -3
  287. package/Microsoft.ReactNative.Managed/ReactContext.cs +0 -9
  288. package/Microsoft.ReactNative.Managed.CodeGen/Microsoft.ReactNative.Managed.CodeGen.csproj +10 -10
  289. package/Microsoft.ReactNative.Managed.CodeGen/Properties/PublishProfiles/DeployAsTool-Debug.pubxml +1 -1
  290. package/Microsoft.ReactNative.Managed.CodeGen/Properties/PublishProfiles/DeployAsTool-Release.pubxml +1 -1
  291. package/PropertySheets/Autolink.props +1 -1
  292. package/PropertySheets/Bundle.props +1 -1
  293. package/PropertySheets/Codegen.props +1 -1
  294. package/PropertySheets/Generated/PackageVersion.g.props +3 -3
  295. package/PropertySheets/JSEngine.props +1 -1
  296. package/PropertySheets/NuGet.LockFile.props +8 -0
  297. package/PropertySheets/React.Cpp.props +8 -7
  298. package/PropertySheets/ReactNativeArchitecture.props +6 -24
  299. package/PropertySheets/WinUI.props +2 -2
  300. package/ReactCommon/ReactCommon.vcxproj +5 -6
  301. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsi/jsi/test/testlib.cpp +1 -1
  302. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsinspector-modern/NetworkIOAgent.cpp +15 -4
  303. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsinspector-modern/NetworkIOAgent.h +13 -0
  304. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/defaults/DefaultTurboModules.cpp +69 -0
  305. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/animationbackend/AnimatedPropSerializer.cpp +717 -0
  306. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/text/ParagraphShadowNode.cpp +10 -10
  307. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/AccessibilityPrimitives.h +2 -21
  308. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/TouchEventEmitter.cpp +151 -0
  309. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/TouchEventEmitter.h +50 -0
  310. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/mapbuffer/MapBuffer.cpp +178 -0
  311. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/uimanager/UIManager.cpp +69 -39
  312. package/Scripts/JustMyXaml.ps1 +8 -8
  313. package/Scripts/OfficeReact.Win32.nuspec +0 -1
  314. package/Scripts/Tfs/Layout-Desktop-Headers.ps1 +4 -39
  315. package/Scripts/UnitTest.ps1 +3 -1
  316. package/Scripts/creaternwapp.cmd +1 -9
  317. package/Scripts/creaternwlib.cmd +4 -24
  318. package/Scripts/rnw-dependencies.ps1 +15 -16
  319. package/Shared/DevSettings.h +0 -3
  320. package/Shared/DevSupportManager.cpp +55 -48
  321. package/Shared/DevSupportManager.h +0 -1
  322. package/Shared/Modules/IWebSocketModuleContentHandler.h +15 -0
  323. package/Shared/Modules/WebSocketModule.cpp +8 -3
  324. package/Shared/Networking/DefaultBlobResource.cpp +37 -0
  325. package/Shared/Networking/DefaultBlobResource.h +12 -0
  326. package/Shared/Networking/WinRTWebSocketResource.cpp +23 -408
  327. package/Shared/Networking/WinRTWebSocketResource.h +8 -120
  328. package/Shared/Shared.vcxitems +63 -38
  329. package/Shared/Shared.vcxitems.filters +54 -0
  330. package/Shared/Utils.cpp +0 -88
  331. package/Shared/Utils.h +0 -14
  332. package/Shared/WebSocketResourceFactory.cpp +28 -0
  333. package/Shared/tracing/fbsystrace.h +1 -0
  334. package/codegen/NativeReactNativeFeatureFlagsSpec.g.h +172 -160
  335. package/codegen/react/components/rnwcore/ModalHostView.g.h +8 -0
  336. package/codegen/react/components/rnwcore/Props.cpp +17 -7
  337. package/codegen/react/components/rnwcore/Props.h +3 -1
  338. package/codegen/react/components/rnwcore/VirtualViewExperimental.g.h +4 -4
  339. package/codegen/rnwcoreJSI.h +93 -77
  340. package/index.js +9 -0
  341. package/index.windows.js +9 -0
  342. package/jest/RefreshControlMock.js +2 -2
  343. package/jest/assetFileTransformer.js +8 -3
  344. package/jest/mockComponent.js +3 -4
  345. package/jest/mockNativeComponent.js +1 -1
  346. package/jest/mocks/ActivityIndicator.js +2 -2
  347. package/jest/mocks/Image.js +2 -2
  348. package/jest/mocks/Modal.js +2 -2
  349. package/jest/mocks/RefreshControl.js +3 -3
  350. package/jest/mocks/RendererProxy.js +2 -2
  351. package/jest/mocks/ScrollView.js +5 -5
  352. package/jest/mocks/Text.js +2 -2
  353. package/jest/mocks/TextInput.js +2 -2
  354. package/jest/mocks/View.js +2 -2
  355. package/jest/mocks/ViewNativeComponent.js +1 -1
  356. package/jest/mocks/requireNativeComponent.js +1 -1
  357. package/jest/mocks/useColorScheme.js +1 -1
  358. package/jest/setup.js +68 -31
  359. package/just-task.js +13 -2
  360. package/package.json +25 -20
  361. package/src/private/animated/NativeAnimatedHelper.js +2 -2
  362. package/src/private/animated/createAnimatedPropsMemoHook.js +19 -19
  363. package/src/private/components/virtualview/VirtualView.js +14 -21
  364. package/src/private/components/virtualview/VirtualViewExperimentalNativeComponent.js +11 -10
  365. package/src/private/components/virtualview/VirtualViewNativeComponent.js +4 -4
  366. package/src/private/devsupport/devmenu/elementinspector/BorderBox.js +2 -2
  367. package/src/private/devsupport/devmenu/elementinspector/BoxInspector.js +3 -3
  368. package/src/private/devsupport/devmenu/elementinspector/ElementBox.js +4 -4
  369. package/src/private/devsupport/devmenu/elementinspector/ElementProperties.js +1 -1
  370. package/src/private/devsupport/devmenu/elementinspector/Inspector.js +39 -17
  371. package/src/private/devsupport/devmenu/elementinspector/InspectorOverlay.js +15 -3
  372. package/src/private/devsupport/devmenu/elementinspector/InspectorPanel.js +2 -2
  373. package/src/private/devsupport/devmenu/elementinspector/ReactDevToolsOverlay.js +2 -8
  374. package/src/private/devsupport/devmenu/elementinspector/StyleInspector.js +1 -1
  375. package/src/private/devsupport/devmenu/elementinspector/resolveBoxStyle.js +1 -1
  376. package/src/private/devsupport/devmenu/elementinspector/useExternalInspection.js +102 -0
  377. package/src/private/devsupport/rndevtools/FuseboxSessionObserver.js +12 -25
  378. package/src/private/devsupport/rndevtools/GlobalStateObserver.js +56 -0
  379. package/src/private/devsupport/rndevtools/TracingStateObserver.js +28 -0
  380. package/src/private/featureflags/ReactNativeFeatureFlags.js +71 -79
  381. package/src/private/featureflags/ReactNativeFeatureFlagsBase.js +2 -2
  382. package/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js +14 -12
  383. package/src/private/setup/setUpDefaultReactNativeEnvironment.js +6 -0
  384. package/src/private/specs_DEPRECATED/components/ActivityIndicatorViewNativeComponent.js +1 -1
  385. package/src/private/specs_DEPRECATED/components/AndroidDrawerLayoutNativeComponent.js +3 -3
  386. package/src/private/specs_DEPRECATED/components/AndroidHorizontalScrollContentViewNativeComponent.js +1 -1
  387. package/src/private/specs_DEPRECATED/components/AndroidSwipeRefreshLayoutNativeComponent.js +2 -2
  388. package/src/private/specs_DEPRECATED/components/AndroidSwitchNativeComponent.js +2 -2
  389. package/src/private/specs_DEPRECATED/components/DebuggingOverlayNativeComponent.js +3 -3
  390. package/src/private/specs_DEPRECATED/components/ProgressBarAndroidNativeComponent.js +1 -1
  391. package/src/private/specs_DEPRECATED/components/PullToRefreshViewNativeComponent.js +1 -1
  392. package/src/private/specs_DEPRECATED/components/RCTInputAccessoryViewNativeComponent.js +1 -1
  393. package/src/private/specs_DEPRECATED/components/RCTModalHostViewNativeComponent.js +7 -3
  394. package/src/private/specs_DEPRECATED/components/RCTSafeAreaViewNativeComponent.js +1 -1
  395. package/src/private/specs_DEPRECATED/components/SwitchNativeComponent.js +2 -2
  396. package/src/private/specs_DEPRECATED/components/UnimplementedNativeViewNativeComponent.js +1 -1
  397. package/src/private/specs_DEPRECATED/modules/NativeImageLoaderIOS.js +1 -1
  398. package/src/private/styles/composeStyles.js +1 -1
  399. package/src/private/utilities/toError.js +27 -0
  400. package/src/private/utilities/toExtendedError.js +30 -0
  401. package/src/private/webapis/dom/events/Event.js +1 -1
  402. package/src/private/webapis/dom/events/EventHandlerAttributes.js +2 -2
  403. package/src/private/webapis/dom/events/EventTarget.js +3 -3
  404. package/src/private/webapis/dom/events/internals/EventInternals.js +2 -2
  405. package/src/private/webapis/dom/nodes/ReadOnlyElement.js +1 -1
  406. package/src/private/webapis/dom/nodes/ReadOnlyNode.js +2 -2
  407. package/src/private/webapis/dom/nodes/specs/NativeDOM.js +14 -14
  408. package/src/private/webapis/dom/oldstylecollections/HTMLCollection.js +2 -2
  409. package/src/private/webapis/dom/oldstylecollections/NodeList.js +2 -2
  410. package/src/private/webapis/geometry/DOMRectList.js +2 -2
  411. package/src/private/webapis/intersectionobserver/IntersectionObserver.js +8 -8
  412. package/src/private/webapis/intersectionobserver/specs/NativeIntersectionObserver.js +6 -6
  413. package/src/private/webapis/mutationobserver/MutationObserver.js +2 -2
  414. package/src/private/webapis/mutationobserver/MutationRecord.js +2 -2
  415. package/src/private/webapis/mutationobserver/internals/MutationObserverManager.js +1 -1
  416. package/src/private/webapis/mutationobserver/specs/NativeMutationObserver.js +3 -3
  417. package/src/private/webapis/performance/LongTasks.js +3 -3
  418. package/src/private/webapis/performance/Performance.js +4 -4
  419. package/src/private/webapis/performance/PerformanceEntry.js +1 -1
  420. package/src/private/webapis/performance/PerformanceObserver.js +2 -2
  421. package/src/private/webapis/performance/specs/NativePerformance.js +8 -8
  422. package/src/private/webapis/structuredClone/structuredClone.js +2 -2
  423. package/src/types/globals.d.ts +4 -4
  424. package/template/cpp-lib/proj/MyLib.sln +0 -33
  425. package/template/cpp-lib/proj/MyLib.vcxproj +1 -1
  426. package/template/cs-lib/proj/MyLib.sln +0 -33
  427. package/template/metro.config.js +12 -2
  428. package/templates/cpp-app/metro.config.js +12 -2
  429. package/templates/cpp-app/windows/MyApp/MyApp.cpp +9 -6
  430. package/templates/cpp-app/windows/MyApp/MyApp.vcxproj +2 -2
  431. package/templates/cpp-app/windows/MyApp.sln +0 -33
  432. package/templates/cpp-lib/example/metro.config.js +12 -2
  433. package/templates/cpp-lib/windows/MyLib/MyLib.vcxproj +2 -2
  434. package/templates/cpp-lib/windows/MyLib.sln +0 -33
  435. package/types/index.d.ts +2 -0
  436. package/Folly/.clang-format +0 -2
  437. package/Folly/Folly.natvis +0 -25
  438. package/Folly/Folly.vcxproj +0 -378
  439. package/Folly/Folly.vcxproj.filters +0 -558
  440. package/Folly/TEMP_UntilFollyUpdate/ConstexprMath.h +0 -978
  441. package/Folly/TEMP_UntilFollyUpdate/Conv.cpp +0 -1205
  442. package/Folly/TEMP_UntilFollyUpdate/chrono/Hardware.h +0 -155
  443. package/Folly/TEMP_UntilFollyUpdate/concurrency/CacheLocality.cpp +0 -633
  444. package/Folly/TEMP_UntilFollyUpdate/json/dynamic-inl.h +0 -1438
  445. package/Folly/TEMP_UntilFollyUpdate/json/json.cpp +0 -1118
  446. package/Folly/TEMP_UntilFollyUpdate/lang/SafeAssert.h +0 -188
  447. package/Folly/TEMP_UntilFollyUpdate/lang/ToAscii.cpp +0 -348
  448. package/Folly/TEMP_UntilFollyUpdate/lang/ToAscii.h +0 -182
  449. package/Folly/TEMP_UntilFollyUpdate/portability/Unistd.cpp +0 -33
  450. package/Folly/cgmanifest.json +0 -25
  451. package/Libraries/Components/View/ReactNativeViewAttributes.js +0 -60
  452. package/Libraries/Components/View/ReactNativeViewAttributes.windows.js +0 -64
  453. package/Libraries/Image/TextInlineImageNativeComponent.js +0 -49
  454. package/Microsoft.ReactNative/Fabric/Composition/CompositionContextHelper_emptyimpl.cpp +0 -75
  455. package/Microsoft.ReactNative/Fabric/Composition/CompositionUIService_emptyimpl.cpp +0 -27
  456. package/Microsoft.ReactNative/Fabric/Composition/Theme_emptyimpl.cpp +0 -81
  457. package/Microsoft.ReactNative/Modules/CreateModules.cpp +0 -24
  458. package/Microsoft.ReactNative/ReactSupport.cpp +0 -221
  459. package/Microsoft.ReactNative/ReactSupport.h +0 -13
  460. package/Microsoft.ReactNative/Utils/UwpPreparedScriptStore.cpp +0 -107
  461. package/Microsoft.ReactNative/Utils/UwpPreparedScriptStore.h +0 -62
  462. package/Microsoft.ReactNative/Utils/UwpScriptStore.cpp +0 -56
  463. package/Microsoft.ReactNative/Utils/UwpScriptStore.h +0 -22
  464. package/Microsoft.ReactNative/XamlUIService.cpp +0 -68
  465. package/Microsoft.ReactNative/XamlUIService.h +0 -37
  466. package/Microsoft.ReactNative/XamlUIService.idl +0 -53
  467. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/CxxNativeModule.cpp +0 -259
  468. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/Instance.cpp +0 -381
  469. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSIndexedRAMBundle.cpp +0 -145
  470. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/MethodCall.cpp +0 -100
  471. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/ModuleRegistry.cpp +0 -256
  472. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/NativeToJsBridge.cpp +0 -363
  473. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/RAMBundleRegistry.cpp +0 -93
  474. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/ReactMarker.cpp +0 -148
  475. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/TraceSection.h +0 -190
  476. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsiexecutor/jsireact/JSINativeModules.cpp +0 -123
  477. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.cpp +0 -27
  478. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/webperformance/NativePerformance.cpp +0 -414
  479. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/text/platform/android/react/renderer/components/text/ParagraphState.h +0 -91
  480. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/core/EventDispatcher.cpp +0 -80
  481. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/core/EventQueueProcessor.cpp +0 -138
  482. package/fmt/.clang-format +0 -2
  483. package/fmt/cgmanifest.json +0 -15
  484. package/fmt/fmt.vcxproj +0 -153
  485. package/fmt/fmt.vcxproj.filters +0 -59
  486. package/fmt/pch.cpp +0 -4
  487. package/fmt/pch.h +0 -2
@@ -1,978 +0,0 @@
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
- #include <folly/lang/CheckedMath.h>
28
- #include <folly/portability/Constexpr.h>
29
-
30
- namespace folly {
31
-
32
- /// numbers
33
- ///
34
- /// mimic: std::numbers, C++20 (partial)
35
- namespace numbers {
36
-
37
- namespace detail {
38
- template <typename T>
39
- using enable_if_floating_t =
40
- std::enable_if_t<std::is_floating_point<T>::value, T>;
41
- }
42
-
43
- /// e_v
44
- ///
45
- /// mimic: std::numbers::e_v, C++20
46
- template <typename T>
47
- inline constexpr T e_v = detail::enable_if_floating_t<T>(
48
- 2.71828182845904523536028747135266249775724709369995L);
49
-
50
- /// ln2_v
51
- ///
52
- /// mimic: std::numbers::ln2_v, C++20
53
- template <typename T>
54
- inline constexpr T ln2_v = detail::enable_if_floating_t<T>(
55
- 0.69314718055994530941723212145817656807550013436025L);
56
-
57
- /// e
58
- ///
59
- /// mimic: std::numbers::e, C++20
60
- inline constexpr double e = e_v<double>;
61
-
62
- /// ln2
63
- ///
64
- /// mimic: std::numbers::ln2, C++20
65
- inline constexpr double ln2 = ln2_v<double>;
66
-
67
- } // namespace numbers
68
-
69
- /// floating_point_integral_constant
70
- ///
71
- /// Like std::integral_constant but for floating-point types holding integral
72
- /// values representable in an integral type.
73
- template <typename T, typename S, S Value>
74
- struct floating_point_integral_constant {
75
- using value_type = T;
76
- static constexpr value_type value = static_cast<value_type>(Value);
77
- constexpr operator value_type() const noexcept { return value; }
78
- constexpr value_type operator()() const noexcept { return value; }
79
- };
80
-
81
- // ----
82
-
83
- namespace detail {
84
-
85
- template <typename T>
86
- constexpr size_t constexpr_iterated_squares_desc_size_(T const base) {
87
- using lim = std::numeric_limits<T>;
88
- size_t s = 1;
89
- auto r = base;
90
- while (r <= lim::max() / r) {
91
- ++s;
92
- r *= r;
93
- }
94
- return s;
95
- }
96
-
97
- } // namespace detail
98
-
99
- /// constexpr_iterated_squares_desc_size_v
100
- ///
101
- /// Effectively calculates: floor(log(max_exponent)/log(base))
102
- ///
103
- /// For use with constexpr_iterated_squares_desc below.
104
- template <typename Base>
105
- inline constexpr size_t constexpr_iterated_squares_desc_size_v =
106
- detail::constexpr_iterated_squares_desc_size_(Base::value);
107
-
108
- /// constexpr_iterated_squares_desc
109
- ///
110
- /// A constexpr scaling array of integer powers-of-powers-of-two, descending,
111
- /// with the associated powers-of-two.
112
- ///
113
- /// scaling = [..., {8, b^8}, {4, b^4}, {2, b^2}, {1, b^1}] for b = base
114
- ///
115
- /// Includes select constexpr scaling algorithms based on the scaling array.
116
- ///
117
- /// The scaling array and the scaling algorithms are general-purpose, if niche.
118
- /// They may be used by other constexpr math functions (floating-point) either
119
- /// to improve runtime performance or to improve numerical approximations.
120
- ///
121
- /// Some compilers fail to support passing some types as non-type template
122
- /// params. In particular, long double is not universally supported. Therefore,
123
- /// this utility takes its base as a type rather than as a value. For floating-
124
- /// point integral bases, that is, bases of floating-point type but of integral
125
- /// value, floating_point_integral_constant is the easiest parameterization.
126
- template <typename T, std::size_t Size>
127
- struct constexpr_iterated_squares_desc {
128
- static_assert(Size > 0, "requires non-zero size");
129
-
130
- using size_type = decltype(Size);
131
- using base_type = T;
132
-
133
- struct item_type {
134
- size_type power;
135
- base_type scale;
136
- };
137
-
138
- static constexpr size_type size = Size;
139
- base_type base;
140
- item_type scaling[size];
141
-
142
- private:
143
- using lim = std::numeric_limits<base_type>;
144
-
145
- static_assert(
146
- lim::max_exponent < std::numeric_limits<size_type>::max(),
147
- "size_type too small for base_type");
148
-
149
- public:
150
- explicit constexpr constexpr_iterated_squares_desc(base_type r) noexcept
151
- : base{r}, scaling{} {
152
- assert(size <= detail::constexpr_iterated_squares_desc_size_(base));
153
- size_type i = 0;
154
- size_type p = 1;
155
- while (true) { // a for-loop might cause multiplication overflow below
156
- scaling[size - 1 - i] = {p, r};
157
- if (++i == size) {
158
- break;
159
- }
160
- p *= 2;
161
- r *= r;
162
- }
163
- }
164
-
165
- /// shrink
166
- ///
167
- /// Returns scaling params of the form:
168
- /// item_type{power, scale} with scale = base ^ power
169
- /// With power the smallest nonnegative integer such that:
170
- /// abs(num) / scale <= max
171
- constexpr item_type shrink(base_type const num, base_type const max) const {
172
- assert(max > base_type(0));
173
- auto const rmax = max / base;
174
- auto const snum = num < base_type(0) ? -num : num;
175
- auto power = size_type(0);
176
- auto scale = base_type(1);
177
- if (!(snum / scale <= max)) {
178
- for (auto const& i : scaling) {
179
- auto const next = scale * i.scale;
180
- auto const div = snum / next;
181
- if (div <= rmax) {
182
- continue;
183
- }
184
- power += i.power;
185
- scale = next;
186
- if (div <= max) {
187
- break;
188
- }
189
- }
190
- }
191
- assert(snum / scale <= max);
192
- return {power, scale};
193
- }
194
-
195
- /// growth
196
- ///
197
- /// Returns scaling params of the form:
198
- /// item_type{power, scale} with scale = base ^ power
199
- /// With power the smallest nonnegative integer such that:
200
- /// abs(num) * scale >= min
201
- constexpr item_type growth(base_type const num, base_type const min) const {
202
- assert(min > base_type(0));
203
- auto const rmin = min * base;
204
- auto const snum = num < base_type(0) ? -num : num;
205
- auto power = size_type(0);
206
- auto scale = base_type(1);
207
- if (!(snum * scale >= min)) {
208
- for (auto const& i : scaling) {
209
- auto const next = scale * i.scale;
210
- auto const mul = snum * next;
211
- if (mul >= rmin) {
212
- continue;
213
- }
214
- power += i.power;
215
- scale = next;
216
- if (mul >= min) {
217
- break;
218
- }
219
- }
220
- }
221
- assert(snum * scale >= min);
222
- return {power, scale};
223
- }
224
- };
225
-
226
- /// constexpr_iterated_squares_desc_v
227
- ///
228
- /// An instance of constexpr_iterated_squares_desc of max size with the given
229
- /// base.
230
- template <typename Base>
231
- inline constexpr auto constexpr_iterated_squares_desc_v =
232
- constexpr_iterated_squares_desc<
233
- typename Base::value_type,
234
- constexpr_iterated_squares_desc_size_v<Base>>{Base::value};
235
-
236
- /// constexpr_iterated_squares_desc_2_v
237
- ///
238
- /// An alias for constexpr_iterated_squares_desc_v with base 2, which is the
239
- /// most common base to use with iterated-squares.
240
- template <typename T>
241
- constexpr auto& constexpr_iterated_squares_desc_2_v =
242
- constexpr_iterated_squares_desc_v<
243
- floating_point_integral_constant<T, int, 2>>;
244
-
245
- // TLDR: Prefer using operator< for ordering. And when
246
- // a and b are equivalent objects, we return b to make
247
- // sorting stable.
248
- // See http://stepanovpapers.com/notes.pdf for details.
249
- template <typename T, typename... Ts>
250
- constexpr T constexpr_max(T a, Ts... ts) {
251
- T list[] = {ts..., a}; // 0-length arrays are illegal
252
- // [Windows #12703 - Fix folly CodeQL issues]
253
- for (size_t i = 0; i < sizeof...(Ts); ++i) {
254
- a = list[i] < a ? a : list[i];
255
- }
256
- return a;
257
- }
258
-
259
- // When a and b are equivalent objects, we return a to
260
- // make sorting stable.
261
- template <typename T, typename... Ts>
262
- constexpr T constexpr_min(T a, Ts... ts) {
263
- T list[] = {ts..., a}; // 0-length arrays are illegal
264
- // [Windows #12703 - Fix folly CodeQL issues]
265
- for (size_t i = 0; i < sizeof...(Ts); ++i) {
266
- a = list[i] < a ? list[i] : a;
267
- }
268
- return a;
269
- }
270
-
271
- template <typename T, typename Less>
272
- constexpr T const& constexpr_clamp(
273
- T const& v, T const& lo, T const& hi, Less less) {
274
- T const& a = less(v, lo) ? lo : v;
275
- T const& b = less(hi, a) ? hi : a;
276
- return b;
277
- }
278
- template <typename T>
279
- constexpr T const& constexpr_clamp(T const& v, T const& lo, T const& hi) {
280
- return constexpr_clamp(v, lo, hi, std::less<T>{});
281
- }
282
-
283
- template <typename T>
284
- constexpr bool constexpr_isnan(T const t) {
285
- return t != t; // NOLINT
286
- }
287
-
288
- namespace detail {
289
-
290
- template <typename T, typename = void>
291
- struct constexpr_abs_helper {};
292
-
293
- template <typename T>
294
- struct constexpr_abs_helper<
295
- T,
296
- typename std::enable_if<std::is_floating_point<T>::value>::type> {
297
- static constexpr T go(T t) { return t < static_cast<T>(0) ? -t : t; }
298
- };
299
-
300
- template <typename T>
301
- struct constexpr_abs_helper<
302
- T,
303
- typename std::enable_if<
304
- std::is_integral<T>::value && !std::is_same<T, bool>::value &&
305
- std::is_unsigned<T>::value>::type> {
306
- static constexpr T go(T t) { return t; }
307
- };
308
-
309
- template <typename T>
310
- struct constexpr_abs_helper<
311
- T,
312
- typename std::enable_if<
313
- std::is_integral<T>::value && !std::is_same<T, bool>::value &&
314
- std::is_signed<T>::value>::type> {
315
- static constexpr typename std::make_unsigned<T>::type go(T t) {
316
- return typename std::make_unsigned<T>::type(t < static_cast<T>(0) ? -t : t);
317
- }
318
- };
319
-
320
- } // namespace detail
321
-
322
- template <typename T>
323
- constexpr auto constexpr_abs(T t)
324
- -> decltype(detail::constexpr_abs_helper<T>::go(t)) {
325
- return detail::constexpr_abs_helper<T>::go(t);
326
- }
327
-
328
- namespace detail {
329
-
330
- template <typename T>
331
- constexpr T constexpr_log2_(T a, T e) {
332
- return e == T(1) ? a : constexpr_log2_(a + T(1), e / T(2));
333
- }
334
-
335
- template <typename T>
336
- constexpr T constexpr_log2_ceil_(T l2, T t) {
337
- return l2 + T(T(1) << l2 < t ? 1 : 0);
338
- }
339
-
340
- } // namespace detail
341
-
342
- template <typename T>
343
- constexpr T constexpr_log2(T t) {
344
- return detail::constexpr_log2_(T(0), t);
345
- }
346
-
347
- template <typename T>
348
- constexpr T constexpr_log2_ceil(T t) {
349
- return detail::constexpr_log2_ceil_(constexpr_log2(t), t);
350
- }
351
-
352
- /// constexpr_trunc
353
- ///
354
- /// mimic: std::trunc (C++23)
355
- template <
356
- typename T,
357
- std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
358
- constexpr T constexpr_trunc(T const t) {
359
- using lim = std::numeric_limits<T>;
360
- using int_type = std::uintmax_t;
361
- using int_lim = std::numeric_limits<int_type>;
362
- static_assert(lim::radix == 2, "non-binary radix");
363
- static_assert(lim::digits <= int_lim::digits, "overwide mantissa");
364
- constexpr auto bound = static_cast<T>(std::uintmax_t(1) << (lim::digits - 1));
365
- auto const neg = !constexpr_isnan(t) && t < T(0);
366
- auto const s = neg ? -t : t;
367
- if (constexpr_isnan(t) || t == T(0) || !(s < bound)) {
368
- return t;
369
- }
370
- if (s < T(1)) {
371
- return neg ? -T(0) : T(0);
372
- }
373
- auto const r = static_cast<T>(static_cast<int_type>(s));
374
- return neg ? -r : r;
375
- }
376
-
377
- template <typename T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
378
- constexpr T constexpr_trunc(T const t) {
379
- return t;
380
- }
381
-
382
- /// constexpr_round
383
- ///
384
- /// mimic: std::round (C++23)
385
- template <typename T>
386
- constexpr T constexpr_round(T const t) {
387
- constexpr auto half = T(1) / T(2);
388
- auto const same = constexpr_isnan(t) || t == T(0);
389
- return same ? t : constexpr_trunc(t < T(0) ? t - half : t + half);
390
- }
391
-
392
- /// constexpr_floor
393
- ///
394
- /// mimic: std::floor (C++23)
395
- template <typename T>
396
- constexpr T constexpr_floor(T const t) {
397
- auto const s = constexpr_trunc(t);
398
- return t < s ? s - T(1) : s;
399
- }
400
-
401
- /// constexpr_ceil
402
- ///
403
- /// mimic: std::ceil (C++23)
404
- template <typename T>
405
- constexpr T constexpr_ceil(T const t) {
406
- auto const s = constexpr_trunc(t);
407
- return s < t ? s + T(1) : s;
408
- }
409
-
410
- /// constexpr_ceil
411
- ///
412
- /// The least integer at least t that round divides.
413
- template <typename T>
414
- constexpr T constexpr_ceil(T t, T round) {
415
- return round == T(0)
416
- ? t
417
- : ((t + (t <= T(0) ? T(0) : round - T(1))) / round) * round;
418
- }
419
-
420
- /// constexpr_mult
421
- ///
422
- /// Multiply two values, allowing for constexpr floating-pooint overflow to
423
- /// infinity.
424
- template <typename T>
425
- constexpr T constexpr_mult(T const a, T const b) {
426
- using lim = std::numeric_limits<T>;
427
- if (constexpr_isnan(a) || constexpr_isnan(b)) {
428
- return constexpr_isnan(a) ? a : b;
429
- }
430
- if (std::is_floating_point<T>::value) {
431
- constexpr auto inf = lim::infinity();
432
- auto const ax = constexpr_abs(a);
433
- auto const bx = constexpr_abs(b);
434
- if ((ax == T(0) && bx == inf) || (bx == T(0) && ax == inf)) {
435
- return lim::quiet_NaN();
436
- }
437
- // floating-point multiplication overflow, ie where multiplication of two
438
- // finite values overflows to infinity of either sign, is not constexpr per
439
- // gcc
440
- // floating-point division overflow, ie where division of two finite values
441
- // overflows to infinity of either sign, is not constexpr per gcc
442
- // floating-point division by zero is not constexpr per any compiler, but we
443
- // use it in the checks for the other two conditions
444
- if (ax != inf && bx != inf && T(1) < bx && lim::max() / bx < ax) {
445
- auto const a_neg = static_cast<bool>(a < T(0));
446
- auto const b_neg = static_cast<bool>(b < T(0));
447
- auto const sign = a_neg == b_neg ? T(1) : T(-1);
448
- return sign * inf;
449
- }
450
- }
451
- return a * b;
452
- }
453
-
454
- namespace detail {
455
-
456
- template <
457
- typename T,
458
- typename E,
459
- std::enable_if_t<std::is_signed<E>::value, int> = 1>
460
- constexpr T constexpr_ipow(T const base, E const exp) {
461
- if (std::is_floating_point<T>::value) {
462
- if (exp < E(0)) {
463
- return T(1) / constexpr_ipow(base, -exp);
464
- }
465
- if (exp == E(0)) {
466
- return T(1);
467
- }
468
- if (constexpr_isnan(base)) {
469
- return base;
470
- }
471
- }
472
- assert(!(exp < E(0)) && "negative exponent with integral base");
473
- if (exp == E(0)) {
474
- return T(1);
475
- }
476
- if (exp == E(1)) {
477
- return base;
478
- }
479
- auto const hexp = constexpr_trunc(exp / E(2));
480
- auto const div = constexpr_ipow(base, hexp);
481
- auto const rem = hexp * E(2) == exp ? T(1) : base;
482
- return constexpr_mult(constexpr_mult(div, div), rem);
483
- }
484
-
485
- template <
486
- typename T,
487
- typename E,
488
- std::enable_if_t<std::is_unsigned<E>::value, int> = 1>
489
- constexpr T constexpr_ipow(T const base, E const exp) {
490
- if (std::is_floating_point<T>::value) {
491
- if (exp == E(0)) {
492
- return T(1);
493
- }
494
- if (constexpr_isnan(base)) {
495
- return base;
496
- }
497
- }
498
- if (exp == E(0)) {
499
- return T(1);
500
- }
501
- if (exp == E(1)) {
502
- return base;
503
- }
504
- auto const hexp = constexpr_trunc(exp / E(2));
505
- auto const div = constexpr_ipow(base, hexp);
506
- auto const rem = hexp * E(2) == exp ? T(1) : base;
507
- return constexpr_mult(constexpr_mult(div, div), rem);
508
- }
509
-
510
- } // namespace detail
511
-
512
- /// constexpr_exp
513
- ///
514
- /// Calculates an approximation of the mathematical function exp(num). Usable in
515
- /// constant evaluations. Like std::exp, which becomes constexpr in C++26.
516
- ///
517
- /// The integer overload uses iterated squaring and multiplication. The
518
- /// floating-point overlaod naively evaluates the taylor series of exp(num)
519
- /// until approximate convergence.
520
- ///
521
- /// mimic: std::exp (C++23, C++26)
522
- template <
523
- typename T,
524
- typename N,
525
- std::enable_if_t<
526
- std::is_floating_point<T>::value && std::is_integral<N>::value &&
527
- !std::is_same<N, bool>::value,
528
- int> = 0>
529
- constexpr T constexpr_exp(N const power) {
530
- auto const npower = constexpr_abs(power);
531
- auto const result = detail::constexpr_ipow(numbers::e_v<T>, npower);
532
- return power < N(0) ? T(1) / result : result;
533
- }
534
- template <
535
- typename N,
536
- std::enable_if_t<
537
- std::is_integral<N>::value && !std::is_same<N, bool>::value,
538
- int> = 0>
539
- constexpr double constexpr_exp(N const power) {
540
- return constexpr_exp<double>(power);
541
- }
542
- template <
543
- typename T,
544
- std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
545
- constexpr T constexpr_exp(T const power) {
546
- using lim = std::numeric_limits<T>;
547
-
548
- // edge cases
549
- if (constexpr_isnan(power)) {
550
- return power;
551
- }
552
- if (power == -lim::infinity()) {
553
- return +T(0);
554
- }
555
- if (power == +lim::infinity()) {
556
- return power;
557
- }
558
-
559
- // convergence works better with positive powers since signs do not alternate
560
- auto const abspower = constexpr_abs(power);
561
- // convergence must short-circuit when terms grow to floating-point infinity
562
- auto const bound = T(1) < abspower ? lim::max() / abspower : lim::infinity();
563
-
564
- // term #index = power * coeff
565
- auto index = size_t(0);
566
- auto term = T(1);
567
- // result = sum of terms
568
- auto result = T(1);
569
- // sum the terms until ~convergence
570
- while (!(constexpr_abs(term) < lim::epsilon())) {
571
- if (bound < term) {
572
- return power < T(0) ? T(0) : lim::infinity();
573
- }
574
- index += 1;
575
- term = term * abspower / index;
576
- result += term;
577
- }
578
- return power < T(0) ? T(1) / result : result;
579
- }
580
-
581
- /// constexpr_log
582
- ///
583
- /// Calculates an approximation of the natural logarithm ln(num).
584
- ///
585
- /// The implementation uses a quickly-converging, high-precision iterative
586
- /// technique as described in:
587
- /// https://en.wikipedia.org/wiki/Natural_logarithm#High_precision
588
- ///
589
- /// The technique works best with numbers that are close enough to 1, so the
590
- /// implementation uses a quick shrink/growth technique as described in:
591
- /// https://en.wikipedia.org/wiki/Natural_logarithm#Efficient_computation
592
- template <
593
- typename T,
594
- std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
595
- constexpr T constexpr_log(T const num) {
596
- using lim = std::numeric_limits<T>;
597
- constexpr auto& isq = constexpr_iterated_squares_desc_2_v<T>;
598
-
599
- // edge cases
600
- if (constexpr_isnan(num)) {
601
- return num;
602
- }
603
- if (num < T(0)) {
604
- return lim::quiet_NaN();
605
- }
606
- if (num == T(0)) {
607
- return -lim::infinity();
608
- }
609
- if (num == lim::infinity()) {
610
- return num;
611
- }
612
-
613
- // compression
614
- auto const shrink = isq.shrink(num, isq.base);
615
- auto const growth = isq.growth(num, T(1));
616
- auto const scaled = num * growth.scale / shrink.scale;
617
- assert(scaled <= isq.base);
618
- assert(scaled >= T(1));
619
-
620
- auto sum = T(0);
621
- auto delta = T(2);
622
- while (constexpr_abs(delta) >= lim::epsilon()) {
623
- auto expterm = constexpr_exp(sum);
624
- delta = T(2) * (scaled - expterm) / (scaled + expterm);
625
- sum += delta;
626
- }
627
- auto const ln2 = numbers::ln2_v<T>;
628
- return sum - growth.power * ln2 + shrink.power * ln2;
629
- }
630
-
631
- /// constexpr_pow
632
- ///
633
- /// Calculates an approximation of the value of base raised to the exponent exp.
634
- ///
635
- /// The implementation uses iterated squaring and multiplication for the integer
636
- /// part of the exponent and uses the identity x^y = exp(y * log(x)) for the
637
- /// fractional part of the exponent.
638
- ///
639
- /// Notes:
640
- /// * Forbids base of +0 or -0 with finite non-positive exponent: in part since
641
- /// the plausible infinite result would be sensitive to the sign of the zero;
642
- /// and in part since std::pow would be required or permitted to raise error
643
- /// div-by-zero.
644
- /// * Forbids finite negative base with finite non-integer exponent: in part
645
- /// since std::pow would be required to raise error invalid.
646
- ///
647
- /// mimic: std::pow (C++26)
648
- template <
649
- typename T,
650
- typename E,
651
- std::enable_if_t<
652
- std::is_integral<E>::value && !std::is_same<E, bool>::value,
653
- int> = 0>
654
- constexpr T constexpr_pow(T const base, E const exp) {
655
- return detail::constexpr_ipow(base, exp);
656
- }
657
- template <
658
- typename T,
659
- std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
660
- constexpr T constexpr_pow(T const base, T const exp) {
661
- using lim = std::numeric_limits<T>;
662
-
663
- // edge cases
664
- if (exp == T(0)) {
665
- return T(1);
666
- }
667
- if (constexpr_isnan(base)) {
668
- return base;
669
- }
670
- if (exp == lim::infinity() || exp == -lim::infinity()) {
671
- auto const abase = constexpr_abs(base);
672
- if (abase < T(1)) {
673
- return exp == lim::infinity() ? T(0) : lim::infinity();
674
- }
675
- if (T(1) < abase) {
676
- return exp == lim::infinity() ? lim::infinity() : T(0);
677
- }
678
- return T(1);
679
- }
680
- if (base == T(1)) {
681
- return base;
682
- }
683
- if (constexpr_isnan(exp)) {
684
- return exp;
685
- }
686
- assert(base != T(0) || exp > T(0)); // error div-by-zero
687
- if (base == lim::infinity()) {
688
- return exp < T(0) ? T(0) : lim::infinity();
689
- }
690
- if (base == -lim::infinity()) {
691
- auto const oddi = //
692
- exp == constexpr_trunc(exp) &&
693
- exp != constexpr_trunc(exp / T(2)) * T(2);
694
- return (oddi ? -T(1) : T(1)) * (exp < T(0) ? T(0) : lim::infinity());
695
- }
696
- if (base == T(0)) {
697
- auto const oddi = //
698
- exp == constexpr_trunc(exp) &&
699
- exp != constexpr_trunc(exp / T(2)) * T(2);
700
- return oddi ? base : T(0);
701
- }
702
- if (exp < T(0)) {
703
- return T(1) / constexpr_pow(base, -exp);
704
- }
705
-
706
- // as an identity: x^y = exp(y * log(x)); but calculation is imprecise ... so,
707
- // for better precision, split the calculation into its integral-power and its
708
- // fractional-power components
709
- // as a cost, the complexity of constexpr_ipow here is logarithmic in y, i.e.,
710
- // linear in the logarithm of y, which can be prohibitive
711
- auto const exp_trunc = constexpr_trunc(exp);
712
- assert(T(0) < base || exp == exp_trunc); // error invalid
713
- auto const exp_fract = exp - exp_trunc;
714
- auto const anyi = exp_fract == T(0);
715
- return constexpr_mult(
716
- detail::constexpr_ipow(base, exp_trunc),
717
- anyi ? T(1) : constexpr_exp(exp_fract * constexpr_log(base)));
718
- }
719
-
720
- /// constexpr_find_last_set
721
- ///
722
- /// Return the 1-based index of the most significant bit which is set.
723
- /// For x > 0, constexpr_find_last_set(x) == 1 + floor(log2(x)).
724
- template <typename T>
725
- constexpr std::size_t constexpr_find_last_set(T const t) {
726
- using U = std::make_unsigned_t<T>;
727
- return t == T(0) ? 0 : 1 + constexpr_log2(static_cast<U>(t));
728
- }
729
-
730
- namespace detail {
731
- template <typename U>
732
- constexpr std::size_t constexpr_find_first_set_(
733
- std::size_t s, std::size_t a, U const u) {
734
- return s == 0 ? a
735
- : constexpr_find_first_set_(
736
- s / 2, a + s * bool((u >> a) % (U(1) << s) == U(0)), u);
737
- }
738
- } // namespace detail
739
-
740
- /// constexpr_find_first_set
741
- ///
742
- /// Return the 1-based index of the least significant bit which is set.
743
- /// For x > 0, the exponent in the largest power of two which does not divide x.
744
- template <typename T>
745
- constexpr std::size_t constexpr_find_first_set(T t) {
746
- using U = std::make_unsigned_t<T>;
747
- using size = std::integral_constant<std::size_t, sizeof(T) * 4>;
748
- return t == T(0)
749
- ? 0
750
- : 1 + detail::constexpr_find_first_set_(size{}, 0, static_cast<U>(t));
751
- }
752
-
753
- template <typename T>
754
- constexpr T constexpr_add_overflow_clamped(T a, T b) {
755
- using L = std::numeric_limits<T>;
756
- using M = std::intmax_t;
757
- static_assert(
758
- !std::is_integral<T>::value || sizeof(T) <= sizeof(M),
759
- "Integral type too large!");
760
- if (!folly::is_constant_evaluated_or(true)) {
761
- if constexpr (std::is_integral_v<T>) {
762
- T ret{};
763
- if (FOLLY_UNLIKELY(!checked_add(&ret, a, b))) {
764
- if constexpr (std::is_signed_v<T>) {
765
- // Could be either overflow or underflow for signed types.
766
- // Can only be underflow if both inputs are negative.
767
- if (a < 0 && b < 0) {
768
- return L::min();
769
- }
770
- }
771
- return L::max();
772
- }
773
- return ret;
774
- }
775
- }
776
- // clang-format off
777
- return
778
- // don't do anything special for non-integral types.
779
- !std::is_integral<T>::value ? a + b :
780
- // for narrow integral types, just convert to intmax_t.
781
- sizeof(T) < sizeof(M)
782
- ? T(constexpr_clamp(
783
- static_cast<M>(a) + static_cast<M>(b),
784
- static_cast<M>(L::min()),
785
- static_cast<M>(L::max()))) :
786
- // when a >= 0, cannot add more than `MAX - a` onto a.
787
- !(a < 0) ? a + constexpr_min(b, T(L::max() - a)) :
788
- // a < 0 && b >= 0, `a + b` will always be in valid range of type T.
789
- !(b < 0) ? a + b :
790
- // a < 0 && b < 0, keep the result >= MIN.
791
- a + constexpr_max(b, T(L::min() - a));
792
- // clang-format on
793
- }
794
-
795
- template <typename T>
796
- constexpr T constexpr_sub_overflow_clamped(T a, T b) {
797
- using L = std::numeric_limits<T>;
798
- using M = std::intmax_t;
799
- static_assert(
800
- !std::is_integral<T>::value || sizeof(T) <= sizeof(M),
801
- "Integral type too large!");
802
- // clang-format off
803
- return
804
- // don't do anything special for non-integral types.
805
- !std::is_integral<T>::value ? a - b :
806
- // for unsigned type, keep result >= 0.
807
- std::is_unsigned<T>::value ? (a < b ? 0 : a - b) :
808
- // for narrow signed integral types, just convert to intmax_t.
809
- sizeof(T) < sizeof(M)
810
- ? T(constexpr_clamp(
811
- static_cast<M>(a) - static_cast<M>(b),
812
- static_cast<M>(L::min()),
813
- static_cast<M>(L::max()))) :
814
- // (a >= 0 && b >= 0) || (a < 0 && b < 0), `a - b` will always be valid.
815
- (a < 0) == (b < 0) ? a - b :
816
- // MIN < b, so `-b` should be in valid range (-MAX <= -b <= MAX),
817
- // convert subtraction to addition.
818
- L::min() < b ? constexpr_add_overflow_clamped(a, T(-b)) :
819
- // -b = -MIN = (MAX + 1) and a <= -1, result is in valid range.
820
- a < 0 ? a - b :
821
- // -b = -MIN = (MAX + 1) and a >= 0, result > MAX.
822
- L::max();
823
- // clang-format on
824
- }
825
-
826
- // clamp_cast<> provides sane numeric conversions from float point numbers to
827
- // integral numbers, and between different types of integral numbers. It helps
828
- // to avoid unexpected bugs introduced by bad conversion, and undefined behavior
829
- // like overflow when casting float point numbers to integral numbers.
830
- //
831
- // When doing clamp_cast<Dst>(value), if `value` is in valid range of Dst,
832
- // it will give correct result in Dst, equal to `value`.
833
- //
834
- // If `value` is outside the representable range of Dst, it will be clamped to
835
- // MAX or MIN in Dst, instead of being undefined behavior.
836
- //
837
- // Float NaNs are converted to 0 in integral type.
838
- //
839
- // Here's some comparison with static_cast<>:
840
- // (with FB-internal gcc-5-glibc-2.23 toolchain)
841
- //
842
- // static_cast<int32_t>(NaN) = 6
843
- // clamp_cast<int32_t>(NaN) = 0
844
- //
845
- // static_cast<int32_t>(9999999999.0f) = -348639895
846
- // clamp_cast<int32_t>(9999999999.0f) = 2147483647
847
- //
848
- // static_cast<int32_t>(2147483647.0f) = -348639895
849
- // clamp_cast<int32_t>(2147483647.0f) = 2147483647
850
- //
851
- // static_cast<uint32_t>(4294967295.0f) = 0
852
- // clamp_cast<uint32_t>(4294967295.0f) = 4294967295
853
- //
854
- // static_cast<uint32_t>(-1) = 4294967295
855
- // clamp_cast<uint32_t>(-1) = 0
856
- //
857
- // static_cast<int16_t>(32768u) = -32768
858
- // clamp_cast<int16_t>(32768u) = 32767
859
-
860
- template <typename Dst, typename Src>
861
- constexpr typename std::enable_if<std::is_integral<Src>::value, Dst>::type
862
- constexpr_clamp_cast(Src src) {
863
- static_assert(
864
- std::is_integral<Dst>::value && sizeof(Dst) <= sizeof(int64_t),
865
- "constexpr_clamp_cast can only cast into integral type (up to 64bit)");
866
-
867
- using L = std::numeric_limits<Dst>;
868
- // clang-format off
869
- return
870
- // Check if Src and Dst have same signedness.
871
- std::is_signed<Src>::value == std::is_signed<Dst>::value
872
- ? (
873
- // Src and Dst have same signedness. If sizeof(Src) <= sizeof(Dst),
874
- // we can safely convert Src to Dst without any loss of accuracy.
875
- sizeof(Src) <= sizeof(Dst) ? Dst(src) :
876
- // If Src is larger in size, we need to clamp it to valid range in Dst.
877
- Dst(constexpr_clamp(src, Src(L::min()), Src(L::max()))))
878
- // Src and Dst have different signedness.
879
- // Check if it's signed -> unsigend cast.
880
- : std::is_signed<Src>::value && std::is_unsigned<Dst>::value
881
- ? (
882
- // If src < 0, the result should be 0.
883
- src < 0 ? Dst(0) :
884
- // Otherwise, src >= 0. If src can fit into Dst, we can safely cast it
885
- // without loss of accuracy.
886
- sizeof(Src) <= sizeof(Dst) ? Dst(src) :
887
- // If Src is larger in size than Dst, we need to ensure the result is
888
- // at most Dst MAX.
889
- Dst(constexpr_min(src, Src(L::max()))))
890
- // It's unsigned -> signed cast.
891
- : (
892
- // Since Src is unsigned, and Dst is signed, Src can fit into Dst only
893
- // when sizeof(Src) < sizeof(Dst).
894
- sizeof(Src) < sizeof(Dst) ? Dst(src) :
895
- // If Src does not fit into Dst, we need to ensure the result is at most
896
- // Dst MAX.
897
- Dst(constexpr_min(src, Src(L::max()))));
898
- // clang-format on
899
- }
900
-
901
- namespace detail {
902
- // Upper/lower bound values that could be accurately represented in both
903
- // integral and float point types.
904
- constexpr double kClampCastLowerBoundDoubleToInt64F = -9223372036854774784.0;
905
- constexpr double kClampCastUpperBoundDoubleToInt64F = 9223372036854774784.0;
906
- constexpr double kClampCastUpperBoundDoubleToUInt64F = 18446744073709549568.0;
907
-
908
- constexpr float kClampCastLowerBoundFloatToInt32F = -2147483520.0f;
909
- constexpr float kClampCastUpperBoundFloatToInt32F = 2147483520.0f;
910
- constexpr float kClampCastUpperBoundFloatToUInt32F = 4294967040.0f;
911
-
912
- // This works the same as constexpr_clamp, but the comparison are done in Src
913
- // to prevent any implicit promotions.
914
- template <typename D, typename S>
915
- constexpr D constexpr_clamp_cast_helper(S src, S sl, S su, D dl, D du) {
916
- return src < sl ? dl : (src > su ? du : D(src));
917
- }
918
- } // namespace detail
919
-
920
- template <typename Dst, typename Src>
921
- constexpr typename std::enable_if<std::is_floating_point<Src>::value, Dst>::type
922
- constexpr_clamp_cast(Src src) {
923
- static_assert(
924
- std::is_integral<Dst>::value && sizeof(Dst) <= sizeof(int64_t),
925
- "constexpr_clamp_cast can only cast into integral type (up to 64bit)");
926
-
927
- using L = std::numeric_limits<Dst>;
928
- // clang-format off
929
- return
930
- // Special case: cast NaN into 0.
931
- constexpr_isnan(src) ? Dst(0) :
932
- // using `sizeof(Src) > sizeof(Dst)` as a heuristic that Dst can be
933
- // represented in Src without loss of accuracy.
934
- // see: https://en.wikipedia.org/wiki/Floating-point_arithmetic
935
- sizeof(Src) > sizeof(Dst) ?
936
- detail::constexpr_clamp_cast_helper(
937
- src, Src(L::min()), Src(L::max()), L::min(), L::max()) :
938
- // sizeof(Src) < sizeof(Dst) only happens when doing cast of
939
- // 32bit float -> u/int64_t.
940
- // Losslessly promote float into double, change into double -> u/int64_t.
941
- sizeof(Src) < sizeof(Dst) ? (
942
- src >= 0.0
943
- ? constexpr_clamp_cast<Dst>(
944
- constexpr_clamp_cast<std::uint64_t>(double(src)))
945
- : constexpr_clamp_cast<Dst>(
946
- constexpr_clamp_cast<std::int64_t>(double(src)))) :
947
- // The following are for sizeof(Src) == sizeof(Dst).
948
- std::is_same<Src, double>::value && std::is_same<Dst, int64_t>::value ?
949
- detail::constexpr_clamp_cast_helper(
950
- double(src),
951
- detail::kClampCastLowerBoundDoubleToInt64F,
952
- detail::kClampCastUpperBoundDoubleToInt64F,
953
- L::min(),
954
- L::max()) :
955
- std::is_same<Src, double>::value && std::is_same<Dst, uint64_t>::value ?
956
- detail::constexpr_clamp_cast_helper(
957
- double(src),
958
- 0.0,
959
- detail::kClampCastUpperBoundDoubleToUInt64F,
960
- L::min(),
961
- L::max()) :
962
- std::is_same<Src, float>::value && std::is_same<Dst, int32_t>::value ?
963
- detail::constexpr_clamp_cast_helper(
964
- float(src),
965
- detail::kClampCastLowerBoundFloatToInt32F,
966
- detail::kClampCastUpperBoundFloatToInt32F,
967
- L::min(),
968
- L::max()) :
969
- detail::constexpr_clamp_cast_helper(
970
- float(src),
971
- 0.0f,
972
- detail::kClampCastUpperBoundFloatToUInt32F,
973
- L::min(),
974
- L::max());
975
- // clang-format on
976
- }
977
-
978
- } // namespace folly