cosmopolitan/libc/nexgen32e/crc32init.S
Justine Tunney f4f4caab0e Add x86_64-linux-gnu emulator
I wanted a tiny scriptable meltdown proof way to run userspace programs
and visualize how program execution impacts memory. It helps to explain
how things like Actually Portable Executable works. It can show you how
the GCC generated code is going about manipulating matrices and more. I
didn't feel fully comfortable with Qemu and Bochs because I'm not smart
enough to understand them. I wanted something like gVisor but with much
stronger levels of assurances. I wanted a single binary that'll run, on
all major operating systems with an embedded GPL barrier ZIP filesystem
that is tiny enough to transpile to JavaScript and run in browsers too.

https://justine.storage.googleapis.com/emulator625.mp4
2020-08-25 04:43:42 -07:00

72 lines
3 KiB
ArmAsm

/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify │
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License. │
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of │
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software │
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/macros.h"
.text.startup
/ Generates lookup table for computing CRC-32 byte-by-byte.
/
/ void crc32init(uint32_t table[256], uint32_t polynomial) {
/ uint32_t d, i, r;
/ for (d = 0; d < 256; ++d) {
/ r = d;
/ for (i = 0; i < 8; ++i) {
/ r = r >> 1 ^ (r & 1 ? polynomial : 0);
/ }
/ table[d] = r;
/ }
/ }
/
/ @param rdi is pointer to uint32_t[256] array
/ @param esi 32-bit binary polynomial config
/ @note imposes ~300ns one-time cost
crc32init:
push %rbp
mov %rsp,%rbp
.profilable
lea 256*4(%rdi),%rdx
movd %esi,%xmm0
pshufd $0,%xmm0,%xmm0 # (uint32_t[]){esi,esi,esi,esi} %xmm0
pushpop 4,%rax
movd %eax,%xmm2 # (int[]){4,4,4,4} %xmm2
pshufd $0,%xmm2,%xmm2
0: sub $4,%rsp # (int[]){0,1,2,3} %xmm1
dec %eax
mov %eax,(%rsp)
jnz 0b
movdqu (%rsp),%xmm1
1: mov $8,%ecx
movdqa %xmm1,%xmm3
2: movdqa %xmm3,%xmm4
psrld $1,%xmm4
pslld $31,%xmm3
psrad $31,%xmm3
pand %xmm0,%xmm3
pxor %xmm4,%xmm3
movdqa %xmm3,%xmm4
loop 2b
movdqu %xmm3,(%rdi)
add $16,%rdi
paddd %xmm2,%xmm1
cmp %rdx,%rdi
jb 1b
leave
ret
.endfn crc32init,globl
.source __FILE__