Last active
July 3, 2022 18:52
-
-
Save bkietz/c00c61e73f847d3cf4454c8c4cb4ced0 to your computer and use it in GitHub Desktop.
exposing FSINCOS to Visual C++ using assembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SinCos.h" | |
#include <cmath> | |
#include <cassert> | |
#include <limits> | |
int main() | |
{ | |
constexpr double pi = 3.14159265358979323846; | |
constexpr double err = std::numeric_limits<double>::epsilon(); | |
double radians, sine, cosine, delta; | |
for (auto i = 0; i != 16; ++i) | |
{ | |
radians = i * pi / 16; | |
SinCos(radians, &sine, &cosine); | |
delta = std::abs(std::sin(radians) - sine); | |
if (delta > err) | |
return 1; | |
delta = std::abs(std::cos(radians) - cosine); | |
if (delta > err) | |
return 1; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.code | |
PUBLIC SinCos | |
; void SinCos(double AngleInRadians, double *pSinAns, double *pCosAns); | |
angle_on_stack$ = 8 | |
SinCos PROC | |
movsd QWORD PTR angle_on_stack$[rsp], xmm0 ; argument angle is in xmm0, move it to the stack | |
fld QWORD PTR angle_on_stack$[rsp] ; push angle onto the FPU stack where we can do FLOPs | |
fsincos | |
fstp QWORD PTR [r8] ; store/pop cosine output argument | |
fstp QWORD PTR [rdx] ; store/pop sine output argument | |
ret 0 | |
SinCos ENDP | |
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
extern "C" | |
{ | |
#if defined(_M_AMD64) | |
// 64 bit implementation in MASM | |
// VS2017> enable MASM by right clicking your project in solution explorer then | |
// Build Dependencies > Build Customizations > MASM | |
extern void SinCos(double AngleInRadians, double *pSinAns, double *pCosAns); | |
// Add your assembly file as a source, but exclude it from 32 bit build | |
#else | |
// 32 bit implementation in inline assembly | |
inline void SinCos(double AngleInRadians, double *pSinAns, double *pCosAns) | |
{ | |
double SinAns, CosAns; | |
__asm { | |
fld QWORD PTR[AngleInRadians] | |
fsincos | |
fstp QWORD PTR[CosAns] | |
fstp QWORD PTR[SinAns] | |
fwait | |
} | |
*pSinAns = SinAns; | |
*pCosAns = CosAns; | |
} | |
#endif | |
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<ItemGroup> | |
<Filter Include="Source Files"> | |
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | |
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | |
</Filter> | |
<Filter Include="Header Files"> | |
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | |
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | |
</Filter> | |
<Filter Include="Resource Files"> | |
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | |
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | |
</Filter> | |
</ItemGroup> | |
<ItemGroup> | |
<ClInclude Include="SinCos.h"> | |
<Filter>Header Files</Filter> | |
</ClInclude> | |
</ItemGroup> | |
<ItemGroup> | |
<ClCompile Include="Main.cpp"> | |
<Filter>Source Files</Filter> | |
</ClCompile> | |
</ItemGroup> | |
<ItemGroup> | |
<MASM Include="SinCos.asm"> | |
<Filter>Source Files</Filter> | |
</MASM> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<ItemGroup Label="ProjectConfigurations"> | |
<ProjectConfiguration Include="Debug|Win32"> | |
<Configuration>Debug</Configuration> | |
<Platform>Win32</Platform> | |
</ProjectConfiguration> | |
<ProjectConfiguration Include="Release|Win32"> | |
<Configuration>Release</Configuration> | |
<Platform>Win32</Platform> | |
</ProjectConfiguration> | |
<ProjectConfiguration Include="Debug|x64"> | |
<Configuration>Debug</Configuration> | |
<Platform>x64</Platform> | |
</ProjectConfiguration> | |
<ProjectConfiguration Include="Release|x64"> | |
<Configuration>Release</Configuration> | |
<Platform>x64</Platform> | |
</ProjectConfiguration> | |
</ItemGroup> | |
<ItemGroup> | |
<ClInclude Include="SinCos.h" /> | |
</ItemGroup> | |
<ItemGroup> | |
<ClCompile Include="Main.cpp" /> | |
</ItemGroup> | |
<ItemGroup> | |
<MASM Include="SinCos.asm"> | |
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild> | |
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild> | |
</MASM> | |
</ItemGroup> | |
<PropertyGroup Label="Globals"> | |
<VCProjectVersion>15.0</VCProjectVersion> | |
<ProjectGuid>{F390DF0D-F092-46E8-B8A5-608C3C19BDF4}</ProjectGuid> | |
<RootNamespace>AsmHackery</RootNamespace> | |
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion> | |
<ProjectName>SinCosAsm</ProjectName> | |
</PropertyGroup> | |
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | |
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | |
<ConfigurationType>Application</ConfigurationType> | |
<UseDebugLibraries>true</UseDebugLibraries> | |
<PlatformToolset>v141</PlatformToolset> | |
<CharacterSet>MultiByte</CharacterSet> | |
</PropertyGroup> | |
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | |
<ConfigurationType>Application</ConfigurationType> | |
<UseDebugLibraries>false</UseDebugLibraries> | |
<PlatformToolset>v141</PlatformToolset> | |
<WholeProgramOptimization>true</WholeProgramOptimization> | |
<CharacterSet>MultiByte</CharacterSet> | |
</PropertyGroup> | |
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | |
<ConfigurationType>Application</ConfigurationType> | |
<UseDebugLibraries>true</UseDebugLibraries> | |
<PlatformToolset>v141</PlatformToolset> | |
<CharacterSet>MultiByte</CharacterSet> | |
</PropertyGroup> | |
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | |
<ConfigurationType>Application</ConfigurationType> | |
<UseDebugLibraries>false</UseDebugLibraries> | |
<PlatformToolset>v141</PlatformToolset> | |
<WholeProgramOptimization>true</WholeProgramOptimization> | |
<CharacterSet>MultiByte</CharacterSet> | |
</PropertyGroup> | |
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | |
<ImportGroup Label="ExtensionSettings"> | |
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.props" /> | |
</ImportGroup> | |
<ImportGroup Label="Shared"> | |
</ImportGroup> | |
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |
</ImportGroup> | |
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |
</ImportGroup> | |
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | |
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |
</ImportGroup> | |
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | |
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |
</ImportGroup> | |
<PropertyGroup Label="UserMacros" /> | |
<PropertyGroup /> | |
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
<ClCompile> | |
<WarningLevel>Level3</WarningLevel> | |
<Optimization>Disabled</Optimization> | |
<SDLCheck>true</SDLCheck> | |
</ClCompile> | |
</ItemDefinitionGroup> | |
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | |
<ClCompile> | |
<WarningLevel>Level3</WarningLevel> | |
<Optimization>Disabled</Optimization> | |
<SDLCheck>true</SDLCheck> | |
</ClCompile> | |
</ItemDefinitionGroup> | |
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |
<ClCompile> | |
<WarningLevel>Level3</WarningLevel> | |
<Optimization>MaxSpeed</Optimization> | |
<FunctionLevelLinking>true</FunctionLevelLinking> | |
<IntrinsicFunctions>true</IntrinsicFunctions> | |
<SDLCheck>true</SDLCheck> | |
</ClCompile> | |
<Link> | |
<EnableCOMDATFolding>true</EnableCOMDATFolding> | |
<OptimizeReferences>true</OptimizeReferences> | |
</Link> | |
</ItemDefinitionGroup> | |
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | |
<ClCompile> | |
<WarningLevel>Level3</WarningLevel> | |
<Optimization>MaxSpeed</Optimization> | |
<FunctionLevelLinking>true</FunctionLevelLinking> | |
<IntrinsicFunctions>true</IntrinsicFunctions> | |
<SDLCheck>true</SDLCheck> | |
</ClCompile> | |
<Link> | |
<EnableCOMDATFolding>true</EnableCOMDATFolding> | |
<OptimizeReferences>true</OptimizeReferences> | |
</Link> | |
</ItemDefinitionGroup> | |
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |
<ImportGroup Label="ExtensionTargets"> | |
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" /> | |
</ImportGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment