Skip to content

Commit

Permalink
lets drink beer
Browse files Browse the repository at this point in the history
  • Loading branch information
nicball committed Jul 11, 2024
1 parent 377e0c5 commit 39af270
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion wxd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ target/wxd_mmap: bad/wxd_mmap.c
gcc bad/wxd_mmap.c -o target/wxd_mmap -O3 -g -Wall -pedantic

target/wxd_intrin: wxd_intrin/wxd_intrin.c wxd_intrin/render.c wxd_intrin/render.h
gcc -O2 -march=znver3 wxd_intrin/wxd_intrin.c wxd_intrin/render.c -Iwxd_intrin -o target/wxd_intrin -g -Wall -pedantic
clang -O2 -march=znver3 wxd_intrin/wxd_intrin.c wxd_intrin/render.c -Iwxd_intrin -o target/wxd_intrin -g -Wall -pedantic

11 changes: 11 additions & 0 deletions wxd/wxd_intrin/render.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include <immintrin.h>
#include "render.h"

void render_line_no(const uint64_t* line_no, uint8_t* const outbuf) {
const __m128i to_s = _mm_set_epi8(
'f', 'e', 'd', 'c', 'b', 'a', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0');
const __m128i byte = _mm_cvtepu8_epi16(_mm_cvtsi64_si128(_loadbe_i32(line_no)));
const __m128i byte_hi = _mm_srli_epi16(byte, 4);
const __m128i byte_lo = _mm_and_si128(_mm_slli_epi16(byte, 8), _mm_set1_epi8(0xF));
const __m128i nib_value = _mm_or_si128(byte_hi, byte_lo);
const __m128i nib = _mm_shuffle_epi8(to_s, nib_value);
_mm_storeu_si64(outbuf, nib);
}

void render_ascii(const uint8_t* const inbuf, const int32_t inbuf_len, uint8_t* const outbuf) {
int i;
for (i = 0; i + 32 <= inbuf_len; i += 32) {
Expand Down
2 changes: 2 additions & 0 deletions wxd/wxd_intrin/render.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdint.h>

void render_line_no(const uint64_t* line_no, uint8_t* const outbuf);

void render_ascii(const uint8_t* const inbuf, const int32_t inbuf_len, uint8_t* const outbuf);

int32_t render_hex(const uint8_t* const inbuf, const int32_t inbuf_len, const int8_t* const next_rel, const int8_t* const o2n_rel, uint8_t* const outbuf);
11 changes: 1 addition & 10 deletions wxd/wxd_intrin/wxd_intrin.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ void parse_cli(const int len, const char** args, cli_args_t* result) {
parse_dump(len - 1, args + 1, &result->dump_args);
}

void hex(unsigned long long value, const int width, uint8_t* const out) {
static const uint8_t to_s[] = "0123456789abcdef";
for (int i = width - 1; i >= 0; --i) {
int d = value & 0xF;
value = value >> 4;
out[i] = to_s[d];
}
}

int read_exactly(const int fd, uint8_t* buf, const int size) {
int count = 0;
while (count != size) {
Expand Down Expand Up @@ -132,7 +123,7 @@ int render_xxd(const uint8_t* const inbuf, const int inbuf_len, const int num_co
for (int inbase = 0; inbase < inbuf_len; inbase += num_columns) {
const int outbase = inbase / num_columns * line_width;
const int r = min(num_columns, inbuf_len - inbase);
hex(offset, 8, &outbuf[outbase]);
render_line_no(&offset, &outbuf[outbase]);
outbuf[outbase + 8] = ':';
outbuf[outbase + 9] = ' ';
cursor = outbase + 10;
Expand Down

0 comments on commit 39af270

Please sign in to comment.