OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Apr 15, 2024 3:31 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Is it possible to use float and double without setting FPU?
PostPosted: Wed Feb 28, 2024 2:07 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
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?


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Wed Feb 28, 2024 3:55 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
https://wiki.osdev.org/FPU

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


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Wed Feb 28, 2024 4:44 pm 
Offline
Member
Member
User avatar

Joined: Mon May 22, 2017 5:56 am
Posts: 815
Location: Hyperspace
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


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Wed Feb 28, 2024 9:12 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5134
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.


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Thu Feb 29, 2024 1:33 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
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?


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Thu Feb 29, 2024 2:30 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
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


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Sat Mar 02, 2024 3:49 am 
Offline
Member
Member

Joined: Sun Apr 21, 2019 7:39 am
Posts: 76
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

_________________
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.


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Sat Mar 02, 2024 3:03 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
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?


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Sat Mar 02, 2024 3:12 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Sat Mar 02, 2024 4:01 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
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.


Top
 Profile  
 
 Post subject: Re: Is it possible to use float and double without setting F
PostPosted: Sat Mar 02, 2024 6:48 pm 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 402
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 37 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group