News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

if you are forced to use only C/C++ on a project...

Started by daydreamer, August 22, 2019, 12:15:19 AM

Previous topic - Next topic

daydreamer

Hi
what should you do if you are forced to use only C/C++ code on a project and must produce readable code without assembly and other advanced functions
and you need it to perform better
should you choose a different and better compiler?(tested to produce faster code on same source with fastest settings)
and if so why just that?
and use knowledge from assembly to make things faster

I think I want to test clang

my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

TimoVJL

If you want to have a same source file for 32/64 bit, C is for it.
gcc and clang have inline assembler support for x64.
clang is a free option, but without CRT and without standard include files and libraries, but have support for MS PDB.
gcc don't have support for MS PDB.

FORCED ?, better to use best compiler/tools for the project.

May the source be with you

Vortex


aw27

My take on this is that when we need to use ASM with C or any other high-level language, it is almost always better to use a separate ASM module. The main reason is that, except for very basic things, such as reversing or rotating bits, inline ASM is cumbersome when compared to full fledged ASM. Sure, it will require a call to the module while inline does not, but does it really weight that much in the final computation? Probably not.

Some people believe that C can replace ASM in every instance. It can not, intrinsics are a panacea. When using vector instructions or looking for extreme performance rotines like memory move, better use real ASM.

hutch--

Every tool has its place, a C compiler can produce very good code in skilled hands but it can also produce garbage when not well written. The main bugbear with C/C++ are its runtime libraries, an experienced C programmer knows how to navigate the available range where an amateur will produce bloated garbage.

Now if you write in MASM, who cares.  :rofl:

TimoVJL

C/C++ users should know, that masm32 have a msvcrt.lib for msvcrt.dll without VC startup routines.
Now we just have to learn to use them, so we can share simple examples without VC CRT, for example that vcruntime140.dll, what isn't part of Windows OS.

For ANSI console program:
#pragma comment(lib, "msvcrt.lib")
#if _MSC_VER >= 17
# ifdef _WIN64
#  pragma comment(linker,"/subsystem:console,5.2")
# else
#  pragma comment(linker,"/subsystem:console,5.1")
# endif
#endif
void __cdecl mainCRTStartup(void)
{
__declspec(dllimport) int __cdecl __getmainargs(int*, char***, char***, int, void*);
__declspec(dllimport) void __cdecl exit(int status);
int __cdecl main(int argc, char **argv);

int    argc;
char** argv;
char** env;
int    si = 0;

__getmainargs(&argc,&argv,&env,0,&si);
exit(main(argc,argv));
}
May the source be with you

daydreamer

thanks everyone for your answers,I read clang has both cl and gcc compability mode
its nice to be able to use inline asm also in 64bit mode,also good to use clang because it seems to produce faster code
AW
I want to make a library of this:
working on a 4xsincostanarcsinarccos PROC,which I want to add jumptable and ifs,so you can choose one or several parts of it
also read about C++ argument against inline asm,it can break compilers optimizating on the rest of the code,dont know if its truth in it


my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

jcfuller

Quote from: TimoVJL on August 22, 2019, 09:47:52 PM
C/C++ users should know, that masm32 have a msvcrt.lib for msvcrt.dll without VC startup routines.
Now we just have to learn to use them, so we can share simple examples without VC CRT, for example that vcruntime140.dll, what isn't part of Windows OS.
Problem is msvcrt.dll is not c99 compliant.
You need to fine a way to access ucrtbase.dll which vc has used since VS2015 I believe.
Not sure how Pelles does it?
This link describes one problem:
https://www.oxygenbasic.org/forum/index.php?topic=1947.0

James

hutch--

There is in fact a good reason for the MASM32/64 versions of MSVCRT, the DLL is part of the system so its functions have been available for a very long time. The VC initialisations are no real use in assembler and are easy enough to code in any case so they are not part of those two import libraries. You can go the path of having to add DLLs for C compilers but surely it defeats the purpose of making stand alone executable files unless you want to create your own DLLs which is a design choice, not a runtime DLL.

TimoVJL

Pelles C don't use ucrtbase.dll

ucrt isn't ready yet, it don't have printf, but VC have inline code for it and an additional library for it.

PellesC test with ucrtint main(int argc, char **argv);

void __cdecl mainCRTStartup(void)
{
//_initialize_narrow_environment();
char* pcml[] = {0,_get_narrow_winmain_command_line()};
stdin  = __acrt_iob_func(0);
stdout = __acrt_iob_func(1);
stderr = __acrt_iob_func(2);

exit(main(0, pcml));
}
May the source be with you

aw27

C/C++ programmers, always pray for users to have all redistributables. What runs behind the scenes is a real mess, even experts have trouble understanding it.

But most of this discussion is indeed useless for Masm programmers that produce Masm standalone applications. Fortunately, msvcrt.dll provides everything needed. Things sometimes become tricky when mixing C/C++ and ASM, but the general rule is to use the same libraries/DLLs already used in the high-level part (C/C++).


daydreamer

but there is both cons and pros with DLL's,isnt one problem if you use big arrays,you get an additional memcopy between dll library and main program of those?

my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

aw27

Quote from: daydreamer on August 23, 2019, 08:37:04 PM
but there is both cons and pros with DLL's,isnt one problem if you use big arrays,you get an additional memcopy between dll library and main program of those?

I don't think I understand what you mean, but the DLL once loaded is in the same memory space as the main program, no additional memcopy is needed.

jj2007

Quote from: jcfuller on August 23, 2019, 07:46:33 PMProblem is msvcrt.dll is not c99 compliant.

James,

Excuse my ignorance, but what exactly is the problem there?

QuoteI received an email pointing out a problem with the time functions which I verified.
Output using vs2019 on Win 10 and gcc on Linux:
Code:
Mon Aug  5 14:52:11 2019
Today is Monday, August 05.
The time is 02:52 PM.
ISO week day is 2019W321.

Output from NUWEN and my bc9/TCLib:
Code:
Mon Aug 05 15:18:27 2019
Today is Monday, August 05.
The time is 03:18 PM.

TimoVJL

#14
Someone has found one way to crash msvcrt.dll printf strftime :rofl:
Pelles C
Fri Aug 23 14:57:32 2019
Today is Friday, August 23.
The time is 02:57 PM.
ISO week day is 2019W345.

msvcrt.dll
Fri Aug 23 15:01:31 2019
Today is Friday, August 23.
The time is 03:01 PM.

ucrtbase.dll
Fri Aug 23 15:02:52 2019
Today is Friday, August 23.
The time is 03:02 PM.
ISO week day is 2019W345.
Quote from: jcfuller on August 23, 2019, 07:46:33 PM
Problem is msvcrt.dll is not c99 compliant.
Of course it isn't, as M$ started to conform C99 somewhere 2012, it just took so long.
May the source be with you