ASM floating point problem

This is a discussion on ASM floating point problem within the C++ Programming forums, part of the General Programming Boards category; This is my code and problems: Code: #include <cstdlib> #include <iostream> #include <stdio.h> #include <conio.h> using namespace std; float angle=45, ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    ASM floating point problem

    This is my code and problems:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <stdio.h>
    #include <conio.h>
    
    using namespace std;
    
    float angle=45, result, output;
    
    int main()
    {
    	__asm("fsinx %1,%0" : "=f" (angle) : "f" (result));
    
            printf("Eventually I'll insert the output here");
    	getch();
    }
    and these are my compiling errors:

    In function `int main()':
    output constraint 0 must specify a single register
    output operand 0 must use `&' constraint
    [Build Error] [main.o] Error 1

    I really need to use floating point math with ASM for my Mandelbrot generator, but I cant even get this simple code working. Does anyone know how to properly compile this code with DevCpp? And if so can you please explain how to properly do floating point mathematics with ASM in DevCpp

    Gratsi

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Although I doubt that you actually need to use assembly, you need a working version that doesn't use assembly first, for comparison. Then only if that's too slow and other optimisation efforts have failed, would you need to consider assembly. Even then though, In all liklihood, you'd only need asm for only a tiny portion of the app to make the speed difference, such as for using fsincos. Unless you're going to use your processor's vector based routines, in which case the code above is the least of your worries.

    Did google searching not help?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    Reply...

    Yes, Ive googled everything that I could on the subject of inline ASM with mingw, and I just cannot get anything useful to my purpose...

    Im having problems with accessing registers, and specifically with floating point registers... I dont know what Im doing wrong but nothing seems to work and I cannot seem to figure out dev cpp's inline syntax even though ive looked it up from all over the place... im just really confused.

    Also the reason why I want to use ASM for my fractal generator is because I need to optimize the mathematics and tweak them at the most basic level, instead of using the usual floating point mathematics from withing the Cpp Compiler id rather work with it on a first hand level.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you had read a little up in the manual, you would see that that code was for a 68881 chip; I'm guessing that's not what you have.

    I found this manual from Intel, which may help. You also may want to ignore the extended asm, if that's giving you trouble, and put things on the floating stack yourself perhaps.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by parad0x13 View Post
    Also the reason why I want to use ASM for my fractal generator is because I need to optimize the mathematics and tweak them at the most basic level, instead of using the usual floating point mathematics from withing the Cpp Compiler id rather work with it on a first hand level.
    That will be really painful if you don't write it directly in C++ first. That's what people who write algorithms in ASM usually do first.
    If you decide beforehand that no matter how fast the C++ version is you're going to write it in ASM afterwards then that's entirely up to you, but you're still better off writing it in C++ first.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    That will be really painful if you don't write it directly in C++ first. That's what people who write algorithms in ASM usually do first.
    If you decide beforehand that no matter how fast the C++ version is you're going to write it in ASM afterwards then that's entirely up to you, but you're still better off writing it in C++ first.
    Also, in my experience, unless you go to SSE or some such, gcc with -O2 -ffast-math will probably produce very similar to what you can do in inline assembler, unless you are "cheating" (that is, you know something that the compiler can't possibly know).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    58
    iMalc, Ive allready been developing my fractal generator for quite some time now and have gradually moved up from ascii images of fractals to opengl versions, the code is allready written and my math algorithms have allready been esablished, however to increase speed I want to translate from my cpp to asm. If I WERE to have started programming something as complex as a fractal generator immidiatly in ASM, that would be something though

    Im interested in this fast math you speak of, I will reasearch it

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I don't know much about fractal generation, but from what I saw doing a quick google search, it's something that can be (easily?) parallelized. (I even found a youtube video of something that looks like real-time Mandelbrot fractal generation)

    So, knowing this, here's some ideas I'm throwing in:
    • Make your application multi threaded (if it's not already done?). This can pay off, especially if you have a high number of core to your disposition. But even with just two cores, if it's well done, it could pays off. No need to write assembly.
    • You could use the parallel instructions your processor offers. I'm taking a guess that you are running an x86 or x86-64 CPU, so here's a link to the Intel documentation. Take a look at volume 1 for the SSE instructions to make your mind about what your CPU has to offer.
    • If you really want to, and have an nVidia graphic card, you could also do some GPGPU with CUDA. Most GPUs offer an extremely big amount of processing power compared to what CPU offers.


    Or, the last solution, you could just not care, because all of the above idea might not be totally trivial to implement. Of course, if you are not afraid, jump straight to GPGPU.
    I hate real numbers.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I believe I've seen fractals implemented directly in pixel shaders without even the need of GPGPU facilities.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    Thanks

    Hey thanks for the pointers, I decided that I'm going to rebuild my generator from the ground up this time with multithreading and since I've never done multithreading before I have a lot of reasearch to do first. I think I'm going to have to learn pthreads instead of WIN32 API threading, unless anyone recommends otherwise which from what I hear, nobody does.

    Also I havn't gotten into all this multithreading and CPU/GPU based optimization because I want my program to be as convertable and useable on most any dated system I can, however this may not be the most forward thinking motive I've ever had.

    Also I really want to be able to port this program to Linux but with all the WIN32 API programming in it I doubt I will have anything but a hard time porting it, for I have no knowledge of the Linux systems internal process yet. More reaserch... yeeeeay

    Thank you all for your input : )

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I think I'm going to have to learn pthreads instead of WIN32 API threading, unless anyone recommends otherwise which from what I hear, nobody does.
    I recommend Boost's threading library. It's very close to what the standard C++ threading support will look like.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number comparison
    By lehe in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2009, 07:11 PM
  2. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  3. Floating point checking problem
    By ikkiutsu in forum C Programming
    Replies: 8
    Last Post: 12-03-2003, 05:35 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21