Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Arccosinus and Arcsinus using floating point assembly

Arccosinus and Arcsinus using floating point assembly

Scheduled Pinned Locked Moved C / C++ / MFC
dotnettutorial
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SystemFiles
    wrote on last edited by
    #1

    Hi everybody, I want to get rid of all the imports from the CLR. So I decided to make some functions on my own for example cos() sin() tan() asin() acos(). I already have cos and sin and tan done:

    FLOAT Sin( FLOAT A )
    {
    _asm FLD A;
    _asm FSIN;
    }

    FLOAT Cos( FLOAT A )
    {
    _asm FLD A;
    _asm FCOS;
    }

    FLOAT Tan( FLOAT A )
    {
    _asm FLD A;
    _asm FPTAN;
    }

    As you can see I am using inline floating point assembly. But then I thought I could probably use FACOS or FASIN, but those instructions don't exist. Anybody know how I could constructe asin() and acos(), without using the CLR ofcourse. Regards, SystemFiles

    L C 2 Replies Last reply
    0
    • S SystemFiles

      Hi everybody, I want to get rid of all the imports from the CLR. So I decided to make some functions on my own for example cos() sin() tan() asin() acos(). I already have cos and sin and tan done:

      FLOAT Sin( FLOAT A )
      {
      _asm FLD A;
      _asm FSIN;
      }

      FLOAT Cos( FLOAT A )
      {
      _asm FLD A;
      _asm FCOS;
      }

      FLOAT Tan( FLOAT A )
      {
      _asm FLD A;
      _asm FPTAN;
      }

      As you can see I am using inline floating point assembly. But then I thought I could probably use FACOS or FASIN, but those instructions don't exist. Anybody know how I could constructe asin() and acos(), without using the CLR ofcourse. Regards, SystemFiles

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      when sine or cosine are known, so is the other (remember sin^2 + cos^2 = 1), hence also the tangent. Therefore use FPATAN. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      S 2 Replies Last reply
      0
      • L Luc Pattyn

        when sine or cosine are known, so is the other (remember sin^2 + cos^2 = 1), hence also the tangent. Therefore use FPATAN. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        S Offline
        S Offline
        SystemFiles
        wrote on last edited by
        #3

        takes a while before I understand what you mean hehe math actually isn't my best point of programming. I will get to this later!

        1 Reply Last reply
        0
        • L Luc Pattyn

          when sine or cosine are known, so is the other (remember sin^2 + cos^2 = 1), hence also the tangent. Therefore use FPATAN. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          S Offline
          S Offline
          SystemFiles
          wrote on last edited by
          #4

          Sorry still dont exactly understand what your saying.

          L 1 Reply Last reply
          0
          • S SystemFiles

            Sorry still dont exactly understand what your saying.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            when you know cos, you know sin (except for its sign); and vice versa. when you know cos and sin, you know tan. So use FPATAN. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            1 Reply Last reply
            0
            • S SystemFiles

              Hi everybody, I want to get rid of all the imports from the CLR. So I decided to make some functions on my own for example cos() sin() tan() asin() acos(). I already have cos and sin and tan done:

              FLOAT Sin( FLOAT A )
              {
              _asm FLD A;
              _asm FSIN;
              }

              FLOAT Cos( FLOAT A )
              {
              _asm FLD A;
              _asm FCOS;
              }

              FLOAT Tan( FLOAT A )
              {
              _asm FLD A;
              _asm FPTAN;
              }

              As you can see I am using inline floating point assembly. But then I thought I could probably use FACOS or FASIN, but those instructions don't exist. Anybody know how I could constructe asin() and acos(), without using the CLR ofcourse. Regards, SystemFiles

              C Offline
              C Offline
              cmk
              wrote on last edited by
              #6

              Here's some old code that may help:

              double Vk05Pi = 1.5707963267948966192313216916398;

              #define MTH_ASM_IS0(LABEL_IS0) __asm \
              { \
              __asm ftst \
              __asm fnstsw ax \
              __asm test ah, 40h \
              __asm jne LABEL_IS0 \
              }
              #define MTH_ASM_IS1(LABEL_IS1) __asm \
              { \
              __asm fld1 \
              __asm fcomp \
              __asm fnstsw ax \
              __asm test ah, 40h \
              __asm jne LABEL_IS1 \
              }

              // arcsine: atan( sqrt( A*A / (1-A*A) ) )
              //
              double FkASinR( double A )
              {
              if( A > 1 ) return(0);
              #ifndef MTH_ASM_USE
              A *= A;
              if( FkIs1(A, DBL_MIN) ) return(Vk05Pi);
              return( atan( sqrt(A/(1-A)) ) );
              #else
              __asm {
              fld A
              fmul A
              MTH_ASM_IS1(isone)
              fld st(0)
              fld1
              fsubr // 1 - A*A
              fdiv // A*A / (1-A*A)
              fsqrt
              fld1
              fpatan
              jmp end
              isone:
              fstp A
              fld Vk05Pi
              end:
              fstp A
              }
              return(A);
              #endif
              }

              // arccosine: atan( sqrt( (1-A*A) / (A*A) ) )
              //
              double FkACosR( double A )
              {
              #ifndef MTH_ASM_USE
              A *= A;
              if( FkIs0(A, DBL_MIN) ) return(Vk05Pi);
              return( atan( sqrt((1-A)/A) ) );
              #else
              __asm {
              fld A
              fmul A
              MTH_ASM_IS0(iszero)
              fld st(0)
              fld1
              fsubr // 1 - A*A
              fdivr // (1-A*A) / A*A
              fsqrt
              fld1
              fpatan
              jmp end
              iszero:
              fstp A
              fld Vk05Pi
              end:
              fstp A
              }
              return(A);
              #endif
              }

              ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

              S 1 Reply Last reply
              0
              • C cmk

                Here's some old code that may help:

                double Vk05Pi = 1.5707963267948966192313216916398;

                #define MTH_ASM_IS0(LABEL_IS0) __asm \
                { \
                __asm ftst \
                __asm fnstsw ax \
                __asm test ah, 40h \
                __asm jne LABEL_IS0 \
                }
                #define MTH_ASM_IS1(LABEL_IS1) __asm \
                { \
                __asm fld1 \
                __asm fcomp \
                __asm fnstsw ax \
                __asm test ah, 40h \
                __asm jne LABEL_IS1 \
                }

                // arcsine: atan( sqrt( A*A / (1-A*A) ) )
                //
                double FkASinR( double A )
                {
                if( A > 1 ) return(0);
                #ifndef MTH_ASM_USE
                A *= A;
                if( FkIs1(A, DBL_MIN) ) return(Vk05Pi);
                return( atan( sqrt(A/(1-A)) ) );
                #else
                __asm {
                fld A
                fmul A
                MTH_ASM_IS1(isone)
                fld st(0)
                fld1
                fsubr // 1 - A*A
                fdiv // A*A / (1-A*A)
                fsqrt
                fld1
                fpatan
                jmp end
                isone:
                fstp A
                fld Vk05Pi
                end:
                fstp A
                }
                return(A);
                #endif
                }

                // arccosine: atan( sqrt( (1-A*A) / (A*A) ) )
                //
                double FkACosR( double A )
                {
                #ifndef MTH_ASM_USE
                A *= A;
                if( FkIs0(A, DBL_MIN) ) return(Vk05Pi);
                return( atan( sqrt((1-A)/A) ) );
                #else
                __asm {
                fld A
                fmul A
                MTH_ASM_IS0(iszero)
                fld st(0)
                fld1
                fsubr // 1 - A*A
                fdivr // (1-A*A) / A*A
                fsqrt
                fld1
                fpatan
                jmp end
                iszero:
                fstp A
                fld Vk05Pi
                end:
                fstp A
                }
                return(A);
                #endif
                }

                ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

                S Offline
                S Offline
                SystemFiles
                wrote on last edited by
                #7

                Thats awesome thank you

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups