Advertisement
es3n1n

x86 inline asm math

Feb 27th, 2022
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.93 KB | None | 0 0
  1. namespace util {
  2.     namespace math {
  3.         constexpr auto e = 2.71828182845904523536f;
  4.         constexpr auto pi = 3.14159265358979323846f;
  5.         constexpr auto radpi = 180.f / pi;
  6.         constexpr auto sqrpi = pi * pi;
  7.         constexpr auto round_error = 0.5;
  8.        
  9.         double __forceinline deg2rad( double x ) {
  10.             return x * ( static_cast< double >( pi ) / 180.0 );
  11.         }
  12.  
  13.         double __forceinline rad2deg( double x ) {
  14.             return x * ( 180.0 / static_cast< double >( pi ) );
  15.         }
  16.  
  17.         double __forceinline __declspec ( naked ) __fastcall sin( double x ) {
  18.             __asm {
  19.                 fld     qword ptr[ esp + 4 ]
  20.                 fsin
  21.                 ret     8
  22.             }
  23.         }
  24.  
  25.         double __forceinline __declspec ( naked ) __fastcall asin( double x ) {
  26.             __asm {
  27.                 fld     qword ptr[ esp + 4 ]
  28.                 fld     st
  29.                 fabs
  30.                 fcom        dword ptr[ round_error ]
  31.                 fstsw       ax
  32.                 sahf
  33.                 jbe     asin_cont
  34.                 fld1
  35.                 fsubrp  st( 1 ), st( 0 )
  36.                 fld     st
  37.                 fadd        st( 0 ), st( 0 )
  38.                 fxch        st( 1 )
  39.                 fmul        st( 0 ), st( 0 )
  40.                 fsubp       st( 1 ), st( 0 )
  41.                 jmp     asin_exit
  42.  
  43.                 asin_cont :
  44.  
  45.                 fstp        st( 0 )
  46.                     fld     st( 0 )
  47.                     fmul        st( 0 ), st( 0 )
  48.                     fld1
  49.                     fsubrp  st( 1 ), st( 0 )
  50.  
  51.                     asin_exit:
  52.  
  53.                 fsqrt
  54.                     fpatan
  55.                     ret     8
  56.             }
  57.         }
  58.  
  59.         double __forceinline __declspec ( naked ) __fastcall sinh( double x ) {
  60.             __asm {
  61.                 fld     qword ptr[ esp + 4 ]
  62.                 fldl2e
  63.                 fmulp       st( 1 ), st
  64.                 fld1
  65.                 fld     st( 1 )
  66.                 fprem
  67.                 f2xm1
  68.                 faddp       st( 1 ), st
  69.                 fscale
  70.                 fxch
  71.                 fstp        st
  72.                 fld1
  73.                 fdiv        st, st( 1 )
  74.                 fsubp       st( 1 ), st
  75.                 mov     eax, 0x3F000000
  76.                 push        eax
  77.                 fld     dword ptr[ esp ]
  78.                 fmulp       st( 1 ), st
  79.                 pop     eax
  80.                 ret     8
  81.             }
  82.         }
  83.  
  84.         // @note: es3n1n: comparing to math.h: (1.) (util::math)0.88137358701954294 vs (math.h)0.88137358701954305
  85.         double __forceinline __declspec ( naked ) __fastcall asinh( double x ) {
  86.             __asm {
  87.                 fld     qword ptr[ esp + 4 ]
  88.                 fld     st
  89.                 fmul        st, st
  90.                 fld1
  91.                 faddp       st( 1 ), st
  92.                 fsqrt
  93.                 faddp       st( 1 ), st
  94.                 fldln2
  95.                 fxch
  96.                 fyl2x
  97.                 ret     8
  98.             }
  99.         }
  100.  
  101.         double __forceinline __declspec ( naked ) __fastcall cos( double x ) {
  102.             __asm {
  103.                 fld     qword ptr[ esp + 4 ]
  104.                 fcos
  105.                 ret     8
  106.             }
  107.         }
  108.  
  109.         // @note: es3n1n: comparing to math.h: (.5) (util::math)1.0471975511965976 vs (math.h)1.0471975511965979
  110.         double __forceinline __declspec ( naked ) __fastcall acos( double x ) {
  111.             __asm {
  112.                 fld     qword ptr[ esp + 4 ]
  113.                 fld1
  114.                 fchs
  115.                 fcomp       st( 1 )
  116.                 fstsw       ax
  117.                 je          skip
  118.  
  119.                 fld     st( 0 )
  120.                 fld1
  121.                 fsubrp  st( 1 ), st( 0 )
  122.                 fxch        st( 1 )
  123.                 fld1
  124.                 faddp       st( 1 ), st( 0 )
  125.                 fdivp       st( 1 ), st( 0 )
  126.                 fsqrt
  127.                 fld1
  128.                 jmp     end
  129.  
  130.                 skip :
  131.  
  132.                 fld1
  133.                     fldz
  134.  
  135.                     end :
  136.  
  137.                 fpatan
  138.                     fadd        st( 0 ), st( 0 )
  139.                     ret     8
  140.             }
  141.         }
  142.  
  143.         double __forceinline __declspec ( naked ) __fastcall cosh( double x ) {
  144.             __asm {
  145.                 fld     qword ptr[ esp + 4 ]
  146.                 fldl2e
  147.                 fmulp       st( 1 ), st
  148.                 fld1
  149.                 fld     st( 1 )
  150.                 fprem
  151.                 f2xm1
  152.                 faddp       st( 1 ), st
  153.                 fscale
  154.                 fxch
  155.                 fstp        st
  156.                 fld1
  157.                 fdiv        st, st( 1 )
  158.                 faddp       st( 1 ), st
  159.                 mov     eax, 0x3F000000
  160.                 push        eax
  161.                 fld     dword ptr[ esp ]
  162.                 fmulp       st( 1 ), st
  163.                 pop     eax
  164.                 ret     8
  165.             }
  166.         }
  167.  
  168.         double __forceinline __declspec ( naked ) __fastcall acosh( double x ) {
  169.             __asm {
  170.                 fld     qword ptr[ esp + 4 ]
  171.                 fld     st
  172.                 fmul        st, st
  173.                 fld1
  174.                 fsubp       st( 1 ), st
  175.                 fsqrt
  176.                 faddp       st( 1 ), st
  177.                 fldln2
  178.                 fxch
  179.                 fyl2x
  180.                 ret     8
  181.             }
  182.         }
  183.  
  184.         double __forceinline __declspec ( naked ) __fastcall tan( double x ) {
  185.             __asm {
  186.                 fld     qword ptr[ esp + 4 ]
  187.                 fptan
  188.                 fstp        st( 0 )
  189.                 ret     8
  190.             }
  191.         }
  192.  
  193.         double __forceinline __declspec ( naked ) __fastcall atan( double x ) {
  194.             __asm {
  195.                 fld     qword ptr[ esp + 4 ]
  196.                 fld1
  197.                 fpatan
  198.                 ret     8
  199.             }
  200.         }
  201.  
  202.         double __forceinline __declspec ( naked ) __fastcall atan2( double x, double y ) {
  203.             __asm {
  204.                 fld     qword ptr[ esp + 4 ]
  205.                 fld     qword ptr[ esp + 12 ]
  206.                 fpatan
  207.                 ret     16
  208.             }
  209.         }
  210.  
  211.         double __forceinline __declspec ( naked ) __fastcall tanh( double x ) {
  212.             __asm {
  213.                 fld     qword ptr[ esp + 4 ]
  214.                 fld     st
  215.                 mov     eax, 0x40000000
  216.                 push        eax
  217.                 fld     dword ptr[ esp ]
  218.                 fmul        st, st( 1 )
  219.                 fldl2e
  220.                 fmulp       st( 1 ), st
  221.                 fld1
  222.                 fld     st( 1 )
  223.                 fprem
  224.                 f2xm1
  225.                 faddp       st( 1 ), st
  226.                 fscale
  227.                 fxch
  228.                 fstp        st
  229.                 fld1
  230.                 fsub        st, st( 1 )
  231.                 fchs
  232.                 fld1
  233.                 faddp       st( 2 ), st
  234.                 fdivrp  st( 1 ), st
  235.                 pop     eax
  236.                 ret     8
  237.             }
  238.         }
  239.  
  240.         // @note: es3n1n: comparing to math.h: (.4) (util::math)0.42364893019360178 vs (math.h)0.42364893019360184
  241.         double __forceinline __declspec ( naked ) __fastcall atanh( double x ) {
  242.             __asm {
  243.                 fld     qword ptr[ esp + 4 ]
  244.                 fld1
  245.                 fsub        st, st( 1 )
  246.                 fld1
  247.                 faddp       st( 2 ), st
  248.                 fdivrp  st( 1 ), st
  249.                 fldln2
  250.                 fxch
  251.                 fyl2x
  252.                 mov     eax, 0xBF000000
  253.                 push        eax
  254.                 fld     dword ptr[ esp ]
  255.                 fmulp       st( 1 ), st
  256.                 pop     eax
  257.                 ret     8
  258.             }
  259.         }
  260.  
  261.         double __forceinline __declspec ( naked ) __fastcall exp( double x ) {
  262.             __asm {
  263.                 fld     qword ptr[ esp + 4 ]
  264.                 fldl2e
  265.                 fmulp       st( 1 ), st
  266.                 fld1
  267.                 fld     st( 1 )
  268.                 fprem
  269.                 f2xm1
  270.                 faddp       st( 1 ), st
  271.                 fscale
  272.                 fxch
  273.                 fstp        st
  274.                 ret     8
  275.             }
  276.         }
  277.  
  278.         // @note: es3n1n: comparing to math.h: (.8) (util::math)1.7411011265922482 vs (math.h)1.7411011265922485
  279.         double __forceinline __declspec ( naked ) __fastcall exp2( double x ) {
  280.             __asm {
  281.                 fld     qword ptr[ esp + 4 ]
  282.                 fld1
  283.                 fld     st( 1 )
  284.                 fprem
  285.                 f2xm1
  286.                 faddp       st( 1 ), st
  287.                 fscale
  288.                 fxch
  289.                 fstp        st
  290.                 ret     8
  291.             }
  292.         }
  293.  
  294.         double __forceinline __declspec ( naked ) __fastcall log( double x ) {
  295.             __asm {
  296.                 fld     qword ptr[ esp + 4 ]
  297.                 fldln2
  298.                 fxch
  299.                 fyl2x
  300.                 ret     8
  301.             }
  302.         }
  303.  
  304.         double __forceinline __declspec ( naked ) __fastcall log2( double x ) {
  305.             __asm {
  306.                 fld     qword ptr[ esp + 4 ]
  307.                 fld1
  308.                 fxch
  309.                 fyl2x
  310.                 ret     8
  311.             }
  312.         }
  313.  
  314.         double __forceinline __declspec ( naked ) __fastcall fabs( double x ) {
  315.             __asm {
  316.                 fld     qword ptr[ esp + 4 ]
  317.                 fabs
  318.                 ret     8
  319.             }
  320.         }
  321.  
  322.         double __forceinline __declspec ( naked ) __fastcall pow( double x, double y ) {
  323.             __asm {
  324.                 fld     qword ptr[ esp + 12 ]
  325.                 fld     qword ptr[ esp + 4 ]
  326.                 ftst
  327.                 fstsw       ax
  328.                 sahf
  329.                 jz          skip
  330.  
  331.                 fyl2x
  332.                 fld1
  333.                 fld     st( 1 )
  334.                 fprem
  335.                 f2xm1
  336.                 faddp       st( 1 ), st( 0 )
  337.                 fscale
  338.  
  339.                 skip :
  340.  
  341.                 fstp        st( 1 )
  342.                     ret     16
  343.             }
  344.         }
  345.  
  346.         double __forceinline __declspec ( naked ) __fastcall ceil( double x ) {
  347.             __asm {
  348.                 fld     qword ptr[ esp + 4 ]
  349.                 fchs
  350.                 fld1
  351.                 fld     st( 1 )
  352.                 fprem
  353.                 sub     esp, 4
  354.                 fst     dword ptr[ esp ]
  355.                 fxch        st( 2 )
  356.                 mov     eax, [ esp ]
  357.                 cmp     eax, 0x80000000
  358.                 jbe     skip
  359.                 fsub        st, st( 1 )
  360.  
  361.                 skip:
  362.  
  363.                 fsub        st, st( 2 )
  364.                     fstp        st( 1 )
  365.                     fstp        st( 1 )
  366.                     fchs
  367.                     pop     eax
  368.                     ret     8
  369.             }
  370.         }
  371.  
  372.         double __forceinline __declspec ( naked ) __fastcall floor( double x ) {
  373.             __asm {
  374.                 fld     qword ptr[ esp + 4 ]
  375.                 fld1
  376.                 fld     st( 1 )
  377.                 fprem
  378.                 sub     esp, 4
  379.                 fst     dword ptr[ esp ]
  380.                 fxch        st( 2 )
  381.                 mov     eax, [ esp ]
  382.                 cmp     eax, 0x80000000
  383.                 jbe     floor_exit
  384.                 fsub        st, st( 1 )
  385.  
  386.                 floor_exit:
  387.  
  388.                 fsub        st, st( 2 )
  389.                     fstp        st( 1 )
  390.                     fstp        st( 1 )
  391.                     pop     eax
  392.                     ret     8
  393.             }
  394.         }
  395.  
  396.         double __forceinline __declspec ( naked ) __fastcall fmod( double x, double y ) {
  397.             __asm {
  398.                 fld     qword ptr[ esp + 12 ]
  399.                 fld     qword ptr[ esp + 4 ]
  400.                 fprem
  401.                 fxch
  402.                 fstp        st
  403.                 ret     16
  404.             }
  405.         }
  406.  
  407.         double __forceinline __declspec ( naked ) __fastcall sqrt( double x ) {
  408.             __asm {
  409.                 fld     qword ptr[ esp + 4 ]
  410.                 fsqrt
  411.                 ret     8
  412.             }
  413.         }
  414.  
  415.         double __forceinline __declspec ( naked ) __fastcall hypot( double x, double y ) {
  416.             __asm {
  417.                 fld     qword ptr[ esp + 12 ]
  418.                 fld     qword ptr[ esp + 4 ]
  419.                 fmul        st, st
  420.                 fxch
  421.                 fmul        st, st
  422.                 faddp       st( 1 ), st
  423.                 fsqrt
  424.                 ret     16
  425.             }
  426.         }
  427.     }
  428. }
  429.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement