Is it possible to use float and double without setting FPU?

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
User avatar
mrjbom
Member
Member
Posts: 315
Joined: Sun Jul 21, 2019 7:34 am

Is it possible to use float and double without setting FPU?

Post by mrjbom »

This is a very stupid question, but I thought I'd ask it nonetheless.
Now in my early initialization code I need to use double for calculations, but I'm not sure I can perform it as it seems to require the FPU to be preconfigured, is this true?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Is it possible to use float and double without setting F

Post by iansjack »

https://wiki.osdev.org/FPU

But why would you need to use floating-point calculations when booting an operating system?
User avatar
eekee
Member
Member
Posts: 891
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: Is it possible to use float and double without setting F

Post by eekee »

We can help convert your calculations to integer ones, if you'd like.
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: Is it possible to use float and double without setting F

Post by Octocontrabass »

If you really need floating-point math, there are ways to do it without using the FPU.

But you probably don't need floating-point math.
User avatar
mrjbom
Member
Member
Posts: 315
Joined: Sun Jul 21, 2019 7:34 am

Re: Is it possible to use float and double without setting F

Post by mrjbom »

iansjack wrote:But why would you need to use floating-point calculations when booting an operating system?
To calculate when I should remap the hash map.

Code: Select all

m->remap = (uint32_t)(m->capacity * ((double)m->load_fac / 100));
For example, m->capacity is 8 and m->load_fac = 75, then, I will need to increase the size of the hash map when the number of elements in it reaches 6.
Specifically this calculation I could replace with something like:

Code: Select all

m->remap = m->capacity / 4 * 3
But can it be done any better? So that I can have different load_fac values, for example?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Is it possible to use float and double without setting F

Post by iansjack »

The integer calculation

Code: Select all

m->remap = (m->capacity * m->load_fac + 99) / 100
(if you want to round up) produces the same result. If you want to round down just use

Code: Select all

m->remap = (m->capacity*m->load->fac) / 100
iProgramInCpp
Member
Member
Posts: 81
Joined: Sun Apr 21, 2019 7:39 am

Re: Is it possible to use float and double without setting F

Post by iProgramInCpp »

mrjbom wrote: Specifically this calculation I could replace with something like:

Code: Select all

m->remap = m->capacity / 4 * 3
This would work better as a replacement for what I quoted above.

Code: Select all

m->remap = m->capacity * 3 / 4
Hey! I'm developing two operating systems:

NanoShell --- A 32-bit operating system whose GUI takes inspiration from Windows 9x and early UNIX desktop managers.
Boron --- A portable SMP operating system taking inspiration from the design of the Windows NT kernel.
User avatar
mrjbom
Member
Member
Posts: 315
Joined: Sun Jul 21, 2019 7:34 am

Re: Is it possible to use float and double without setting F

Post by mrjbom »

iProgramInCpp wrote:
mrjbom wrote: Specifically this calculation I could replace with something like:

Code: Select all

m->remap = m->capacity / 4 * 3
This would work better as a replacement for what I quoted above.

Code: Select all

m->remap = m->capacity * 3 / 4
Why?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Is it possible to use float and double without setting F

Post by iansjack »

You need to do the multiplication before the division (if using integer arithmetic) to get the correct result. And it’s best to use parentheses to ensure the correct order.

Code: Select all

8 * (75 / 100) = 0
whereas

Code: Select all

(8 * 75) / 100 = 6
The latter is obviously the result you want.

You’re looking for an integer result, so you don’t need floating-point calculations.
User avatar
mrjbom
Member
Member
Posts: 315
Joined: Sun Jul 21, 2019 7:34 am

Re: Is it possible to use float and double without setting F

Post by mrjbom »

iansjack wrote:You need to do the multiplication before the division (if using integer arithmetic) to get the correct result. And it’s best to use parentheses to ensure the correct order.

Code: Select all

8 * (75 / 100) = 0
whereas

Code: Select all

(8 * 75) / 100 = 6
The latter is obviously the result you want.

You’re looking for an integer result, so you don’t need floating-point calculations.
I understand this.
He said that I should replace

Code: Select all

m->remap = m->capacity / 4 * 3
with

Code: Select all

m->remap = m->capacity * 3 * 4
which I did not understand.
thewrongchristian
Member
Member
Posts: 426
Joined: Tue Apr 03, 2018 2:44 am

Re: Is it possible to use float and double without setting F

Post by thewrongchristian »

mrjbom wrote:
iansjack wrote:You need to do the multiplication before the division (if using integer arithmetic) to get the correct result. And it’s best to use parentheses to ensure the correct order.

Code: Select all

8 * (75 / 100) = 0
whereas

Code: Select all

(8 * 75) / 100 = 6
The latter is obviously the result you want.

You’re looking for an integer result, so you don’t need floating-point calculations.
I understand this.
He said that I should replace

Code: Select all

m->remap = m->capacity / 4 * 3
with

Code: Select all

m->remap = m->capacity * 3 * 4
which I did not understand.
No.

The suggested code was:

Code: Select all

m->remap = m->capacity * 3 / 4
The reason it was suggested is because doing the division by 4 first will lose you precision. If you multiply by 3 first, you lose no precision in the intermediate result before dividing by 4.

The other good thing about the code is that the optimiser will turn this all into shifts and adds, so it'll be blazing fast.
Post Reply