0

I am writing a compiler for a variation of Fortran to MIPS as an assignment for the University.

In that language, power operator is built-in, exactly like Python where you can say a ** b and it's done.

So, in some point I have to write assembly for the pow function.

Of course, pow(int x, int y) is easy, as pow(float x, int y) is easy, but what happens when the exponent is float? How do I calculate that given my instruction set can only give me addition, subtraction, multiplication and division?

I have think of Taylor series, but how could I implement that? I am not so sure.

Let's say I take the 3rd order Taylor's polynomial around a fixed point (x0, y0), won't that approximation be really bad for points far from (x0, y0)?

I need an easy and simple way to do that, because that's a toy compiler, not a scientific one with real use cases, it has no optimizations etc.

Moreover, I don't really know what it should do when the the base is negative and the exponent is a fraction, let's say 1/2 where the result is a complex number (language supports complex numbers).

So, please give me your sights. Thanks in advance.

  • 1
    Doing this well is a "delicate issue of numerical analysis" and may not have an easy or simple answer. Are you allowed to just generate a call to the standard C library's `powf` function? That's what most compilers will do - it means everyone can use a single well-tested implementation written by experts, instead of having to hack together their own. – Nate Eldredge May 21 '21 at 19:14
  • @NateEldredge, Given a .f file my compiler has to give as an output .asm file, how do you suggest calling C for doing operations in MIPS? – Constantinos Petrakis May 21 '21 at 19:33
  • @Jean-BaptisteYunès I need more precision in the answer, I want something like a pseudo-code: E.g step 1: Take the 3d order Taylor's polynomial in point .. Step 2... Not stuff I have no idea to implement. Thanks though. – Constantinos Petrakis May 21 '21 at 19:35
  • @ConstantinosPetrakis: I don't know the MIPS architecture very well, but you look up the calling conventions, load your values into registers accordingly, invoke the appropriate call instruction, and retrieve the result from the appropriate register. Maybe like [this](https://godbolt.org/z/vzanqhs4T). So the asm file will just contain those instructions. Of course, to run the code, you'll have to link with the system C library. – Nate Eldredge May 21 '21 at 21:22
  • 1
    @ConstantinosPetrakis: The problem is that a complete explanation looks to be something like a book chapter, and I'm not sure you'll find someone here willing to take the time to write it bespoke for you. – Nate Eldredge May 21 '21 at 21:28

0 Answers0