News for hackers. Stuff that matters.
Re:KolibriOS – operating system written entirely in assembly... by jbk on Wednesday April 15, 2020 @03:35AM (#22875455)
> but with modern compilers, what does assembly get you over just using C or another language [...] I was under the impression compilers nowadays have lots of optimizations that would take a lot of work for a human to do by hand

dav1d, the open source AV1 decoder, has now more asm than C code. It's one of the most recent open source projects with significant asm work ongoing.

The asm version outperforms the C version (full optimizations enabled) by 4,5x on AVX2.

We have similar results in SSSE3 (3,5x) and ARM64 (4x).

We're not talking about a few percents, we're talking about multiple times faster.

And AV1 is a standard, so there are no algo shortcuts that can be made: it is either compliant or it is not.

Sure, in most cases, it is not needed to write asm; but there are cases, notably for multimedia, where writing asm by hand is a lot faster, and that includes codecs and game engines.

Re:KolibriOS – operating system written entirely in assembly... by wiz21c on Wednesday April 15, 2020 @03:40AM (#22875472)
Well, video decoding is very adapted to ASM, I'd say. You can use vectorisation, you can optimize pipelined executions, etc. Things were compiler may not be so good (my experience is a bit dated). You also optimize a few tight loops, so you can really invest time in them with good ROI.

I'm not sure that operating systems are so suited to ASM optimization (in the sense that you may not reap so many benefits). Maybe one could optimize for size (so you can make super tiny OS) ?

Re:KolibriOS – operating system written entirely in assembly... by londons_explore on Wednesday April 15, 2020 @04:18AM (#22875623)
I think one element of writing ASM by hand is the programmer subliminally changes the algorithm to be 'simpler' to write.

In Java, that might involve adding more classes and abstractions to make things conceptually simpler. In C, that might involve using structs to keep relevant data together. In C++, it might involve using a std::set to keep a list of 3 constants, Etc.

It turns out that when you have to write the asm by hand and you see that double pointer dereference is a pain, you avoid data structures with double dereferences (as C data structures often end up with). You avoid classes and abstractions (as are common in Java), because they involve a lot of boilerplate. You use compile time constants for that set of three things rather than any complex hashing that C++ would do, etc.

There are lots of small things like this, and it turns out the effect adds up significantly.

Re:KolibriOS – operating system written entirely in assembly... by wiz21c on Wednesday April 15, 2020 @08:01AM (#22876714)
>> I think one element of writing ASM by hand is the programmer subliminally changes the algorithm to be 'simpler' to write.

I strongly disagree :-)

My experience is that optimizing assembler leads to reorganize your code or even your algorithm into a form that you'll CPU will be most efficient at executing which almost always translate to unbelievably intricate, super hard to modify code. I've done stuff on 6502, 80x86, MMX and the assembly parts, optimized for speed and ended up with impossibly tricky code. Worst, sometimes I have to adapt my data structures to allow optimization, which becomes even tougher. So, I prefer to leave assembly code at the "onyl if necessary" level

But now, to be honest, optimizing assembly code is incredibly satisfying to me :-) I've got the feeling to use a CPU to its maximum capacity. Also, using assembly comes after thorough algorithmic study. So once I'm at the ASM level, I've maxed out my own capabilities ! How happy me !

So if you have the chance to do that, just give it a try !

Re:KolibriOS – operating system written entirely in assembly... by arghwhat on Wednesday April 15, 2020 @08:23AM (#22876866)
I suspect that this occurs for you exactly because you're aiming for optimization. This can happen in any language, really.

I'd assume that when you maintain larger codebases (i.e. full OS and application suite), that you start writing practical and maintainable code instead.

Re:KolibriOS – operating system written entirely in assembly... by wiz21c on Wednesday April 15, 2020 @12:40PM (#22879853)
ahhh I'm biased. Your analysis is right :-)

But even so, I'd say that ASM doesn't help. Clumsy code in ASM is worse than clumsy code in high-level language :-)

Re:KolibriOS – operating system written entirely in assembly... by fouc on Wednesday April 15, 2020 @10:14PM (#22885402)
If you write assembler in a maintainable way, do you still gain lots of benefits over a compiler?
Re:KolibriOS – operating system written entirely in assembly... by _never_k on Wednesday April 15, 2020 @11:25AM (#22878818)
>I think one element of writing ASM by hand is the programmer subliminally changes the algorithm to be 'simpler' to write.

It's not subliminal. It's packing a backpack that someone else has to carry, vs packing a backpack that _you're_ going to carry.

Re:KolibriOS – operating system written entirely in assembly... by jbk on Wednesday April 15, 2020 @05:01AM (#22875809)
> I'm not sure that operating systems are so suited to ASM optimization

I do not know, to be honest.

I was just pointing that multimedia and game engines can get a lot of helps on ASM by hand.

Re:KolibriOS – operating system written entirely in assembly... by spockz on Wednesday April 15, 2020 @06:28AM (#22876211)
I’m wondering if they time spent in writing asm for av1 could also be spend in writing an optimising pass for the c compiler? So that any optimisations can be applied by the compiler while the readability of the C language is retained.
Re:KolibriOS – operating system written entirely in assembly... by gridlockd on Wednesday April 15, 2020 @08:10AM (#22876774)
The compiler knows nothing about your program and its data layout, which is were all of that speedup comes from.

There are some SIMD constructs that you can efficiently express in C by writing intrinsics, but that isn't substantially different from writing ASM.

Then there are some trivial cases where a loop can be unrolled and packed into SIMD instructions automagically, which retains readability, but greatly limits what you can write. You'll need to read the generated machine code to make sure you didn't mess up.

The benefit of just writing ASM is to not have such a translation layer between you and the processor. That same code that you carefully wrote to make it through the optimization passes for one compiler will likely get messed up in another compiler.

Re:KolibriOS – operating system written entirely in assembly... by jbk on Wednesday April 15, 2020 @11:49AM (#22879191)
> I’m wondering if they time spent in writing asm for av1 could also be spend in writing an optimising pass for the c compiler?

Frankly, no.

Improving the compiler could get a few dozen of %, which is huge already. But 300%+, no.

Re:KolibriOS – operating system written entirely in assembly... by papermachete on Wednesday April 15, 2020 @11:17AM (#22878711)
You mean like intrinsics? Sure.
Re:KolibriOS – operating system written entirely in assembly... by JohnBooty on Wednesday April 15, 2020 @03:57AM (#22875540)

    Maybe one could optimize for size (so you can make super tiny OS) ?
Yes, and remember... optimizing for code size is optimizing for speed. =)

Smaller code and data = more CPU cache hits, which are orders of magnitude faster than fetching from RAM. So even if "all" you do is make code smaller, you can get more speed...

Re:KolibriOS – operating system written entirely in assembly... by fractalb on Wednesday April 15, 2020 @04:19AM (#22875629)
> Yes, and remember... optimizing for code size is optimizing for speed. =)

Not always. I think loop-unrolling is a common perf-optimisation technique. Even if the perf-gain is positive in this case, I highly suspect that it doesn't outweigh the cost of maintaining asm code(vs C/other higher level code).

edit: formatting

Re:KolibriOS – operating system written entirely in assembly... by davedx on Wednesday April 15, 2020 @04:37AM (#22875698)
Yep. I remember loop unrolling the skeletal animation code in libgdx way back resulted in a significant performance increase. And that was in Java
Re:KolibriOS – operating system written entirely in assembly... by rurban on Wednesday April 15, 2020 @05:01AM (#22875810)
More so inlining. Much more than loop unrolling.
Re:KolibriOS – operating system written entirely in assembly... by jfkebwjsbx on Wednesday April 15, 2020 @06:06AM (#22876097)
Optimizing for size is a local maximum in performance.
Re:KolibriOS – operating system written entirely in assembly... by papermachete on Wednesday April 15, 2020 @11:23AM (#22878800)
Less code doesn't imply faster code. GCC coreutils have blasted through openbsd coreutils for years.

Now we have CPUs with 256KB instruction cache and up. You can compile your whole OS at insane optimizations.

Re:KolibriOS – operating system written entirely in assembly... by pjmlp on Wednesday April 15, 2020 @09:35AM (#22877495)
There are some LLVM optimization talks that show otherwise, where longer vectorized code actually runs faster than the smaller version, though.
Re:KolibriOS – operating system written entirely in assembly... by userbinator on Wednesday April 15, 2020 @09:46AM (#22877622)
...in microbenchmarks.

That's what created multi-kilobyte memcpy() implementations, which barely beat REP MOVSB but cause huge icache bloat.

Re:KolibriOS – operating system written entirely in assembly... by londons_explore on Wednesday April 15, 2020 @04:26AM (#22875653)
Compare a 512 byte boot sector game [1] to a ~1Gigabyte unity 'hello world'.

Until the day the compiler can smartly trim down all the unity framework to just 512 bytes because it notices I'm not using most of it, hand coding in asm will always work out smaller and faster, if one puts in enough effort.

[1]: https://www.youtube.com/watch?v=1UzTf0Qo37A

Re:KolibriOS – operating system written entirely in assembly... by Dylan16807 on Wednesday April 15, 2020 @04:47AM (#22875741)
Unity isn't.. what? And the problem there is using an ill-suited framework, not the language.

If you compile hello world in C, it's not going to be very big either.

> if one puts in enough effort

"enough effort" is a cheat. I can replace almost any program with a smaller javascript version, if I put in "enough effort". It's not a reasonable way to compare anything.

Re:KolibriOS – operating system written entirely in assembly... by snvzz on Wednesday April 15, 2020 @07:10AM (#22876424)
>If you compile hello world in C, it's not going to be very big either.

You should actually try that, and compare sizes with the asm hello world.

Re:KolibriOS – operating system written entirely in assembly... by SomeoneFromCA on Wednesday April 15, 2020 @10:48AM (#22878346)
The problem not in the language per se, but in the linker settings. By tweaking it, you can achieve almost same size as the assembler version has.
Re:KolibriOS – operating system written entirely in assembly... by BubRoss on Wednesday April 15, 2020 @01:00PM (#22880128)
You say that, but I still haven't seen any compilers link in only parts of libc. Use any of it and it all comes in.

Hello world in C becomes anywhere from 120KB to 1.2MB even if just using puts. Even setting the entry point to a function with no arguments doesn't help.

A tiny hello world ends up needing to ignore the standard library and use a console output function from the OS.

Re:KolibriOS – operating system written entirely in assembly... by SomeoneFromCA on Wednesday April 15, 2020 @04:44PM (#22882804)
Just checked on a Ubuntu machine. Hello world takes 6Kb. Uses printf.
Re:KolibriOS – operating system written entirely in assembly... by BubRoss on Wednesday April 15, 2020 @05:14PM (#22883163)
That's because it links dynamically to the standard library.
Re:KolibriOS – operating system written entirely in assembly... by Dylan16807 on Wednesday April 15, 2020 @10:24PM (#22885445)
Is that a problem? It's there whether you use it or not, and it's not like the asm is running in a vacuum either.

If you go to an embedded point of view where there is nothing already supplied, you can get a hello world, even with a basic printf, down to that size.

Re:KolibriOS – operating system written entirely in assembly... by snvzz on Thursday April 16, 2020 @06:22AM (#22887911)
Especially since you use external libraries to do the bulk of the work, 6KB is a lot.

It is an order of magnitude bigger than the assembly version.

Re:KolibriOS – operating system written entirely in assembly... by SomeoneFromCA on Thursday April 16, 2020 @11:24AM (#22890344)
After some manipulations with the linker, I was able to cut the size down to 750 bytes. You cannot go below this, without hacking the ELF file format.
Re:KolibriOS – operating system written entirely in assembly... by snvzz on Thursday April 16, 2020 @01:51PM (#22891809)
I got my asm one down to 268 without even trying.

It prints (via write syscall) "hello world.\n" and then calls exit(0).

Assembled with fasm, linked with ld -n, then stripped.

Re:KolibriOS – operating system written entirely in assembly... by SomeoneFromCA on Thursday April 16, 2020 @05:14PM (#22893649)
Ok fine, I've used the tool called sstrip from a suite called ELFKickers. Now my hello_world.cpp (yes, c++) is 380 bytes long. I could not make it any shorter, so yeah, it is 120 bytes (50%) bigger than yours.
Re:KolibriOS – operating system written entirely in assembly... by snvzz on Friday April 17, 2020 @01:21AM (#22896452)
Regarding sstrip, I am familiar with it. Be careful that you know what it actually does.
Re:KolibriOS – operating system written entirely in assembly... by SomeoneFromCA on Friday April 17, 2020 @09:53AM (#22898974)
So, not order of magnitude then?
Re:KolibriOS – operating system written entirely in assembly... by snvzz on Friday April 17, 2020 @12:17PM (#22900301)
Yes, just look into how much effort you had to put vs my five minutes attempt.

The difference is between just using asm, or going out of the way to try to coerce a C toolchain to minimize the bloat.

Re:KolibriOS – operating system written entirely in assembly... by SomeoneFromCA on Friday April 17, 2020 @01:18PM (#22900935)
Yea, large effort (objcopy, strip, sstrip), that I need to do only once, after that I already know how to do this an can just put it in a shell script. Now, I would like to know how quickly you'll be able to write a quicksort implementaion, that would outperform my C code, while being at least 25% smaller . Yeah, and how big your binary will become, once you link libc or opengl or whatever to make it able to do actual work.
Re:KolibriOS – operating system written entirely in assembly... by 7thaccount on Wednesday April 15, 2020 @09:24AM (#22877394)
95KB using MinGW's GCC on Windows using default compilation.

When I tell it to emit the assembly with "-S" I get 28 lines of code when I have a feeling assembly is much shorter.

Re:KolibriOS – operating system written entirely in assembly... by jfkebwjsbx on Wednesday April 15, 2020 @06:03AM (#22876079)
I agree that Unity is bloated and should take way less constant storage, but that's an invalid comparison. They are not the same domains, they don't use the same APIs, the don't on the same environment, they don't talk to the same hardware, they don't have the same functionality, etc.
Re:KolibriOS – operating system written entirely in assembly... by dhhwrongagain on Wednesday April 15, 2020 @04:54AM (#22875763)
All of the performance wins you’re referencing come from vectorization, you typically can’t vectorize OS code since it isn’t ALU bound, which makes your point moot.
Re:KolibriOS – operating system written entirely in assembly... by jbk on Wednesday April 15, 2020 @04:59AM (#22875800)
> you typically can’t vectorize OS code since it isn’t ALU bound

The answer and parent were in no way only talking about OS.

Re:KolibriOS – operating system written entirely in assembly... by dhhwrongagain on Wednesday April 15, 2020 @05:12AM (#22875854)
The parent was definitely talking about the applicability of using assembly in the context of this project. Vectorization is irrelevant here.
Re:KolibriOS – operating system written entirely in assembly... by tromp on Wednesday April 15, 2020 @04:21AM (#22875636)
> by 4,5x on AVX2

I would expect a much smaller gap as AVX2 is also available through intrinsics on C ?!

Re:KolibriOS – operating system written entirely in assembly... by Someone on Wednesday April 15, 2020 @08:28AM (#22876901)
When using Clang org CC (possibly other compilers, too), you don’t need to use intrinsics. These compilers have some support for vector types (https://clang.llvm.org/docs/LanguageExtensions.html#vectors-..., https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html)

One can, for example, write:

  typedef int v4si __attribute__ ((vector_size (16)));

  v4si a, b, c;
  […]
  b += 3; // add 3 to each of b’s elements
  c = a + b; // pairwise addition
Using this gives up some control over assembly; it doesn’t guarantee vector instructions get used, but it makes it easier for the compiler to generate them.

Also, I’m fairly sure there are vector instructions not covered by this extension.

Re:KolibriOS – operating system written entirely in assembly... by pjmlp on Wednesday April 15, 2020 @04:41AM (#22875715)
CPU intrinsics are just another way of doing inline Assembly.
Re:KolibriOS – operating system written entirely in assembly... by throwaway_pdp09 on Wednesday April 15, 2020 @04:58AM (#22875789)
IANA C/C++/ASM expert, but one thing I've been thinking of would greatly benefit from type-punning (overlaying 2 kinds of data and interpreting one as the other) because it can use a 64-bit register to parallelise 4 ops at once where speed really, really matters.

That's trivial in ASM I believe, but don't know how well it's possible in C/C++, if at all.

Re:KolibriOS – operating system written entirely in assembly... by pjmlp on Wednesday April 15, 2020 @05:31AM (#22875934)
I fail to see why not just use a __m64 variable and do the conversion.

https://software.intel.com/sites/landingpage/IntrinsicsGuide...

You can also try to make a union whose one field is a __m64, but beware not to trigger UB conditions.

Re:KolibriOS – operating system written entirely in assembly... by saagarjha on Wednesday April 15, 2020 @05:27AM (#22875911)
Type punning in general would be somewhat difficult to do here in a way that vectorized but was still defined by the standard. You’d need to put a branch with undefined behavior in it when alignment wasn’t satisfied, then do a memcpy into a vector type, and hope that the compiler understands what you’re doing and doesn’t deoptimize it.
Re:KolibriOS – operating system written entirely in assembly... by dhhwrongagain on Wednesday April 15, 2020 @04:56AM (#22875776)
False or vacuously true
Re:KolibriOS – operating system written entirely in assembly... by jbk on Wednesday April 15, 2020 @04:57AM (#22875787)
That is still writing asm. You need to write it, the compiler cannot do it by itself.
Re:KolibriOS – operating system written entirely in assembly... by DagAgren on Wednesday April 15, 2020 @06:43AM (#22876283)
Video coding is one of the few outliers where compilers are not very good at the kind of optimisations that really help. Any experiences with that kind of code does not generalise to other types of code.
Re:KolibriOS – operating system written entirely in assembly... by vanderZwan on Wednesday April 15, 2020 @04:17AM (#22875618)
How much would something like Halide help with writing optimized code for AV1? It's originally designed for image manipulation, but I imagine encoding/decoding to be a little bit more demanding of expressiveness.

https://halide-lang.org/

Re:KolibriOS – operating system written entirely in assembly... by thesz on Wednesday April 15, 2020 @05:43AM (#22875984)
When I worked with Halide, on a thing that is relatively close to the image decoding (transform a series of angle-amplitude pairs (radioastronomy) to the image, a lot of sprite-with-opaqueness painting), I was perplexed to find out how hard it is to work in Halide with non-constant offsets. The functionality was essentially non-existent back then (2015, I assume).

In fact, any scatter-gather operations were non-existent in Halide.

From what I remember these operations were introduced at some point, but we moved away from Halide.

Also, it was not quite simple to transform a loop that draws sprites over the entire image into a loop that draw sprites over part of image and draws many parts of image in parallel (change nesting). Hand-written CUDA version of the algorithm ended up with exactly that.

Thus, if you need some partial-derivatives-numerical-kernel, Halide is good for you. If you are working on the video decoding, Halide is not that good for you. If you are working on video encoding, Halide will be more of a nuisance than a helping hand (early exits from loops, computable access ranges, etc).

Re:KolibriOS – operating system written entirely in assembly... by BubRoss on Wednesday April 15, 2020 @01:08PM (#22880245)
That is all very interesting. If you were drawing sprites, why not use straight openGL?

Do you think it would have worked to organize tiles and threads outside of halide and use halide for isolated parts that are already organized into arrays?

Re:KolibriOS – operating system written entirely in assembly... by thesz on Wednesday April 15, 2020 @02:53PM (#22881552)
We would like to be relatively target-agnostic. OpenGL could be one of targets, but not only one. We also would like to work on regular and/or GPU-equipped cluster machines, etc.

On the suggestion in your second part: why use Halide then? Should it be responsibility of Halide to work out the best loop nesting and best use of threads?

Again, Halide was put aside and we used CUDA for final version, exactly because of inability of Halide to do good work in our case.

Re:KolibriOS – operating system written entirely in assembly... by BubRoss on Wednesday April 15, 2020 @05:21PM (#22883221)
> Should it be responsibility of Halide to work out the best loop nesting and best use of threads?

I don't know about "should", but it seems to me that it would still be valuable, even if working out the threading and organization.of the data into an array.

> Again, Halide was put aside and we used CUDA for final version, exactly because of inability of Halide to do good work in our case

I didn't say anything about that. I'm not sure why you are restating it.

Re:KolibriOS – operating system written entirely in assembly... by vanderZwan on Wednesday April 15, 2020 @01:16PM (#22880360)
I suppose Halide was mainly involved for this part:

> transform a series of angle-amplitude pairs (radioastronomy) to the image

Re:KolibriOS – operating system written entirely in assembly... by MithrilTuxedo on Wednesday April 15, 2020 @06:19PM (#22883780)
> We're not talking about a few percents, we're talking about multiple times faster.

I wonder how long it will take for compilers close that gap. I assume compilers will eventually produce assembly code that outperforms anything written by human.

Re:KolibriOS – operating system written entirely in assembly... by tcbawo on Wednesday April 15, 2020 @06:29PM (#22883868)
These aren't necessarily the typical use cases that compiler writers are trying to optimize, either. So, compilers for general purpose languages may never close that gap.
Re:KolibriOS – operating system written entirely in assembly... by agumonkey on Wednesday April 15, 2020 @07:52AM (#22876671)
Have you seen a non asm low level language that could fit hardware better than C ? Not starting a flamewar, just that it seems that you're in a context where you may have reviewed things we might not know of.
Re:KolibriOS – operating system written entirely in assembly... by cxr on Wednesday April 15, 2020 @09:37AM (#22877511)
I hear tell that a lot of the places where modern Fortran shows up are contexts where faster-than-C performance is desirable.
Re:KolibriOS – operating system written entirely in assembly... by mywittyname on Wednesday April 15, 2020 @11:17AM (#22878715)
A company I used to work for has spent 30 years trying to migrate FORTRAN code to C. I was not involved in the project, so I don't know the details, but the gist of what I was told is that there are edge cases where FORTRAN stomps C in every performance metric. So you can make decent progress for a while, but you'll eventually hit a major roadblock that halts progress.
Re:KolibriOS – operating system written entirely in assembly... by _never_k on Wednesday April 15, 2020 @11:20AM (#22878776)
I think of the BLAS algos as being very Fortran friendly, and the Fortran references never _out_perform the C implementations. (The asm implementations are of course the best.)

Be curious to know what you're thinking of here.

Re:KolibriOS – operating system written entirely in assembly... by setpatchaddress on Wednesday April 15, 2020 @12:53PM (#22880018)
I've no personal experience, but people historically point to pointer aliasing as the primary thing that speeds up Fortran code vs C.

https://en.wikipedia.org/wiki/Pointer_aliasing

Re:KolibriOS – operating system written entirely in assembly... by agumonkey on Wednesday April 15, 2020 @01:20PM (#22880412)
No personal experience either but discussing with people invested in high perf C, they said that `restrict` was enough to avoid aliasing issues.
Re:KolibriOS – operating system written entirely in assembly... by agumonkey on Wednesday April 15, 2020 @09:41AM (#22877553)
But that's plausibly the compiler doing its magic. Or is there anything in Fortran language/semantics that maps close to vectorized ISAs ?
Re:KolibriOS – operating system written entirely in assembly... by ajxs on Wednesday April 15, 2020 @09:25PM (#22885162)
I've done some bare-metal development in Ada. It was originally designed for embedded systems, so it has features that fit this niche very well. It's definitely not as simple as C, it has a much more modern design however.
Re:KolibriOS – operating system written entirely in assembly... by BubRoss on Wednesday April 15, 2020 @01:03PM (#22880171)
ISPC in this case is exactly that. It makes vectorization fairly easy and very fast.