Page 1 of 1
Is it possible to use float and double without setting FPU?
Posted: Wed Feb 28, 2024 2:07 pm
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?
Re: Is it possible to use float and double without setting F
Posted: Wed Feb 28, 2024 3:55 pm
by iansjack
https://wiki.osdev.org/FPU
But why would you need to use floating-point calculations when booting an operating system?
Re: Is it possible to use float and double without setting F
Posted: Wed Feb 28, 2024 4:44 pm
by eekee
We can help convert your calculations to integer ones, if you'd like.
Re: Is it possible to use float and double without setting F
Posted: Wed Feb 28, 2024 9:12 pm
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.
Re: Is it possible to use float and double without setting F
Posted: Thu Feb 29, 2024 1:33 am
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:
But can it be done any better? So that I can have different load_fac values, for example?
Re: Is it possible to use float and double without setting F
Posted: Thu Feb 29, 2024 2:30 am
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
Re: Is it possible to use float and double without setting F
Posted: Sat Mar 02, 2024 3:49 am
by iProgramInCpp
mrjbom wrote:
Specifically this calculation I could replace with something like:
This would work better as a replacement for what I quoted above.
Re: Is it possible to use float and double without setting F
Posted: Sat Mar 02, 2024 3:03 pm
by mrjbom
iProgramInCpp wrote:mrjbom wrote:
Specifically this calculation I could replace with something like:
This would work better as a replacement for what I quoted above.
Why?
Re: Is it possible to use float and double without setting F
Posted: Sat Mar 02, 2024 3:12 pm
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.
whereas
The latter is obviously the result you want.
You’re looking for an integer result, so you don’t need floating-point calculations.
Re: Is it possible to use float and double without setting F
Posted: Sat Mar 02, 2024 4:01 pm
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.
whereas
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
with
which I did not understand.
Re: Is it possible to use float and double without setting F
Posted: Sat Mar 02, 2024 6:48 pm
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.
whereas
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
with
which I did not understand.
No.
The suggested code was:
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.