News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

No doubles in C?

Started by jj2007, May 22, 2011, 05:08:12 PM

Previous topic - Next topic

jj2007

I am stuck with a lousy little C++ problem:

Quote#include <stdio.h>
// #include <stdlib.h> // no effect

extern "C"
void __stdcall fooBar();

double mydb=123.456;  // works

int jjInt (int jj)     // works
  {
   return jj;
  }

int jjDouble (double jj)     // fails with error LNK2019: unresolved external symbol __ftol2_sse referenced in function "int __cdecl jjDouble(double)" (?jjDouble@@YAHN@Z)
{
return jj;
  }

void __stdcall  fooBar()
  {
   printf("fooBar called successfully\n");
  }

Any ideas? Command line is
"%ProgramFiles%\Microsoft Visual Studio 9.0\VC\bin\cl.exe" /c /Zl /Fa /Fp FooBar.cpp

drizz

int jjDouble (double jj) {
return jj; // Implicit typecast of double to int
}


http://www.masm32.com/board/index.php?topic=15446.msg126452#msg126452
http://www.masm32.com/board/index.php?topic=15126.msg122556#msg122556

Since it complains about __ftol2_sse, does that mean that -arch:SSE is default in VS >= 9.0 ?


The solution is to pass "%ProgramFiles%\Microsoft Visual Studio 9.0\VC\lib\libc[mt].lib" to linker command line.
BUT
Mixing masm32 stuff with much newer VS components is a BAD idea, unless you know what you are doing.


The truth cannot be learned ... it can only be recognized.

jarekfall

Quote from: drizzSince it complains about __ftol2_sse, does that mean that -arch:SSE is default in VS >= 9.0 ?

In my MS VS 2008 (9.0) on AMD K6-2 processor this option is disabled by default, thus I think it depends on used processor.

jj2007, please make sure whether you have specified a value for an option "Enable Enhanced Instruction Set" in project settings.

jj2007

Thanks, drizz and jarek.
/QIfist helped to get rid of "unresolved external symbol __ftol2_sse", but I still can't call it.
Right now I am downloading service pack 3 for my XP, so that I can install VC Express properly...

drizz

See if you are interested in these trunc procs that I wrote.

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
fast_real4_trunc proc C uses ebx r4:real4
; ----- SEEEEEEEEFFFFFFFFFFFFFFFFFFFFFFF
mov edx,[esp+4]; r4
mov eax,1
push ebx
mov ecx,edx
mov ebx,edx
and ecx,01111111100000000000000000000000b
and edx,00000000011111111111111111111111b
sub ecx,127 shl 23
; 1.FFFFFFFFFFFFFFFFFFFFFFF
sar ebx,31
shl edx,9
shr ecx,23
shld eax,edx,cl
xor eax,ebx
sub eax,ebx
pop ebx
ret
fast_real4_trunc endp

fast_real8_trunc proc C uses ebx r8:real8
push ebx
mov eax,[esp+2*4][4]
mov ebx,[esp+1*4][4]
mov ecx,eax
cdq
and ecx,01111111111100000000000000000000b
and eax,00000000000011111111111111111111b
sub ecx,1023 shl 20
; 1.FFFFFFFFFFFFFFFFFFFFFFF
shld eax,ebx,12
mov ebx,eax
shr ecx,20
mov eax,1
shld eax,ebx,cl
xor eax,edx
sub eax,edx
pop ebx
ret
fast_real8_trunc endp

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF
The truth cannot be learned ... it can only be recognized.

jj2007

Quote from: drizz on May 22, 2011, 10:56:14 PM
See if you are interested in these trunc procs that I wrote.

As a new function for MasmBasic? Yes, that does look interesting - although the Forum's 256k limit is giving me headaches... ;-)
fld FP4(-123.456)
push eax
fistp dword ptr [esp]
pop eax
Print Str$("Trunc by FPU=%i\n", eax)

This works, too, but it requires that the rounding mode be set correctly. Your solution is independent of that. I wonder how one could test it with a range of floats as input ::)

GregL

jj,
If you have SSE3 support you can use FISTTP for truncating floating-point. It ignores the rounding mode and always uses "chop".  Check it out.

Drizz,
Your method looks good. Your stuff is always good Drizz. How well is it tested?




jj2007

Quote from: drizz on May 22, 2011, 05:17:20 PM
The solution is to pass "%ProgramFiles%\Microsoft Visual Studio 9.0\VC\lib\libc[mt].lib" to linker command line.

The linker does find the library but complains a lot:
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
libcmt.lib(a_env.obj) : error LNK2019: unresolved external symbol __imp__GetEnvironmentStrings@0 referenced in function ___crtGetEnvironmentStringsA


Strangely enough, everything is fine with a simple
include \masm32\include\masm32rt.inc
includelib libcmt.lib

drizz

Quote from: jj2007 on June 06, 2011, 09:42:45 AMThe linker does find the library but complains a lot:
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
libcmt.lib(a_env.obj) : error LNK2019: unresolved external symbol __imp__GetEnvironmentStrings@0 referenced in function ___crtGetEnvironmentStringsA


Strangely enough, everything is fine with a simple
include \masm32\include\masm32rt.inc
includelib libcmt.lib


Exactly what I meant with :

Quote from: drizz on May 22, 2011, 05:17:20 PMMixing masm32 stuff with [much newer] VS components is a BAD idea, unless you know what you are doing.

1) USE "\VC\PlatformSDK\Lib" and not "\masm32\lib"
2) WinMain/main
3) GetEnvironmentStrings

Quote from: GregL on June 06, 2011, 01:31:39 AMYour method looks good. Your stuff is always good Drizz. How well is it tested?
It should work for all 32-bit signed integers (needs test for 0). It's faster than fstcw fldcw, but slower than SSE 1,2 CVTTS[S,D]2SI.
The truth cannot be learned ... it can only be recognized.

jj2007

Quote from: drizz on June 06, 2011, 10:26:52 AM
Exactly what I meant with :

Quote from: drizz on May 22, 2011, 05:17:20 PMMixing masm32 stuff with [much newer] VS components is a BAD idea, unless you know what you are doing.

My version works just fine, but thanks anyway.

GregL

Quote from: drizzIt's faster than fstcw fldcw, but slower than SSE 1,2 CVTTS[S,D]2SI.

I had forgotten about CVTTSS2SI and CVTTSD2SI. I use them my personal library though, along with FISTTP.  Just check the support and use the fastest one you can.