OSDev.org
https://forum.osdev.org/

Is it possible to use float and double without setting FPU?
https://forum.osdev.org/viewtopic.php?f=1&t=57138
Page 1 of 1

Author:  mrjbom [ Wed Feb 28, 2024 2:07 pm ]
Post subject:  Is it possible to use float and double without setting FPU?

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?

Author:  iansjack [ Wed Feb 28, 2024 3:55 pm ]
Post subject:  Re: Is it possible to use float and double without setting F

https://wiki.osdev.org/FPU

But why would you need to use floating-point calculations when booting an operating system?

Author:  eekee [ Wed Feb 28, 2024 4:44 pm ]
Post subject:  Re: Is it possible to use float and double without setting F

We can help convert your calculations to integer ones, if you'd like.

Author:  Octocontrabass [ Wed Feb 28, 2024 9:12 pm ]
Post subject:  Re: Is it possible to use float and double without setting F

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.

Author:  mrjbom [ Thu Feb 29, 2024 1:33 am ]
Post subject:  Re: Is it possible to use float and double without setting F

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:
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:
m->remap = m->capacity / 4 * 3

But can it be done any better? So that I can have different load_fac values, for example?

Author:  iansjack [ Thu Feb 29, 2024 2:30 am ]
Post subject:  Re: Is it possible to use float and double without setting F

The integer calculation
Code:
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:
m->remap = (m->capacity*m->load->fac) / 100

Author:  iProgramInCpp [ Sat Mar 02, 2024 3:49 am ]
Post subject:  Re: Is it possible to use float and double without setting F

mrjbom wrote:
Specifically this calculation I could replace with something like:
Code:
m->remap = m->capacity / 4 * 3


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

Author:  mrjbom [ Sat Mar 02, 2024 3:03 pm ]
Post subject:  Re: Is it possible to use float and double without setting F

iProgramInCpp wrote:
mrjbom wrote:
Specifically this calculation I could replace with something like:
Code:
m->remap = m->capacity / 4 * 3


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

Why?

Author:  iansjack [ Sat Mar 02, 2024 3:12 pm ]
Post subject:  Re: Is it possible to use float and double without setting F

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:
8 * (75 / 100) = 0
whereas
Code:
(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.

Author:  mrjbom [ Sat Mar 02, 2024 4:01 pm ]
Post subject:  Re: Is it possible to use float and double without setting F

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:
8 * (75 / 100) = 0
whereas
Code:
(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:
m->remap = m->capacity / 4 * 3
with
Code:
m->remap = m->capacity * 3 * 4
which I did not understand.

Author:  thewrongchristian [ Sat Mar 02, 2024 6:48 pm ]
Post subject:  Re: Is it possible to use float and double without setting F

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:
8 * (75 / 100) = 0
whereas
Code:
(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:
m->remap = m->capacity / 4 * 3
with
Code:
m->remap = m->capacity * 3 * 4
which I did not understand.


No.

The suggested code was:

Code:
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.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/