HNHacker News
TopNewBestAskShowJobs

clausecker

335 karma · joined September 13, 2023

submissionscomments
clausecker··on C's Flexible Integer Sizes Were Not a Design Mistake
The key problem with C's type sizes is that ANSI C had no way to get fixed integer types, so if the basic types char, short, int, and long didn't have the size you wanted, it might as well not have existed.

For this reason, platform designers were motivated to provide some 32 bit type, which by elimination needs to be int (it can't be char, which must be 8 bits, it can't be short or else you have no 16 bit type, so it must be int or long, but a 16 bit int is silly). Initial 64 bit ABIs were defined with 64 bit int types (ILP64 platforms, such as the first SPARC64 Solaris port) and the problems quickly became apparent when random C software packages failed to configure, compile, or work. Switching to LP64/LLP64 (where int is 32 bits and long is 32 resp. 64 bits) fixed this, at the cost of having to reeducate programmers to use size_t for array indexing. The decision between LP64 and LLP64 was then probably motivated by time_t, which traditionally is a long. If you do LLP64, long and thus time_t is 32 bits and you'll fall prey to the year 2036 problem. Or you use long long for time_t, breaking compatibility with ANSI C and a lot of old software. Most UNIX platforms decided to use LP64 thus, while non-UNIX platforms without this issue frequently went with LLP64 instead, reaping the benefit of staying compatible with software that assumes long is 32 bits.

These days, you probably need only three platform-dependently-sized types (as Go does):

- int/uint for array indexing; native word size, signed and unsigned - uintptr for round-tripping data pointers

uintptr needs to be different as there are a few platforms where pointers are larger than indices, for example:

- on 8086 and i286, pointers are 32 bits (segment+offset) in some code models, while indices are only 16 bits - on IBM iSeries, pointers are 128 bits, while indices are 32 or 64 bits - on capability architectures like CHERI, capabilities (pointers) are 128 bits and need special treatment, while integers are 32 or 64 bits - on MSP430X, pointers are 20 bits (!) while integers are 16 bits

Conceivably one might also need a separate type for text pointers, as these are some times of different size than data pointers (e.g. on PIC), but as arithmetic on text pointers does not generally make sense, a corresponding integer type can perhaps be omitted (use a generic function type). This also helps with platforms like PowerPC, where text pointers are pairs of addresses (entry+toc).

C's ptrdiff_t can be omitted if you follow C semantics, where differences between pointers are only meaningful within the same object, as that's just int. intptr_t does not really make a lot of sense, pointers are unsigned except on weirdo platforms (such as balanced ternary machines).

C's maxalign_t and (u)intmax_t are problematic, as they fail when the architecture is expanded with new features. For example, maxalign_t might have an 8 byte alignment on x86, but then you add SSE which requires a 16 byte alignment, AVX with a 32 byte alignment, AVX-512 with a 64 byte alignment and so on. Not good news. Same for (u)intmax_t, which end up stopping you from ever supporting larger integer types. It is not entirely clear what the solution is. For maxalign_t, perhaps the maximum alignment can be queried at runtime. For (u)intmax_t, perhaps use generic programming to instantiate the relevant code paths for the actual type used.

clausecker··on C's Flexible Integer Sizes Were Not a Design Mistake
The main reason they did it like that is that a lot of software expects 8, 16, and 32 bits to be found among the basic types and if it did not find all the types, things would go wrong horribly. It's quite unfortunate.
clausecker··on Just Use Go
Go had panic/recover right from the get go. Nobody “caved.” And indeed, you are free to use panic/recover for error handling in your code. That was always allowed.

That said, the key insight of the exception craze is that error returns are a normal part of a functions behaviour and as such should not use extraordinary control flow to take place.

Exceptions (panics) are used for things that should never happen or are indicative of a programmer error, like nil dereferences or out-of-bounds array accesses. That is, things where the programmer is not expected to provide reasonable behaviour on the API level if it happens (but perhaps there is a whole-program fault handler to shut down cleanly).

Opening a file that does not exist? Database record not found? Invalid credentials? These should be anticipated by the programmer and can occur at any time. Not exceptions, normal return values. Go requests that you think about these possibilities instead of pretending they can be ignored.

clausecker··on Just Use Go
Really bad idea.

Silently returning some (the wrong) value is always worse than catching the error right then and their and panicking. A panic is a noticeable symptom of something just having go wrong that is easy to chase after and debug. A silently returned wrong value just causes data corruption down the line, at great pain. It is very annoying to debug, in particular if people start to some times rely on this behaviour as an intentional part of their data flow.

clausecker··on Just Use Go
> No it really doesn't. It litters your code with if statements that are all just about the same, except that one that needs to be different, and you go blind looking at them all and can't spot the difference.

If most of your error handling is just bubbling the error up, you are doing something wrong.

  if err != nil { return err; }
is an antipattern. Not because it's verbose, but because you are supposed to handle the error, not pass it right through, in most cases.
clausecker··on A SIMD coding challenge: First non-space character after newline
You can find the first set bit in an integer with a machine instruction, it's completely branch free. gcc has __builtin_ctz() for this. You'll either need to iterate over all set bits (so one branch per set bit) or use a compression instruction (requiring AVX-512) to turn the bit set into a set of integers.

That said, as you seem to actually want to do something with the results, you'll take a branch per match anyway, so I don't see the problem.

clausecker··on A SIMD coding challenge: First non-space character after newline
So I've thought about it and I don't really feel like spending more time to convince you that this works. If you have questions I am happy to answer them, but please write your own code.
clausecker··on A SIMD coding challenge: First non-space character after newline
This does track the state. If you want to track it across multple vectors of input, you'll need to carry it over manually.
clausecker··on A SIMD coding challenge: First non-space character after newline
You can do it like this, assuming A is the mask of newlines and B is the mask of non-spaces.

1. Compute M1 = ~A & ~B, which is the mask of all spaces that are not newlines 2. Compute M2 = M1 + (A << 1) + 1, which is the first non-space or newline after each newline and then additional bits behind each such newline. 3. Compute M3 = M2 & ~M1, which removes the junk bits, leaving only the first match in each section

Here is what it looks like:

    10010000 = A
    01100110 = B
    00001001 = M1 = ~A & ~B
    00101010 = M2 = M1 + (A << 1) + 1
    00100010 = M3 = M2 & ~M1
Note that this code treats newlines as non-spaces, meaning if a line comprises only spaces, the terminating NL character is returned. You can have it treat newlines as spaces (meaning a line of all spaces is not a match) by computing M4 = M3 & ~A.
clausecker··on Package managers keep using Git as a database, it never works out
We do this with FreeBSD ports, but users don't have to clone the ports tree unless they want to modify ports or compile them with custom options.
clausecker··on Uninitialized garbage on ia64 can be deadly (2004)
And the same reason NVRAM was dead on arrival. No affordable dev systems meant that only enterprise software supported it.
clausecker··on Addressing the adding situation
That's more "load store architecture" than RISC. And by that measure, S/360 could be considered a RISC.
clausecker··on Addressing the adding situation
You may enjoy the RISC deprogrammer: https://blog.erratasec.com/2022/10/the-risc-deprogrammer.htm...
clausecker··on Poll HN: What operating system do you primarily develop on?
FreeBSD
clausecker··on FEX-emu – Run x86 applications on ARM64 Linux devices
ARM already has most stuff required for this on board. Two proprietary extensions are used by Rosetta: one emulates the parity (rarely used) and half-carry (obsolete) flags, which can also be emulated conventionally. The other implementa TSO memory ordering, which can either be ignored or implemented with explicit barriers; some other chips apparently have a similar setting.

The other stuff is all present in ARMv8.5 I think.

clausecker··on Fortran Outsmarted Our Billion-Dollar AI Chips
It's very likely that there is some serious autovectorisation going on behind the scenes.
clausecker··on End of Japanese community
Best one was when gedit had the option to syntax highlight for a language named “Los.”
clausecker··on Why every Rust crate feels like a research paper on abstraction
Had the same feeling browsing through the Haskell package collection. Felt like and almagamation of PhD theses, none of which were maintained after the author got his degree. Every single one a work of art, but most engeneered so badly that you would only use them begrudgingly.
clausecker··on Mysterious Intrigue Around an x86 "Corporate Entity Other Than Intel/AMD"
Such as?
clausecker··on Comparing a RISC and a CISC with similar hardware organization (1991)
I'm talking about how they are able to integrate stuff that normally wouldn't fit into 32 bits (such as 3 operand simd with masking), not about getting the instruction set more compact. ARM knows how to do this (Thumb being the most compact mainstream ISA is evidence of that), they just have decided to waste a bit more space to make decoding simpler, while also adding more quality-of-life features.
clausecker··on Comparing a RISC and a CISC with similar hardware organization (1991)
Also, the VAX instruction encoding is a class of horror above that of x86.
clausecker··on Comparing a RISC and a CISC with similar hardware organization (1991)
ARM64 has a trick up its sleeve: many instructions that would be longer on other architecturea are instead split into easily recognisable pairs on ARM64. This allows for simple inplementations to pretend it's fixed length while more complex ones can pretend it's variable length. SVE takes this one step further with MOVPRFX, which can add be placed before almost all SVE instructions to supply masking and a third operand.
clausecker··on Comparing a RISC and a CISC with similar hardware organization (1991)
LL/SC is performant, it just doesn't scale to high core counts.

The VEX encoding is actually only rarely longer than the legacy one, and frequently it is shorter.

clausecker··on Processing Strings 109x Faster Than Nvidia on H100
When I implemented SIMD-accelerated string functions for FreeBSD's libc, I briefly looked at Stringzilla, but the code didn't look particularly interesting or fast. So no surprise here.
clausecker··on The Fisherman and His Wife (1857)
If you give in to an unreasonable person's demands, that person will demand more and more until it all comes crashing down.
clausecker··on The Fisherman and His Wife (1857)
A famous fairytale from Germany.
clausecker··on I didn't bring my son to a museum to look at screens
My favourite museums are those that are a huge pile of old shit with some labels telling you what you are looking at. This whole "hundreds of screens with some odd artifact inbetween" style is just boring.
clausecker··on How to slow down a program and why it can be useful
Skylake has the exact same optimisations.
clausecker··on The Folk Economics of Housing
In the US, isn't this partially the cause of zoning laws banning anything that is not single-family homes from being built?
clausecker··on A brief history of the absurdities of the Soviet Union
Not entirely true. Jews willing to leave had to pay an exit tax so high that it basically meant leaving all your wealth behind. Later, they weren't permitted to emigrate at all.
Page 1 of 4Next →