4 ms·
$ cat parse256.c #include <stdio.h> #include <stdint.h> #include <string.h> int parse_uint8_fastswar(const char *str, size_t len, uint8_t *num) { u
by kazinator 3y ago
$ cat parse256.c
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int parse_uint8_fastswar(const char *str, size_t len, uint8_t *num) {
union {
uint8_t as_str[4];
uint32_t as_int;
} digits;
memcpy(&digits.as_int, str, sizeof(digits));
digits.as_int ^= 0x30303030lu;
digits.as_int <<= (4 - (len & 0x3)) * 8;
uint32_t y = ((UINT64_C(0x640a0100) * digits.as_int)>>32)&0xff;
*num = (uint8_t)(y);
return (digits.as_int & 0xf0f0f0f0) == 0 && y < 256 && len != 0
&& len < 4;
}
int main(int argc, char **argv)
{
if (argv[1]) {
uint8_t val = 0;
if (parse_uint8_fastswar(argv[1], strlen(argv[1]), &val)) {
printf("value of %s is %d\n", argv[1], (int) val);
} else {
printf("%s is invalid, value stored %d\n", argv[1], (int) val);
}
}
return 0;
}
$ ./parse256 123
123 is invalid, value stored 225
$ uname -a
Linux gcc1-power7.osuosl.org 3.10.0-862.14.4.el7.ppc64 #1 SMP Wed Sep 26 20:38:32 GMT 2018 ppc64 ppc64 ppc64 GNU/Linux
Oops!
- deleted 3y ago[deleted]
- bangonkeyboard 3y agoThese gotchas make keeping a big-endian machine around totally worth it.
- dataflow 3y agoI'm guessing there isn't any way to simulate big endian on x86?
- deleted 3y ago[deleted]
- JonChesterfield 3y agoA compiler could do that - involves inserting bswap on the way to/from memory, might also be a worthwhile flag for big endian machines that want to execute code that assumes little endian load/stores. I don't know of an implementation of such though.
- o11c 3y agoGCC has `__attribute__((scalar_storage_order("big-endian")))`. Note that the attribute is applied on `struct`s (and `union`s), taking effect for scalars within them, but does not recurse into further aggregates.
- RaisingSpear 3y agoDon't forget a middle-endian machine too!
- JonChesterfield 3y agoThis (from the op, not from Kaz) reads as nonsense. Half the union is unused so that should be a uint32_t. Looks like a failed attempt to bypass aliasing limitations. The out of bounds read is bad, would segv if you aimed shortly before a page you don't have permission to read. Maybe the subsequent shuffling does something useful but it's dubious to be returning a single uint8_t and a boolean (upcast to an int, weirdly) per four bytes of input. Took another look at it and can't work out the interface. Reading ascii in base 10, but 'len' claims to be the length of the string and not how many digits to read, and the behaviour is roughly return 0 on non-ascii-numeric-char. Maybe it's quicker than the unrolled loop, though I suspect if you mark the pointer aligned 4 (which would make the reading past the end segv-safe) the compiler would produce a single load for you from the byte at a time code.
- secondcoming 3y agoThe article does caveat that you must be able to safely read 4 bytes of data even if the string is shorter.
- duskwuff 3y agoIt also implicitly requires that you be able to perform an unaligned 32-bit read. On some architectures, this will trap; on many others, it's slow.
- deleted 3y ago[deleted]
- classichasclass 3y agoSuspect this line: uint32_t y = ((UINT64_C(0x640a0100) * digits.as_int)>>32)&0xff; This makes assumptions about how a 64-bit int is laid out in memory. Not near a big endian system right now (I know, right?) but I'm pretty sure this is the problem.