CRT and FTOL

Programming, for all ages and all languages.
Post Reply
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

CRT and FTOL

Post by mark3094 »

Hi,

I've been trying to work out what _ftol2 and _ftol2_sse are used for

I'm starting with my Kernel, and have created some basic C library functions, including a fully functional printf. I have tested the printf in a normal Visual Studio project, and it works fine.

However, when I setup a project to start a Kernel, and remove built in CRT, etc, it doesn't compile, due to missing external symbols.

I have gotten most of these sorted out. The only ones remaining are _ftol2, _ftol2_sse and _fltused.

From my research, it looks like _ftol2_sse is the same as _ftol2, so I can ignore that for now. I have also found out that I should set _fltused like this:

Code: Select all

int _fltused = 1
but so far, that's not working

All I can find on _ftol2 is that it is used to convert a double to a long integer. Looks like the functions are declared like this:

Code: Select all

//long _ftol( double f );
//long _ftol2( double f) { return _ftol(f); }
However, I've no idea what code should go in those functions. Anyway, it looks like they rely on an _ftol function, which I also don't know anything about.

Can someone help me with this. For starters, I need to know accurately what these functions do. Maybe even some sample code if possible.

Thankyou all for your help
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: CRT and FTOL

Post by Combuster »

I see a bad habit of creating new threads for the same problems. Please provide a proper follow-up so that others can solve the problem as well.
"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 ]
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: CRT and FTOL

Post by mark3094 »

This is not entirely the same problem

That thread mentions ftol, etc, but is mainly related to other functions, such as aulldiv, which I show in my follow up, with links to code found on the Microsoft Research page.
EDIT: For clarity, I have posted on the other thread, pointing out that the long integer functions are complete... Thanks to all who helped on this

Any ideas on the ftol specific issues?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: CRT and FTOL

Post by neon »

Hello,

int _fltused = 1 is correct. I believe ftol2 would be the following:

Code: Select all

long __declspec (naked) _ftol2_sse() {
	int a;
	_asm {
		fistp [a]
		mov	ebx, a
		ret
	}
}
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: CRT and FTOL

Post by mark3094 »

Thanks Neon, that definitely gives me something to work with.

Would there be anything special to declare it as a C function instead of a C++ function?
And, is _ftol_sse the same function as I suspect?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: CRT and FTOL

Post by neon »

Hello,

It should be defined as a C function to insure proper linking. Also the code provided was _ftol_sse from the previous version of our CRT; _ftol is indeed basically the same.

(*weird, double post but hit Submit only once.)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: CRT and FTOL

Post by Owen »

Make sure you set the correct rounding mode on the FPU in order to get standard conforming rounding behavior.
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: CRT and FTOL

Post by mark3094 »

I've tried declaring these (such as int _fltused = 1;) in a header called hal.h, which is included in several .c files, including main.c and printf.c (as well as some others).
I've started the file with #ifndef, etc, but I still get this error when compiling:

C-Lib.lib(printf.obj) : error LNK2005: __fltused already defined in main.obj
C-Lib.lib(printf.obj) : error LNK2005: __ftol2 already defined in main.obj
C-Lib.lib(printf.obj) : error LNK2005: __ftol2_sse already defined in main.obj
C-Lib.lib(putchar.obj) : error LNK2005: __fltused already defined in main.obj
C-Lib.lib(putchar.obj) : error LNK2005: __ftol2 already defined in main.obj
C-Lib.lib(putchar.obj) : error LNK2005: __ftol2_sse already defined in main.obj
HAL.lib(console.obj) : error LNK2005: __fltused already defined in main.obj
HAL.lib(console.obj) : error LNK2005: __ftol2 already defined in main.obj
HAL.lib(console.obj) : error LNK2005: __ftol2_sse already defined in main.obj


There must be something really simple that I'm overlooking, but so far I can't see it...
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: CRT and FTOL

Post by mark3094 »

Solved it.
I was declaring the int in the header when it should have been in a separate .c file

I now have hal.h:

Code: Select all

long _ftol2();
long _ftol2_sse();

And ftol.c:

Code: Select all

int _fltused = 1;


long __declspec (naked) _ftol2_sse() {
   int a;
   _asm {
      fistp [a]
      mov   ebx, a
      ret
   }
}


long __declspec (naked) _ftol2() {
   int a;
   _asm {
      fistp [a]
      mov   ebx, a
      ret
   }
}
I have not yet gotten round to setting the rounding on the FPU, as I will have to figure that out from scratch too. I'll do some further testing on what I've done so far then finish off with that.


Thankyou everyone
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: CRT and FTOL

Post by mark3094 »

Just another follow up. I didn't have much luck with the ftol code above, but I did find this code:

Code: Select all

fnstcw  word ptr [esp-2]
mov     ax, word ptr [esp-2]
or      ax, 0C00h
mov     word ptr [esp-4], ax
fldcw   word ptr [esp-4]
fistp   qword ptr [esp-12]
fldcw   word ptr [esp-2]
mov     eax, dword ptr [esp-12]
mov     edx, dword ptr [esp-8]
ret
On this site:

http://www.jbox.dk/sanos/source/lib/math/ftol.asm.html


Hopefully this helps someone else out in future
Post Reply