diff options
Diffstat (limited to 'rgo/src')
-rw-r--r-- | rgo/src/fndchr.S | 40 | ||||
-rw-r--r-- | rgo/src/memcpy.S | 61 | ||||
-rw-r--r-- | rgo/src/memdup.c | 22 | ||||
-rw-r--r-- | rgo/src/memfill.S | 38 | ||||
-rw-r--r-- | rgo/src/strdup.c | 17 | ||||
-rw-r--r-- | rgo/src/strfill.c | 17 | ||||
-rw-r--r-- | rgo/src/strlen.S | 35 |
7 files changed, 230 insertions, 0 deletions
diff --git a/rgo/src/fndchr.S b/rgo/src/fndchr.S new file mode 100644 index 0000000..97c3768 --- /dev/null +++ b/rgo/src/fndchr.S @@ -0,0 +1,40 @@ +/* + Copyright 2022 Gabriel Jensen + + This file is part of rgo. + + rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + rgo 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <rgo.h> + +.global rgo_fndchr + +rgo_fndchr: +#if defined(__x86_64__) + /* + rdi: char const * str + sil: char chr + */ + /* rax: Address of the current character. */ + movq %rdi,%rax + /* dl: Current character. */ +.loop: + movb (%rax),%dl + cmpb %dl,%sil + je .done /* Exit loop if we have found the character. */ + testb %dl,%dl + je .err /* We encounted the null-terminator but not the specified character. */ + incq %rax + jmp .loop +.done: + subq %rdi,%rax + ret +.err: + movq $0xFFFFFFFFFFFFFFFF,%rax + ret +#endif diff --git a/rgo/src/memcpy.S b/rgo/src/memcpy.S new file mode 100644 index 0000000..d20e9d6 --- /dev/null +++ b/rgo/src/memcpy.S @@ -0,0 +1,61 @@ +/* + Copyright 2022 Gabriel Jensen + + This file is part of rgo. + + rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + rgo 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <rgo.h> + +.global rgo_memcpy + +rgo_memcpy: +#if defined(__x86_64__) + /* + rdi: void const * in + rsi: size_t num + rdx: void * out + */ + /* rcx: Address of the current input element. */ + movq %rdi,%rcx + /* r8: Address of the current output element. */ + movq %rdx,%r8 + /* r9: Number of remaining elements. */ + movq %rsi,%r9 + /* r10: Temporary. */ + /* xmm0: Temporary. */ +.big128cpy: /* SSE2 is a part of AMD64. */ + cmpq $0x10,%r9 + jl .wrdcpy + movdqu (%rcx),%xmm0 + movdqu %xmm0,(%r8) + addq $0x10,%rcx + addq $0x10,%r8 + subq $0x10,%r9 + jmp .big128cpy +.wrdcpy: + cmpq $0x8,%r9 + jl .bytecpy + movq (%rcx),%r10 + movq %r10,(%r8) + addq $0x8,%rcx + addq $0x8,%r8 + subq $0x8,%r9 + jmp .wrdcpy +.bytecpy: + testq %r9,%r9 + je .done + movb (%rcx),%r10b + movb %r10b,(%r8) + incq %rcx + incq %r8 + decq %r9 + jmp .bytecpy +.done: + ret +#endif diff --git a/rgo/src/memdup.c b/rgo/src/memdup.c new file mode 100644 index 0000000..0d166f0 --- /dev/null +++ b/rgo/src/memdup.c @@ -0,0 +1,22 @@ +/* + Copyright 2022 Gabriel Jensen + + This file is part of rgo. + + rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + rgo 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <rgo.h> + +#include <stdlib.h> + +void * __rgo_memdup(void const * const __restrict__ _ptr,size_t const _num) { + void * const __restrict__ dup = malloc(_num); + if (__builtin_expect (dup == NULL,0x0l)) {return NULL;} + rgo_memcpy(_ptr,_num,dup); + return dup; +} diff --git a/rgo/src/memfill.S b/rgo/src/memfill.S new file mode 100644 index 0000000..3c4fb1f --- /dev/null +++ b/rgo/src/memfill.S @@ -0,0 +1,38 @@ +/* + Copyright 2022 Gabriel Jensen + + This file is part of rgo. + + rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + rgo 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <rgo.h> + +.global rgo_memfill + +rgo_memfill: +#if defined(__x86_64__) + /* + rdi: void const * ptr + rsi: size_t num + dl: int_least8_t val + */ + /* We don't need to preserve any of the registers we use according to the ABI. */ + /* rcx: Address of the current element. */ + movq %rdi,%rcx + /* r8: Address of the last element. */ + movq %rdi,%r8 + addq %rsi,%r8 +.loop: + cmpq %r8,%rcx + je .done /* Exit loop if we have reached the final element. */ + movb %dl,(%rcx) + incq %rcx /* Continue to next element. */ + jmp .loop +.done: + ret +#endif diff --git a/rgo/src/strdup.c b/rgo/src/strdup.c new file mode 100644 index 0000000..f10ef84 --- /dev/null +++ b/rgo/src/strdup.c @@ -0,0 +1,17 @@ +/* + Copyright 2022 Gabriel Jensen + + This file is part of rgo. + + rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + rgo 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <rgo.h> + +#include <stdlib.h> + +char * rgo_strdup(char const * const __restrict__ _str) {return rgo_memdup(_str,rgo_strlen(_str) + (size_t)0x1);} diff --git a/rgo/src/strfill.c b/rgo/src/strfill.c new file mode 100644 index 0000000..4d73c11 --- /dev/null +++ b/rgo/src/strfill.c @@ -0,0 +1,17 @@ +/* + Copyright 2022 Gabriel Jensen + + This file is part of rgo. + + rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + rgo 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <rgo.h> + +#include <stdint.h> + +void rgo_strfill(char const * const __restrict__ _str,char const _chr) {rgo_memfill(_str,rgo_strlen(_str),(uint8_t)_chr);} diff --git a/rgo/src/strlen.S b/rgo/src/strlen.S new file mode 100644 index 0000000..ff6ed70 --- /dev/null +++ b/rgo/src/strlen.S @@ -0,0 +1,35 @@ +/* + Copyright 2022 Gabriel Jensen + + This file is part of rgo. + + rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + rgo 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <rgo.h> + +.global rgo_strlen + +rgo_strlen: +#if defined(__x86_64__) + /* + rdi: char const * str + */ + /* rsi: Address of the current character. */ + movq %rdi,%rsi + /* dl: Current character. */ +.loop: + movb (%rsi),%dl + testb %dl,%dl + jz .done /* Exit loop if we have reached the null-terminator. */ + incq %rsi /* Continue to the next character. */ + jmp .loop +.done: + movq %rsi,%rax + subq %rdi,%rax + ret +#endif |