Page 1 of 1

very low-level fraction convertions

Posted: Mon May 23, 2011 12:46 pm
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:

Re: very low-level fraction convertions

Posted: Mon May 23, 2011 1:13 pm
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?

Re: very low-level fraction convertions

Posted: Mon May 23, 2011 1:22 pm
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.

Re: very low-level fraction convertions

Posted: Mon May 23, 2011 1:34 pm
by Karlosoft

Code: Select all

splash_draw_bar(SLD_XOFF, 80, barwidth, SLD_COLOR);
why are you calling it with 80 like y value?

Re: very low-level fraction convertions

Posted: Mon May 23, 2011 1:38 pm
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?

Re: very low-level fraction convertions

Posted: Mon May 23, 2011 1:39 pm
by Combuster
You seem to be using the bar width where you should be using the screen width (pitch) - those are two different numbers.

Re: very low-level fraction convertions

Posted: Mon May 23, 2011 1:40 pm
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

Re: very low-level fraction convertions

Posted: Mon May 23, 2011 1:44 pm
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.