Skip to contentSkip to conversation

nasm over gas?

DISCUSSION Fruhwirth Clemens 18 participants 36 messages Sep 4, 2003 – Sep 13, 2003

lore
Share & tools
lore
#0 lore raw

Hi!

I recently posted a module for twofish which implements the algorithm in assembler (http://marc.theaimsgroup.com/?l=linux-kernel&m=106210815132365&w=2)

Unfortunately the assembler used is masm. I'd like to change that. Netwide Assembler (nasm) is the assembler of my choice since it focuses on portablity and has a more powerful macro facility (macros are heavily used by 2fish_86.asm). But as I'd like to make my work useful (aim for an inclusion in the kernel) I noticed that this would be the first module to depend on nasm. Everything else uses gas.

So the question is: Is a patch which depends on nasm likely to be merged?

For more information on "what is nasm":
http://nasm.sourceforge.net/doc/html/nasmdoc1.html#section-1.1

Regards, Clemens

#1 lore raw
Quoted 5 lines · fruhwirth clemens

Hi!

I recently posted a module for twofish which implements the algorithm in assembler (http://marc.theaimsgroup.com/?l=linux-kernel&m=106210815132365&w=2)

Quoted 9 lines

Unfortunately the assembler used is masm. I'd like to change that. Netwide Assembler (nasm) is the assembler of my choice since it focuses on portablity and has a more powerful macro facility (macros are heavily used by 2fish_86.asm). But as I'd like to make my work useful (aim for an inclusion in the kernel) I noticed that this would be the first module to depend on nasm. Everything else uses gas.

So the question is: Is a patch which depends on nasm likely to be merged?

I hope no ...

Some years ago, we converted the only part of the kernel that used as86 to GNU as: see arch/i386/boot. I think this was an improvement. Using nasm for only one small piece of code would be a regression, imho.

Regards.

PS: GCC pass .S assembler source files through cpp, so you get macros expanding.

Signature · 2 lines
Yann Droneaud <[email protected]>
<[email protected]> <[email protected]>
#2 lore raw
Quoted 19 lines · Yann Droneaud
Quoted 5 lines · fruhwirth clemens

Hi!

I recently posted a module for twofish which implements the algorithm in assembler (http://marc.theaimsgroup.com/?l=linux-kernel&m=106210815132365&w=2)

Quoted 9 lines

Unfortunately the assembler used is masm. I'd like to change that. Netwide Assembler (nasm) is the assembler of my choice since it focuses on portablity and has a more powerful macro facility (macros are heavily used by 2fish_86.asm). But as I'd like to make my work useful (aim for an inclusion in the kernel) I noticed that this would be the first module to depend on nasm. Everything else uses gas.

So the question is: Is a patch which depends on nasm likely to be merged?

I hope no ...

Quoted 9 lines

Some years ago, we converted the only part of the kernel that used as86 to GNU as: see arch/i386/boot. I think this was an improvement. Using nasm for only one small piece of code would be a regression, imho.

Regards.

PS: GCC pass .S assembler source files through cpp, so you get macros expanding.

GAS also has macro capability. It's just "strange". However, it does everything MASM (/ducks/) can do. It's just strange, backwards, etc. It takes some getting used to.

If you decide to use gcc as a preprocessor, you can't use comments, NotGood(tm) because the "#" and some stuff after it gets "interpreted" by cpp.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.22 on an i686 machine (794.73 BogoMips).
Note 96.31% of all statistics are fiction.

#3 lore raw
Quoted 3 lines · Richard B. Johnson

If you decide to use gcc as a preprocessor, you can't use comments, NotGood(tm) because the "#" and some stuff after it gets "interpreted" by cpp.

Although one could use C-style comments in this scenario, yes?

Signature · 1 line
Ah bay tsay day vitamin.
#4 lore raw
Quoted 8 lines · Sean Neakums
Quoted 3 lines · Richard B. Johnson

If you decide to use gcc as a preprocessor, you can't use comments, NotGood(tm) because the "#" and some stuff after it gets "interpreted" by cpp.

Although one could use C-style comments in this scenario, yes?

Sure. Then it's not assembly. It's some polymorphic conglomeration of crap ......... don't get me started. If you write in assembler, please learn to use the assembler. Assembly is not 'C'.

Use the right tool for the right thing. Both are tools, the fact that you can shovel with an axe does not make the axe a shovel.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.22 on an i686 machine (794.73 BogoMips).
Note 96.31% of all statistics are fiction.

#5 lore raw
Quoted 10 lines · Richard B. Johnson
Quoted 7 lines · Sean Neakums
Quoted 3 lines · Richard B. Johnson

If you decide to use gcc as a preprocessor, you can't use comments, NotGood(tm) because the "#" and some stuff after it gets "interpreted" by cpp.

Quoted 8 lines
Quoted 2 lines

Although one could use C-style comments in this scenario, yes?

Sure. Then it's not assembly. It's some polymorphic conglomeration of crap ......... don't get me started. If you write in assembler, please learn to use the assembler. Assembly is not 'C'.

Comments are not useful to the assembler, it doesn't look at it ;) So being removed by cpp or by the assembler itself does not matter for me.

GAS support C style comment and there's an option to cpp to keep comment if you want comment in the assembler listing.

The preprocessor is 'just a kind of filter', you can preprocess your files through m4, sed, perl, etc ... what's the problem with that ?

Quoted 3 lines

Use the right tool for the right thing. Both are tools, the fact that you can shovel with an axe does not make the axe a shovel.

...

Signature · 2 lines
Yann Droneaud <[email protected]>
<[email protected]> <[email protected]>
#6 lore raw
In reply to Richard B. Johnson
Quoted 9 lines · Richard B. Johnson

GAS also has macro capability. It's just "strange". However, it does everything MASM (/ducks/) can do. It's just strange, backwards, etc. It takes some getting used to.

If you decide to use gcc as a preprocessor, you can't use comments, NotGood(tm) because the "#" and some stuff after it gets "interpreted" by cpp.

Yep, this is why arch/i386/boot/Makefile use -traditional flag.

In the past i rewrote all the comments with the C++ and now C99 form:
// comment
but my changes wasn't applied.

Files were hosted at http://project.meuh.eu.org/kernel/,
but they are no more on line, and i'm away from the computer where they are backed up.

Signature · 2 lines
Yann Droneaud <[email protected]>
<[email protected]> <[email protected]>
#7 lore raw
Quoted 7 lines · Yann Droneaud
Quoted 4 lines · Richard B. Johnson

GAS also has macro capability. It's just "strange". However, it does everything MASM (/ducks/) can do. It's just strange, backwards, etc. It takes some getting used to.

Quoted 7 lines
Quoted 4 lines

If you decide to use gcc as a preprocessor, you can't use comments, NotGood(tm) because the "#" and some stuff after it gets "interpreted" by cpp.

Yep, this is why arch/i386/boot/Makefile use -traditional flag.

Isn't this throwing out the baby with the bath? It makes writting a header that is use in both C and ASM all that much harder.

Signature · 3 lines
George Anzinger   [email protected]
High-res-timers:  http://sourceforge.net/projects/high-res-timers/
Preemption patch: http://www.kernel.org/pub/linux/kernel/people/rml
#8 lore raw
In reply to Yann Droneaud
Quoted 14 lines · Yann Droneaud
Quoted 13 lines · fruhwirth clemens

Hi!

I recently posted a module for twofish which implements the algorithm in assembler
(http://marc.theaimsgroup.com/?l=linux-kernel&m=106210815132365&w=2)

Unfortunately the assembler used is masm. I'd like to change that. Netwide Assembler (nasm) is the assembler of my choice since it focuses on portablity and has a more powerful macro facility (macros are heavily used by 2fish_86.asm). But as I'd like to make my work useful (aim for an inclusion in the kernel) I noticed that this would be the first module to depend on nasm. Everything else uses gas.

Quoted 8 lines
Quoted 1 line

So the question is: Is a patch which depends on nasm likely to be merged?

I hope no ...

Some years ago, we converted the only part of the kernel that used as86 to GNU as: see arch/i386/boot. I think this was an improvement. Using nasm for only one small piece of code would be a regression, imho.

Concur, not worthwhile to start using a fairly unsupported tool in the kernel.

As to using assembler, It is better to get rid of it but in special cases. Todays compilers are the better coders in 98+% of applications, and if you follow some of the discussions here on the list, you will be amazed what people do with a C compiler - all portable and much more maintainable.

I guess your code should be 80-90% C and 10-20% assmbler. This will make it up to 10 times a portable.

As to using nasm, note for gas and gcc 3.2+:

+ GAS does intel syntax too using the directive
.intel_syntax

+ GCC can do intel syntax asm output as well, tried it on some mid size apps - it works fine.

As to "abuse" of macros, macros beyond C-style pre-processing are generaly obsolete, better use C, or write an app-specific front end.

I did the latter to "preprocess" a common source, running 16 bit/embedded source through wasm and 32bit/linux source through gas.

Regards
Michael

#9 lore raw
Quoted 10 lines · Michael Frank
Quoted 2 lines · Yann Droneaud

Using nasm for only one small piece of code would be a regression, imho.

Concur, not worthwhile to start using a fairly unsupported tool in the kernel.

As to using assembler, It is better to get rid of it but in special cases. Todays compilers are the better coders in 98+% of applications, and if you follow some of the discussions here on the list, you will be amazed what people do with a C compiler - all portable and much more maintainable.

The gcc optimized code for sure much better than I do, but gcc's optimization captabilities is for sure a joke compared to the guy how wrote 2fish_86.asm (just have a look at the source). The assembler implementation is twice as fast as the C implemention we have in the kernel. Same is true for AES (although just 50% faster instead of 100%:
http://clemens.endorphin.org/patches/aes-i586-asm-2.5.58.diff . That's gas btw. )

Quoted 2 lines

I guess your code should be 80-90% C and 10-20% assmbler. This will make it up to 10 times a portable.

The Twofish code is C but has hooks to use an asm backend in special cases (keysetup, en/decrypt). But a plain C version of twofish is already present in the kernel.

Quoted 4 lines

As to using nasm, note for gas and gcc 3.2+:

+ GAS does intel syntax too using the directive
.intel_syntax

That's certainly nice to hear. At least some cut/pasting can be done :)

Regards, Clemens

#10 lore raw
In reply to Michael Frank
Quoted 5 lines · Michael Frank

Concur, not worthwhile to start using a fairly unsupported tool in the kernel.

As to using assembler, It is better to get rid of it but in special cases. Todays compilers are the better coders in 98+% of applications, and if you

Better coders? Show me the evidence.

Quoted 2 lines

follow some of the discussions here on the list, you will be amazed what people do with a C compiler - all portable and much more maintainable.

Portable yes. Maintainable yes. Better code _no_.

I'd say compiler generated asm code quality can be anywhere in between of "hair raising crawling horror" and "not so bad although I can do better".

I have never seen really clever compiler yet. Writing a good compiler is a very tough thing to do.

Signature · 1 line
vda
#11 lore raw
Quoted 13 lines · insecure
Quoted 6 lines · Michael Frank

Concur, not worthwhile to start using a fairly unsupported tool in the kernel.

As to using assembler, It is better to get rid of it but in special cases. Todays compilers are the better coders in 98+% of applications, and if you

Better coders? Show me the evidence.

Quoted 2 lines

follow some of the discussions here on the list, you will be amazed what people do with a C compiler - all portable and much more maintainable.

Quoted 7 lines

Portable yes. Maintainable yes. Better code _no_.

I'd say compiler generated asm code quality can be anywhere in between of "hair raising crawling horror" and "not so bad although I can do better".

I have never seen really clever compiler yet. Writing a good compiler is a very tough thing to do.

Just got another reply to this thread which helps to explain what I meant by "better coders in 98+% of applications"

Quoted 11 lines · Jörn Engel

How big is the .text of the asm and c variant? If the text of yours is much bigger, you just traded 2fish performance for general performance. Everything else will suffer from cache misses. Forget your microbenchmark, your variant will make the machine slower.

How many bugs are in your code? Are there any buffer overflows or other security holes? How can you be sure about it? (Most people aren't sure about c either, but it is much easier to check.)

If your code fails on any one of these questions, forget about it. If it survives them, post your results and have someone else verify them.

There is another technical argument - which I am not very familiar with:
Modern and future CPU's are optimized for high level languages, it is just too troublesome to arrange all the instructions best-case for the hardware to be well utilized.

Back to my original message, my implied definition of "Better coders" is the compromise between performance, development effort, stability and security (and more).

It does not just refer to the best possible "perfect" code.

Let me give you a example of "best possible code" (to the best of my ability):

I do mostly embedded applications, years ago did consumer design for this kind of Hong Kong made $19.99 gimmicks ($6 FOB) priced to be purchased "on impulse" by joe consumer.

For one of those gimmicks, I used a 4bit running on 1.5V with 1K instruction ROM and 64 nibbles RAM doing 32KIPS (32768 instructions per second) to establish the speed of a tennis ball it was built into. It required floating point calculations and display on a built-in LCD display with 64 segments.

This takes __clever__ __optimized__ code, and is at least a week of work, and affordable only in high-volume applications.

Consider, one week of work for what you can do in C using GLIBC within 1 hour or less!

Now, please consider a real life (linux) system, which you use everyday, of course you could make every piece of code "better" by hand coding and optimizing, but what is the real benefit?

Assuming these millions of lines of C having been implemented in optimized assembly, could it perform that much faster (if that is what you call "better"), or would you use "half" the memory for the same job?

Now, what about it's stability and maintainability - not to mention COST, even you could find all those great human coders?

Guess the pioneering days are over ;)

Regards
Michael

#12 lore raw
Quoted 8 lines · Michael Frank

Just got another reply to this thread which helps to explain what I meant by "better coders in 98+% of applications"

Quoted 4 lines · Jörn Engel

How big is the .text of the asm and c variant? If the text of yours is much bigger, you just traded 2fish performance for general performance. Everything else will suffer from cache misses. Forget your microbenchmark, your variant will make the machine slower.

A random example form one small unrelated program (gcc 3.2):

main:

        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    $32, %esp
        xorl    %ebp, %ebp
        cmpl    $1, 52(%esp)
        movl    $0, 20(%esp)
        movl    $1000000, %edi      <----
        movl    $1000000, 16(%esp)  <----
        movl    $0, 12(%esp)
        movl    $.LC27, 8(%esp)
        je      .L274
        movl    $1, %esi
        cmpl    52(%esp), %esi
        jge     .L272

No sane human will do that.

main:

        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    $32, %esp
        xorl    %ebp, %ebp
        cmpl    $1, 52(%esp)
        movl    $0, 20(%esp)
        movl    $1000000, %edi
        movl    %edi, 16(%esp)	<-- save 4 bytes
        movl    %ebp, 12(%esp)  <-- save 4 bytes
        movl    $.LC27, 8(%esp)
        je      .L274
        movl    $1, %esi
        cmpl    52(%esp), %esi
        jge     .L272

And this is only from a cursory examination.

Quoted 4 lines

There is another technical argument - which I am not very familiar with:
Modern and future CPU's are optimized for high level languages, it is just too troublesome to arrange all the instructions best-case for the hardware to be well utilized.

You took marketspeak too seriously.

Quoted 29 lines

Back to my original message, my implied definition of "Better coders" is the compromise between performance, development effort, stability and security (and more).

It does not just refer to the best possible "perfect" code.

Let me give you a example of "best possible code" (to the best of my ability):

I do mostly embedded applications, years ago did consumer design for this kind of Hong Kong made $19.99 gimmicks ($6 FOB) priced to be purchased "on impulse" by joe consumer.

For one of those gimmicks, I used a 4bit running on 1.5V with 1K instruction ROM and 64 nibbles RAM doing 32KIPS (32768 instructions per second) to establish the speed of a tennis ball it was built into. It required floating point calculations and display on a built-in LCD display with 64 segments.

This takes __clever__ __optimized__ code, and is at least a week of work, and affordable only in high-volume applications.

Consider, one week of work for what you can do in C using GLIBC within 1 hour or less!

Now, please consider a real life (linux) system, which you use everyday, of course you could make every piece of code "better" by hand coding and optimizing, but what is the real benefit?

Quoted 8 lines

Assuming these millions of lines of C having been implemented in optimized assembly, could it perform that much faster (if that is what you call "better"), or would you use "half" the memory for the same job?

Now, what about it's stability and maintainability - not to mention COST, even you could find all those great human coders?

Guess the pioneering days are over ;)

What gives you an impression that anyone is going to rewrite linux in asm? I _only_ saying that compiler-generated asm is not 'good'. It's mediocre. Nothing more. I am not asm zealot.

Signature · 1 line
vda
#13 lore raw
Quoted 3 lines · insecure

What gives you an impression that anyone is going to rewrite linux in asm? I _only_ saying that compiler-generated asm is not 'good'. It's mediocre.

Depends. A couple weeks back, I've entered the Linuxtag coding contest with a friend. The objective was to optimize some matrix multiplication. We've produced *much* faster code than anyone who tried to do it in assembler and we entered the contest during a slow hour, when it was half over.

Given infinite developer time, you can create better assembler code than the compiler can. But with limited time, it is a challenge. Plus the code isn't automagically updated to new cpus simply by recompiling.

So in the real world, compiler generated assembler is not perfect, but it is still faster than what most human would come up with even if they had the time.

Jörn

Signature · 4 lines
Public Domain  - Free as in Beer
General Public - Free as in Speech
BSD License    - Free as in Enterprise
Shared Source  - Free as in "Work will make you..."
#14 lore raw
Quoted 10 lines · Jörn Engel
Quoted 3 lines · insecure

What gives you an impression that anyone is going to rewrite linux in asm? I _only_ saying that compiler-generated asm is not 'good'. It's mediocre.

Depends. A couple weeks back, I've entered the Linuxtag coding contest with a friend. The objective was to optimize some matrix multiplication. We've produced *much* faster code than anyone who tried to do it in assembler and we entered the contest during a slow hour, when it was half over.

Can I see the code?
--
vda

#15 lore raw
In reply to insecure
Quoted 51 lines · insecure
Quoted 8 lines · Michael Frank

Just got another reply to this thread which helps to explain what I meant by "better coders in 98+% of applications"

Quoted 4 lines · Jörn Engel

How big is the .text of the asm and c variant? If the text of yours is much bigger, you just traded 2fish performance for general performance. Everything else will suffer from cache misses. Forget your microbenchmark, your variant will make the machine slower.

A random example form one small unrelated program (gcc 3.2):

main:

        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    $32, %esp
        xorl    %ebp, %ebp
        cmpl    $1, 52(%esp)
        movl    $0, 20(%esp)
        movl    $1000000, %edi      <----
        movl    $1000000, 16(%esp)  <----
        movl    $0, 12(%esp)
        movl    $.LC27, 8(%esp)
        je      .L274
        movl    $1, %esi
        cmpl    52(%esp), %esi
        jge     .L272

No sane human will do that.

main:

        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    $32, %esp
        xorl    %ebp, %ebp
        cmpl    $1, 52(%esp)
        movl    $0, 20(%esp)
        movl    $1000000, %edi
        movl    %edi, 16(%esp)	<-- save 4 bytes
        movl    %ebp, 12(%esp)  <-- save 4 bytes
        movl    $.LC27, 8(%esp)
        je      .L274
        movl    $1, %esi
        cmpl    52(%esp), %esi
        jge     .L272
Quoted 1 line

And this is only from a cursory examination.

Actually it is no as simple as that. With the instruction that uses %edi following immediately after the instruction that populates it you cannot execute those two instructions in parallel. So the code may be slower. The exact rules depend on the architecture of the cpu.

Quoted 3 lines

What gives you an impression that anyone is going to rewrite linux in asm? I _only_ saying that compiler-generated asm is not 'good'. It's mediocre. Nothing more. I am not asm zealot.

I think I would agree with that statement most compiler-generated assembly code is mediocre in general. At the same time I would add most human generated assembly is poor, and a pain to maintain.

If you concentrate on those handful of places where you need to optimize that is reasonable. Beyond that there simply are not the developer resources to do good assembly. And things like algorithmic transformations in assembly are an absolute nightmare. Where they are quite simple in C.

And if the average generated code quality bothers you enough with C the compiler can be fixed, or another compiler can be written that does a better job, and the benefit applies to a lot more code.

Eric

#16 lore raw
Quoted 4 lines · Eric W. Biederman

Actually it is no as simple as that. With the instruction that uses %edi following immediately after the instruction that populates it you cannot execute those two instructions in parallel. So the code may be slower. The exact rules depend on the architecture of the cpu.

I remember inserting a "nop" into a loop and it went significantly faster on a Pentium Pro :)

Quoted 5 lines

If you concentrate on those handful of places where you need to optimize that is reasonable. Beyond that there simply are not the developer resources to do good assembly. And things like algorithmic transformations in assembly are an absolute nightmare. Where they are quite simple in C.

If we had enough developer resources to write the whole thing in good assembly, then for _sure_ we'd have enough to write a perfect compiler!

I would argue that the most powerful algorithmic transformations are a nightmare in C, too. Less so, though.

-- Jamie

#17 lore raw
Quoted 8 lines · Jamie Lokier
Quoted 4 lines · Eric W. Biederman

Actually it is no as simple as that. With the instruction that uses %edi following immediately after the instruction that populates it you cannot execute those two instructions in parallel. So the code may be slower. The exact rules depend on the architecture of the cpu.

I remember inserting a "nop" into a loop and it went significantly faster on a Pentium Pro :)

My example in _not_ a loop, far from it. That's the point. GCC thinks everything is a loop.

Quoted 8 lines
Quoted 5 lines

If you concentrate on those handful of places where you need to optimize that is reasonable. Beyond that there simply are not the developer resources to do good assembly. And things like algorithmic transformations in assembly are an absolute nightmare. Where they are quite simple in C.

If we had enough developer resources to write the whole thing in good assembly, then for _sure_ we'd have enough to write a perfect compiler!

Peace, Jamie. I do _not_ advocate using asm anywhere except speed critical code.

Signature · 1 line
vda
#18 lore raw
In reply to Eric W. Biederman
Quoted 52 lines · Eric W. Biederman
Quoted 51 lines · insecure
Quoted 8 lines · Michael Frank

Just got another reply to this thread which helps to explain what I meant by "better coders in 98+% of applications"

Quoted 4 lines · Jörn Engel

How big is the .text of the asm and c variant? If the text of yours is much bigger, you just traded 2fish performance for general performance. Everything else will suffer from cache misses. Forget your microbenchmark, your variant will make the machine slower.

A random example form one small unrelated program (gcc 3.2):

main:

        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    $32, %esp
        xorl    %ebp, %ebp
        cmpl    $1, 52(%esp)
        movl    $0, 20(%esp)
        movl    $1000000, %edi      <----
        movl    $1000000, 16(%esp)  <----
        movl    $0, 12(%esp)
        movl    $.LC27, 8(%esp)
        je      .L274
        movl    $1, %esi
        cmpl    52(%esp), %esi
        jge     .L272

No sane human will do that.

main:

        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    $32, %esp
        xorl    %ebp, %ebp
        cmpl    $1, 52(%esp)
        movl    $0, 20(%esp)
        movl    $1000000, %edi
        movl    %edi, 16(%esp)	<-- save 4 bytes
        movl    %ebp, 12(%esp)  <-- save 4 bytes
        movl    $.LC27, 8(%esp)
        je      .L274
        movl    $1, %esi
        cmpl    52(%esp), %esi
        jge     .L272
Quoted 6 lines
Quoted 1 line

And this is only from a cursory examination.

Actually it is no as simple as that. With the instruction that uses %edi following immediately after the instruction that populates it you cannot execute those two instructions in parallel. So the code may be slower. The exact rules depend on the architecture of the cpu.

That instruction is in main() initialization sequence. I.e. it is executed once per program invocation.
Summary: we lost 8 bytes for no gain. There's not even a speed gain - we lost 8 bytes of _icache_, that will bite us somewhere else.

Quoted 7 lines
Quoted 3 lines

What gives you an impression that anyone is going to rewrite linux in asm? I _only_ saying that compiler-generated asm is not 'good'. It's mediocre. Nothing more. I am not asm zealot.

I think I would agree with that statement most compiler-generated assembly code is mediocre in general. At the same time I would add most human generated assembly is poor, and a pain to maintain.

I had an impression people think gcc generates code which is 'mostly good' even compared to handwritted code. That is not true (yet).

Signature · 1 line
vda
#19 lore raw
Quoted 4 lines · insecure

That instruction is in main() initialization sequence. I.e. it is executed once per program invocation. Summary: we lost 8 bytes for no gain. There's not even a speed gain - we lost 8 bytes of _icache_, that will bite us somewhere else.

You're quite right, but the I-Cache is a non issue: this code will be evicted when there is need to put something else. And because its only run once at the beginning of the program, it won't cause anything important to be evicted. You can complain about the time it gets to fetch the code from RAM though.

Quoting another post from you: "I do _not_ advocate using asm anywhere except speed critical code."
This code is obviously not critical. So, it makes a bad choice for discussion.

Signature · 1 line
	Ricardo
#20 lore raw
Quoted 7 lines · Ricardo Bugalho
Quoted 4 lines · insecure

That instruction is in main() initialization sequence. I.e. it is executed once per program invocation. Summary: we lost 8 bytes for no gain. There's not even a speed gain - we lost 8 bytes of _icache_, that will bite us somewhere else.

You're quite right, but the I-Cache is a non issue: this code will be

Please disable icache on your CPU ;)

Quoted 3 lines

evicted when there is need to put something else. And because its only run once at the beginning of the program, it won't cause anything important to be evicted.

How can you know that it won't evict useful code?

Quoted 2 lines

You can complain about the time it gets to fetch the code from RAM though.

Thanks for the tip. I missed that!

Quoted 4 lines

Quoting another post from you: "I do _not_ advocate using asm anywhere except speed critical code."
This code is obviously not critical. So, it makes a bad choice for discussion.

It makes perfectly fine point that gcc code is not good. It just wasted 8 bytes in a rather simple code sequence.

Signature · 1 line
vda
#21 lore raw
Quoted 2 lines · insecure
Quoted 1 line

You're quite right, but the I-Cache is a non issue: this code will be

Please disable icache on your CPU ;)

[snip]

Quoted 1 line

How can you know that it won't evict useful code?

a) the code is at the beginning of the program
b) its only run once

Therefore, its impact on i-cache is a non-issue. I wasn't disscussing the merits of a i-cache.

Quoted 4 lines
Quoted 2 lines

You can complain about the time it gets to fetch the code from RAM though.

Thanks for the tip. I missed that!

Welcome.

Quoted 2 lines

It makes perfectly fine point that gcc code is not good. It just wasted 8 bytes in a rather simple code sequence.

First of all, it would have been nice to see the relevant code and optimization flags.

I'll assume the goal is speed, not code size. Lets look at that piece of code:
a) its only run once, so it doesn't benefict of caching
b) there are no loads there, so it can't be prefetched while the CPU waits for loads
And thats what makes its performance benefict of smaller code size. But its also irrelevant to global performance. And I can't even imagin any code that meets a) and b) and is relevant for performance. In modern, general purpose, computer systems, code size is irrelevant. It has been for 15 years and its not going to change.
And I'm not going to complaint about the compiler making a bad decision on such a irrelevant case and one that is not worth re-writing in assembly.

Signature · 1 line
	Ricardo
#22 lore raw
Quoted 11 lines · Ricardo Bugalho
Quoted 1 line · insecure

How can you know that it won't evict useful code?

a) the code is at the beginning of the program
b) its only run once

Therefore, its impact on i-cache is a non-issue.

Quoted 2 lines
Quoted 2 lines

You can complain about the time it gets to fetch the code from RAM though.

Non-issue, eh? ;)

Quoted 3 lines

I'll assume the goal is speed, not code size. Lets look at that piece of code:
a) its only run once, so it doesn't benefict of caching

While it may not benefit from caching, cache misses still hurt it. And as it is only run once, there are only cache misses (ignoring the cacheline effect). Shorter code is faster code.

Quoted 2 lines

In modern, general purpose, computer systems, code size is irrelevant. It has been for 15 years and its not going to change.

- How long does you 50MB word processor take to load?
- Why do dietlibc and friends speed up server workloads?
- Why has Alan measured faster kernels with -Os than with -O2?

Code size *does* matter.

Jörn

Signature · 2 lines
Victory in war is not repetitious.
-- Sun Tzu
#23 lore raw
Quoted 3 lines · Jörn Engel

- Why has Alan measured faster kernels with -Os than with -O2?

Code size *does* matter.

That's not just i-cache pressure. It is partly a GCC problem, and it's possible -Os would run faster than -O2 even with no i-cache.

I've observed -Os emitting exactly the same code as -O2 for some trivial functions, except that -O2 has a few extra redundant instructions.

Obvious the _intent_ of -O2 is to compile for speed, but it's clear that GCC often emits trivially redundant instructions (like stack adjustments) that don't serve to speed up the program at all.

-- Jamie

#24 lore raw
Quoted 8 lines · Jamie Lokier
Quoted 3 lines · Jörn Engel

- Why has Alan measured faster kernels with -Os than with -O2?

Code size *does* matter.

That's not just i-cache pressure. It is partly a GCC problem, and it's possible -Os would run faster than -O2 even with no i-cache.

Quoted 7 lines

I've observed -Os emitting exactly the same code as -O2 for some trivial functions, except that -O2 has a few extra redundant instructions.

Obvious the _intent_ of -O2 is to compile for speed, but it's clear that GCC often emits trivially redundant instructions (like stack adjustments) that don't serve to speed up the program at all.

I haven't collected too many numbers, but the few I did collect show -O2 code actually being faster than -Os, as long as you stay in userspace and the code is small and loopy. It may get worse for large run-once code, but I don't have numbers for that.

My explanation for Alans results is that nature of kernel code. Usually, kernel code execution takes only a fraction of the cpu time, so the user code run in between effectively flushes the cache. Each system call causes near 100% cache misses, so smaller code is almost always faster.

So even if your observations were wrong and gcc would create perfect code for both -O2 and -Os, I wouldn't expect -O2 to be faster for kernel code.

Jörn

Signature · 4 lines
More computing sins are committed in the name of efficiency (without
necessarily achieving it) than for any other single reason - including
blind stupidity.
-- W. A. Wulf 
#25 lore raw
In reply to insecure
Quoted 7 lines · insecure
Quoted 6 lines · Eric W. Biederman
Quoted 4 lines · insecure

On Friday 05 September 2003 15:59, Michael Frank wrote:
What gives you an impression that anyone is going to rewrite linux in asm? I _only_ saying that compiler-generated asm is not 'good'. It's mediocre. Nothing more. I am not asm zealot.

Quoted 7 lines
Quoted 3 lines

I think I would agree with that statement most compiler-generated assembly code is mediocre in general. At the same time I would add most human generated assembly is poor, and a pain to maintain.

I had an impression people think gcc generates code which is 'mostly good' even compared to handwritted code. That is not true (yet).

It is true. Not when compared to hand optimized code. But compared to a day to day churn it is true. Although I tend to still prefer gcc 2.95 for the code size.

Eric

#26 lore raw
Quoted 1 line · Eric W. Biederman

Although I tend to still prefer gcc 2.95 for the code size.

I just compiled a small C function with GCC 3.2.2.

With -O2, it had two completely redundant stack adjustment instructions. With -Os, those instructions were gone and it was good code.

Why, oh why is -O2 still so lame after all these years? :)

-- Jamie

#27 lore raw
In reply to insecure

Hi!

Quoted 23 lines

A random example form one small unrelated program (gcc 3.2):

main:

        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    _32, %esp
        xorl    %ebp, %ebp
        cmpl    _1, 52(%esp)
        movl    _0, 20(%esp)
        movl    _1000000, %edi      <----
        movl    _1000000, 16(%esp)  <----
        movl    _0, 12(%esp)
        movl    _.LC27, 8(%esp)
        je      .L274
        movl    _1, %esi
        cmpl    52(%esp), %esi
        jge     .L272

No sane human will do that.

main:

Quoted 12 lines
        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    _32, %esp
        xorl    %ebp, %ebp
        cmpl    _1, 52(%esp)
        movl    _0, 20(%esp)
        movl    _1000000, %edi
        movl    %edi, 16(%esp)	<-- save 4 bytes
        movl    %ebp, 12(%esp)  <-- save 4 bytes
        movl    _.LC27, 8(%esp)

Hmm, but gcc version is likely faster. No sane person would write multiply by 5 using single lea instruction, yet gcc will do that...
Pavel

Signature · 2 lines
				Pavel
Written on sharp zaurus, because my Velo1 broke. If you have Velo you don't need...
#28 lore raw
In reply to insecure
Quoted 13 lines · insecure
Quoted 6 lines · Michael Frank

Concur, not worthwhile to start using a fairly unsupported tool in the kernel.

As to using assembler, It is better to get rid of it but in special cases. Todays compilers are the better coders in 98+% of applications, and if you

Better coders? Show me the evidence.

Quoted 2 lines

follow some of the discussions here on the list, you will be amazed what people do with a C compiler - all portable and much more maintainable.

Quoted 7 lines

Portable yes. Maintainable yes. Better code _no_.

I'd say compiler generated asm code quality can be anywhere in between of "hair raising crawling horror" and "not so bad although I can do better".

I have never seen really clever compiler yet. Writing a good compiler is a very tough thing to do.

Actually, you mean "writing a good optimizer is a very tough thing to do".

The problem is NOT the compiler, or the coder. A "well defined" algorithm such as the twofish mentioned CAN be compiled well by almost any compiler, but the code optimizer MUST be at the highest level. There is also the problem that what is optimum for one CPU, is NOT optimum for the next generation, even if it uses the same identical architecture. This is where the human coder can beat the compiler. That person will make many, many passes through the code, and try similar/related instructions to optimize the result. This amount of optimization also means that the result may not work at all on a different processor member of the architecture, it is MUCH more likely to run slower. Even minor things like the size of cache in the processor will affect the human coder.

This a compiler cannot do since the optimizer is targeted toward a family of processors and not a single member of that family. It will provide the most compatibility. And since it is in a higher level language, the translation to many other architectures is possible, even those that do not have available human coders. The advantage the compiler has is that the optimizer can recieve input from MANY excellent coders contributing rules for code generation. This give the compiler the ability to surpass 90+% of the coders, and exceed the productivity of all the assember coders.

The final question is "Is it fast enough?"

Only if this question is NO does it make sense to do assember. It doesn't matter if it can be faster. And portability means you don't have to speed hours rewriting it...

#29 lore raw
In reply to Michael Frank
Quoted 2 lines · Michael Frank

As to using assembler, It is better to get rid of it but in special cases. Todays compilers are the better coders in 98+% of applications

This has already been shot down pretty well but a good example of why it's not true is George Woltman's prime95 (mprime on linux) program. The core is an FFT which has been painfully rewriten several times for different cores to maximize performance. It makes extensive use of SSE2 on the P4, which I haven't seen compilers do much of. George is a great coder and he's pipelined everything so well that there really isn't much room for improvement. This assembly is several times faster than what gcc would be able to generate.

#30 lore raw
Quoted 6 lines · Aaron Lehmann
Quoted 2 lines · Michael Frank

As to using assembler, It is better to get rid of it but in special cases. Todays compilers are the better coders in 98+% of applications

isn't much room for improvement. This assembly is several times faster than what gcc would be able to generate.

You're making the rash leap of logic that "gcc" is anywhere representative of what "todays compilers" are capable of. For example, IBM recently released a new version of their 'xlc' compiler with support for the PPC970 core:

http://www.spscicomp.org/ScicomP7/Presentations/Blainey-SciComp7_compiler_update.pdf

20 pages of marketing fluff, interesting graphs on the last 2 pages. The upshot is that for the SPECint2000 and SPECfp2000 benchmark suite, IBM's xlc was easily able to generate code that ran almost twice as fast as gcc. It was particularly impressive on the eon, apsi, and sixtrack tests.

Not 10% faster code. Not 20%. *TWICE* as fast.

It usually isn't hard to hand-write code that's 20% faster than gcc's generated code. Anybody think they can consistently generate code by hand that's twice as fast?

/Valdis (who wonders if the kernel could be made xlc-compilable.. ;)

#31 lore raw
In reply to Fruhwirth Clemens
Quoted 5 lines · fruhwirth clemens

Hi!

I recently posted a module for twofish which implements the algorithm in assembler (http://marc.theaimsgroup.com/?l=linux-kernel&m=106210815132365&w=2)

Quoted 7 lines

Unfortunately the assembler used is masm. I'd like to change that. Netwide Assembler (nasm) is the assembler of my choice since it focuses on portablity and has a more powerful macro facility (macros are heavily used by 2fish_86.asm). But as I'd like to make my work useful (aim for an inclusion in the kernel) I noticed that this would be the first module to depend on nasm. Everything else uses gas.

Check those archive about the boot code rewrite.

http://www.ussg.iu.edu/hypermail/linux/kernel/9908.0/0107.html
http://www.ussg.iu.edu/hypermail/linux/kernel/9908.1/0083.html

http://www.ussg.iu.edu/hypermail/linux/kernel/9907.3/0960.html

Signature · 2 lines
Yann Droneaud <[email protected]>
<[email protected]> <[email protected]>
#32 lore raw
In reply to Fruhwirth Clemens
Quoted 12 lines · Fruhwirth Clemens

Hi!

I recently posted a module for twofish which implements the algorithm in assembler (http://marc.theaimsgroup.com/?l=linux-kernel&m=106210815132365&w=2)

Unfortunately the assembler used is masm. I'd like to change that. Netwide Assembler (nasm) is the assembler of my choice since it focuses on portablity and has a more powerful macro facility (macros are heavily used by 2fish_86.asm). But as I'd like to make my work useful (aim for an inclusion in the kernel) I noticed that this would be the first module to depend on nasm. Everything else uses gas.

Quoted 4 lines

So the question is: Is a patch which depends on nasm likely to be merged?

For more information on "what is nasm":
http://nasm.sourceforge.net/doc/html/nasmdoc1.html#section-1.1

nasm is i386-only.

gas is part of binutils and supports lots of target CPUs: I think the first part of a new gcc architecture backend is the machines description so that gas can assemble code for it.

Greets, Antonio.

#33 lore raw
In reply to Fruhwirth Clemens
Quoted 10 lines · Fruhwirth Clemens

I recently posted a module for twofish which implements the algorithm in assembler (http://marc.theaimsgroup.com/?l=linux-kernel&m=106210815132365&w=2)

Unfortunately the assembler used is masm. I'd like to change that. Netwide Assembler (nasm) is the assembler of my choice since it focuses on portablity and has a more powerful macro facility (macros are heavily used by 2fish_86.asm). But as I'd like to make my work useful (aim for an inclusion in the kernel) I noticed that this would be the first module to depend on nasm. Everything else uses gas.

Orthogonally to the nasm/gas question, there are the problems or performance and maintenance.

Do some benchmarks on lots of different machines and measure the performance of the asm and c code. If it's faster on PPro but not on PIII or Athlon, forget about it.

How big is the .text of the asm and c variant? If the text of yours is much bigger, you just traded 2fish performance for general performance. Everything else will suffer from cache misses. Forget your microbenchmark, your variant will make the machine slower.

How many bugs are in your code? Are there any buffer overflows or other security holes? How can you be sure about it? (Most people aren't sure about c either, but it is much easier to check.)

If your code fails on any one of these questions, forget about it. If it survives them, post your results and have someone else verify them.

As to nasm/gas, you will have a hard time to explain, why joe user needs yet another tool to compile his own kernel. There may be good arguments in favor, but they better be good, else translate to gas.

Jörn

Signature · 2 lines
Homo Sapiens is a goal, not a description.
-- unknown
#34 lore raw
Quoted 10 lines · Jörn Engel

On Thu, 4 September 2003 12:42:45 +0200, Fruhwirth Clemens wrote:

Do some benchmarks on lots of different machines and measure the performance of the asm and c code. If it's faster on PPro but not on PIII or Athlon, forget about it.

How big is the .text of the asm and c variant? If the text of yours is much bigger, you just traded 2fish performance for general performance. Everything else will suffer from cache misses. Forget your microbenchmark, your variant will make the machine slower.

Men! Why is everyone doubting the usefulness of assembler optimized parts? It's twice as fast on my Athlon. I assert the same is true for P3/P4. Just test.

twofish-i586.ko's .text section is smaller than the kernel's twofish.ko's. 945 bytes to be precise. Please note that twofish-i586 includes TWO
implementations: C and assembler. Just think about how much smaller it will be when I rip out the C part.

So much for that.

Quoted 1 line

How many bugs are in your code?

42... Is this a serious question?

Quoted 2 lines

Are there any buffer overflows or other security holes? How can you be sure about it?

How can you be sure? Mathematical program verification applies quite badly to assembler.

Quoted 2 lines

If your code fails on any one of these questions, forget about it. If it survives them, post your results and have someone else verify them.

I'm sorry, your critique is too generel to be useful.

Regards, Clemens

#35 lore raw
Quoted 7 lines · Fruhwirth Clemens
Quoted 6 lines · Jörn Engel

On Thu, 4 September 2003 12:42:45 +0200, Fruhwirth Clemens wrote:

Do some benchmarks on lots of different machines and measure the performance of the asm and c code. If it's faster on PPro but not on PIII or Athlon, forget about it.

Quoted 6 lines
Quoted 4 lines

How big is the .text of the asm and c variant? If the text of yours is much bigger, you just traded 2fish performance for general performance. Everything else will suffer from cache misses. Forget your microbenchmark, your variant will make the machine slower.

Men! Why is everyone doubting the usefulness of assembler optimized parts?

Because assembler is such a pain. :)

In general, you don't want any code to be in assembler, so it is your duty to prove that this is a valid exception.

Quoted 2 lines

It's twice as fast on my Athlon. I assert the same is true for P3/P4. Just test.

Again, that is your job. It is faster and smaller on Athlon, good. How about i386? Does it even run on that machine? If not, you need Kconfig to make sure it isn't compiled in for i386. Repeat for all other platforms.

Repeat for all the other cpus.

Quoted 6 lines

twofish-i586.ko's .text section is smaller than the kernel's twofish.ko's. 945 bytes to be precise. Please note that twofish-i586 includes TWO
implementations: C and assembler. Just think about how much smaller it will be when I rip out the C part.

So much for that.

Ok, that is good. It might still be bigger, depending on the target cpu, but that is unlikely.

Quoted 3 lines
Quoted 1 line

How many bugs are in your code?

42... Is this a serious question?

It is. You code should at least survive test runs against a different implementation to make sure the good case is working correctly. I guess you have done it, but have you really? Didn't tell us yet.

Quoted 5 lines
Quoted 2 lines

Are there any buffer overflows or other security holes? How can you be sure about it?

How can you be sure? Mathematical program verification applies quite badly to assembler.

I'm not sure about the rest of the kernel either, but I have a much better feeling. The encryption implementations in the kernel were based on existing and tested code, got some more testing, were reviewed by several people...

If noone else ever went over your code, I will assume there are some security holes left and a machine using that code is at least vulnerable to local users. This may still be ok, but it should be noted in BIG LETTERS: DANGEROUS.

Quoted 4 lines
Quoted 2 lines

If your code fails on any one of these questions, forget about it. If it survives them, post your results and have someone else verify them.

I'm sorry, your critique is too generel to be useful.

Sorry, I don't feel like going through intel assembler in nasm syntax in my free time, especially if I have to go and fetch the code myself.

Your idea looks promising - the reason why I bother to answer at all - but if it should go in, it still needs a lot of work. The fun part is finished, you are 20% done. ;)

Jörn

Signature · 2 lines
It does not matter how slowly you go, so long as you do not stop.
-- Confucius