FPU initialization

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
abstractmath
Member
Member
Posts: 46
Joined: Mon Sep 07, 2020 5:50 pm

FPU initialization

Post by abstractmath »

Hello! I've been having trouble with floating point operations in my OS, and I've been having issues trouble shooting exactly what the problem is. One suggestion I got was to check if the FPU was initialized, and after a lot of googling I haven't been able to find much information on it. I have looked at the x86 floating point instruction set, but using the fninit instruction as suggested still yields the same problems. I'd really appreciate some advice when it comes to handling the FPU! Thanks!
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: FPU initialization

Post by Octocontrabass »

To initialize the x87 FPU, set CR0.MP and CR0.NE, clear CR0.EM and CR0.TS, and execute FNINIT. (I'm assuming you don't care about ancient or embedded hardware.)

What problems are you having, exactly?
abstractmath
Member
Member
Posts: 46
Joined: Mon Sep 07, 2020 5:50 pm

Re: FPU initialization

Post by abstractmath »

I'm getting a strange issue with floating point division. From what I can tell, multiplication, subtraction, and addition work just fine. Any floating point division results in a NaN.
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: FPU initialization

Post by Octocontrabass »

I notice from your github that you're assigning strings to variables before printing them. Is there some reason you can't directly pass string literals to your print function?
abstractmath
Member
Member
Posts: 46
Joined: Mon Sep 07, 2020 5:50 pm

Re: FPU initialization

Post by abstractmath »

Yeah, for some reason when I do that, it doesn't print anything. I didn't think it was entirely significant since I could just work out around it out of laziness.
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: FPU initialization

Post by Octocontrabass »

It's pretty significant. String constants and floating-point constants both go in the rodata section, and I'd guess yours is either not included in the final binary or is not loaded at the correct address.

You might need to start using a linker script for that.
abstractmath
Member
Member
Posts: 46
Joined: Mon Sep 07, 2020 5:50 pm

Re: FPU initialization

Post by abstractmath »

Oh wow I had no idea those two issues would be linked! As for linker scripts, I'm not entirely sure how I would go about making one, and how it would resolve this issue. Could you elaborate more?
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: FPU initialization

Post by Octocontrabass »

There's an example script here you can adapt for your own use. Pass its filename to ld using the "-T" option.

I suggest also examining the resulting binary in a hex editor to ensure your strings are actually being included - if they don't print but you see them in the binary, that means the section is present but the linker thinks it will be loaded at a different address. If you don't see them at all, there's a problem with the linker script. (I haven't tested the example linker script, so it may not be correct!)
abstractmath
Member
Member
Posts: 46
Joined: Mon Sep 07, 2020 5:50 pm

Re: FPU initialization

Post by abstractmath »

That fixed both issues, even with just copy/pasting that linker script into my code! Thanks a ton!
Post Reply