Hacker Newsnew | past | comments | ask | show | jobs | submit | more explodingwaffle's commentslogin

The more and more bit-hacks I see, the more convinced I am that they should be handled by a cleverer compiler. Some intrinsic(s) where you say “do x, y, to these bits” and it just figures out the optimum way of doing it for whatever platform you compile for.


This tool is handy if you want a specific bit shuffle:

https://programming.sirrida.de/calcperm.php

It doesn't always find the optimal solution but it usually gets close.


That's a really cool side. Sad that it doesn't allow duplicate indices. I tried implementing the same thing but with support for duplicates and it was definitely a humbling exercise. I'm curious if there is any formal mathematical theory for this (shifted-off bits would probably make it less elegant than pure rotation)


that page + the book Hacker's Delight are a big part of the reason I am so convinced of this :)

If compilers/languages/standard libraries provided these bit permutations, and it was just something ~everyone had learned, it would be a lot easier to work with bits without needing to come up with the bitwise ops (or use that generator). In addition it would probably make better use of the hardware: sure, people like to pretend that we’re still programming C for PDP11, but modern hardware supports more operations than C has operators for (RISC-V B extension and co have the right idea <3)

Modern compilers are probably pretty good? but I doubt they are perfect at turning code like that in OP into the best instructions. It is probably a bit late for C/C++ though. maaaybe possible to get it into LLVM and Rust.


I always wanted to see stuff like this website put into the compiler. https://graphics.stanford.edu/~seander/bithacks.html

The very tough problem is recognizing to emit the correct code.


Well it is already the case, for example things like ROR or ROL are generated by optimizers[1] from certain patterns of bitops.

[1]: https://godbolt.org/z/eE1cx9GT4


That's more like the opposite, OP wants to tell the compiler what transform to do and have it figure out a solution, but in your example you give the compiler a solution and it works backwards to figure out the intent and rewrite it to a completely different solution.

Most languages do have intrinsics for ROL/ROR at least, which you should generally use instead of relying on optimizer magic. I've certainly run into cases where those magic patterns don't get optimized (looking at you MSVC) but intrinsics always work.


At least in my iOS 18 Passwords app, the passwords are shown in a monospace font that (probably deliberately) differentiates all these (e.g. a slashed zero).


I wouldn’t call it a pet peeve but I understand where you’re coming from with this: but I think the main reason for it is electronic engineers, with little-to-no product design or even mechanical design/CAD background. IMO it’s a shame: it’s sort of like the open source hardware version of the “Blender before it got good UX” problem.


The cheap option is https://github.com/ataradov/usb-sniffer which is actually by the same guy as the OP :)

Open hardware so you could build your own for fairly cheap. I bought a clone (is it a clone if the creator never sold them? I'd have preferred to buy it from him...) off aliexpress for ~$50.



Thanks, that's more what I was looking for. Looks like it is still pretty early stuff but could be useful in the future.


Great article. You would hope this feature was more readily exposed on USB-C laptops where it makes a bit more sense… oh well.


I thought it was just supposed to be a pun on “gen’ AI”


> thought it was just supposed to be a pun on “gen’ AI”

Then they'd have gone with Genie.

Which would be kinda genius on their part, but Google isn't that kind of company anymore.


Yes: because Apple actually got this added to the spec: https://www.usb.org/document-library/usb-type-cr-cable-and-c... which is the real story IMO (I am impressed no one bothered looking at the Type-C spec update after the Type-C iPhone finally came out)

This actually replaced the analog audio mode (good riddance- whoever thought adding analog audio into a port that also does digital audio was a good idea?)


Me with an analog headphone?


The problem is that it's a rather confusing Alt Mode.

Basically, it's only supposed to be used for USB-C-to-3.5mm adapters, and it was only implemented by some smartphones. It's not a reliable way to get audio out of a USB-C port, and because it's using the internal DAC the audio quality isn't going to be good anyways.

The proper solution is to use a USB DAC. This provides the same user experience but it actually works with all USB-C devices. Considering the official Apple USB DAC is available for $9, the Audio Alt Mode really doesn't have a reason to exist.


Why is it confusing or bad? Sure, it sacrifices a data line pair, but the same can be said of DisplayPort Alt Mode - if you want 4K@60Hz on DP 1.2 you need to sacrifice USB3.0.

The ability to make this tradeoff should be up to the user.


For an iPhone...

Unless I'm misunderstanding the mode, there could be uses to get audio out of a USB port that don't involve phones...


Thanks. I didn’t know that that connector actually contained a DAC.


Did the manufacturer really think they would get away with such blatant shenanigans? I imagine the lawsuit for this is going to be open and shut.


Anyone else feeling the frequency illusion with rep movsb?

(https://lock.cmpxchg8b.com/reptar.html)


This is unrelated.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: