very low-level fraction convertions

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
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

very low-level fraction convertions

Post by mariuszp »

I'm using a double in my kernel code, and I need to convert it to an int. The standard type-casting does not seem to work (or am I wrong?). How do I convert a double into an int (I want to remove the fraction part). Or perhaps someone knows how else I could improve the following code:

Code: Select all

/*

splash.h
Atom user-friendly loading screen.

When kernel debugging is disabled, a more
user-friendly loading screen (known as a splash
screen) is displayed to let the user know that
something is happening (a black screen may cause
users to lose patience).

*/

#ifndef _SPLASH_H
#define _SPLASH_H

#include <atom/kernel.h>
#include <atom/konsole.h>

#include <config/splash.conf>

#define SLD_XOFF	(80/2)-(SLD_WIDTH/2)
#define	SLD_XEND	(80/2)+(SLD_WIDTH/2)

typedef struct splash_action_struct
{
	void (*callback)(int);
	int arg;
} splash_action_t;

double splash_single_len;

double splash_count_actions(splash_action_t actions[])
{
	double count=0;
	int i=0;
	while (1)
	{
		if (actions[i++].callback == NULL) return count;
		else count += 1.0;
	};
};

void splash_draw_bar(int x, int y, int width, u8int attr)
{
	unsigned char * vidmem = (unsigned char *) 0xb8000;
	int loc = (((y * width) + x) * 2) + 1;
	printdec(loc);
	while (width--)
	{
		vidmem[loc] = attr;
		loc += 2;
	};
	putk(' ');
};

void splash_init(splash_action_t actions[])
{
	konsole_clear(SLD_ATTR);
	splash_single_len = (1.0/splash_count_actions(actions))*((float)SLD_WIDTH);
	int textoff = (80/2)-(strlen(SLD_MSG)/2);
	konsole_status.x = textoff;
	konsole_status.y = 18;
	printk(SLD_MSG);
	splash_draw_bar(SLD_XOFF, 80, SLD_WIDTH, SLD_BACK);

	int i;
	double barwidth=0;
	for (i=0; i<splash_count_actions(actions); i++)
	{
		splash_draw_bar(SLD_XOFF, 80, barwidth, SLD_COLOR);
		actions[i].callback(actions[i].arg);
		barwidth += splash_single_len;
	};
};

#endif
Any help?

EDIT: Oh yeah, I forgot... i'll post a screenshot to show what this 'masterpiece' does:
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: very low-level fraction convertions

Post by bluemoon »

1. You can avoid all the doubles by rearrange the arithmetic with integer operations.

2. Since you're running your own kernel, chances is that FPU is not initialized yet. Try FINIT to put it in good state. (note that I'd never used double in my kernel so I'm not so sure on this).
The standard type-casting does not seem to work (or am I wrong?)
How it does not work? can't compile, link, calculate wrong values, or it reset the CPU upon execute?
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: very low-level fraction convertions

Post by mariuszp »

bluemoon wrote:1. You can avoid all the doubles by rearrange the arithmetic with integer operations.

2. Since you're running your own kernel, chances is that FPU is not initialized yet. Try FINIT to put it in good state. (note that I'd never used double in my kernel so I'm not so sure on this).
The standard type-casting does not seem to work (or am I wrong?)
How it does not work? can't compile, link, calculate wrong values, or it reset the CPU upon execute?
Well, maybe the doubles work, but then why do all the magenta bars appear away from the background bar? Is there something wrong with my code? It appears to work when I directly pass an int to the splash_draw_bar() function.
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Re: very low-level fraction convertions

Post by Karlosoft »

Code: Select all

splash_draw_bar(SLD_XOFF, 80, barwidth, SLD_COLOR);
why are you calling it with 80 like y value?
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: very low-level fraction convertions

Post by mariuszp »

Because there's obviously something wrong with it. When I put 20 it appears above the "Atom loading..." message. Which is weird. Maybe it's got something to do with the error anyway?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: very low-level fraction convertions

Post by Combuster »

You seem to be using the bar width where you should be using the screen width (pitch) - those are two different numbers.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: very low-level fraction convertions

Post by mariuszp »

Combuster wrote:You seem to be using the bar width where you should be using the screen width (pitch) - those are two different numbers.
Crap, LOL that's it :D:D:D:D:D
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Re: very low-level fraction convertions

Post by Karlosoft »

And if I were you I'd like to control if x and y are values in the range, and I'd also add an empty char because if the screen is already filled you will see some colored text.
Post Reply