Page 1 of 1

CRT and FTOL

Posted: Sun Jul 17, 2011 5:18 am
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

Re: CRT and FTOL

Posted: Sun Jul 17, 2011 9:20 am
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.

Re: CRT and FTOL

Posted: Sun Jul 17, 2011 4:05 pm
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?

Re: CRT and FTOL

Posted: Sun Jul 17, 2011 7:53 pm
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
	}
}

Re: CRT and FTOL

Posted: Sun Jul 17, 2011 9:16 pm
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?

Re: CRT and FTOL

Posted: Sun Jul 17, 2011 9:19 pm
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.)

Re: CRT and FTOL

Posted: Mon Jul 18, 2011 12:39 am
by Owen
Make sure you set the correct rounding mode on the FPU in order to get standard conforming rounding behavior.

Re: CRT and FTOL

Posted: Mon Jul 18, 2011 4:44 am
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...

Re: CRT and FTOL

Posted: Mon Jul 18, 2011 5:28 am
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

Re: CRT and FTOL

Posted: Thu Jul 21, 2011 3:31 am
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