diff options
-rw-r--r-- | CHANGELOG.txt | 7 | ||||
-rw-r--r-- | test/source/main.c | 21 | ||||
-rw-r--r-- | test/source/test/memcnt.i | 28 | ||||
-rw-r--r-- | test/source/test/memfnd.i (renamed from test/source/test/fndbyte.i) | 20 | ||||
-rw-r--r-- | test/source/test/memfor.i (renamed from test/source/test/foreach.i) | 6 | ||||
-rw-r--r-- | test/source/test/strfnd.i (renamed from test/source/test/fndchr.i) | 8 | ||||
-rw-r--r-- | zap/GNUmakefile | 7 | ||||
-rw-r--r-- | zap/include/zap/bs.h | 2 | ||||
-rw-r--r-- | zap/include/zap/mem.h | 11 | ||||
-rw-r--r-- | zap/source/amd64/mem/memcat.c | 2 | ||||
-rw-r--r-- | zap/source/amd64/mem/memcnt.c | 22 | ||||
-rw-r--r-- | zap/source/amd64/mem/memfnd.S (renamed from zap/source/amd64/mem/fndbyte.S) | 4 | ||||
-rw-r--r-- | zap/source/amd64/mem/memfor.S (renamed from zap/source/amd64/mem/foreach.S) | 4 | ||||
-rw-r--r-- | zap/source/amd64/mem/strfnd.S (renamed from zap/source/amd64/mem/fndchr.S) | 4 |
14 files changed, 103 insertions, 43 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 131b7d7..e5b72ee 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,10 @@ +# 13.0 + +* Add counting function; +* Rename foreach to memfor; +* Rename fndbtyte to memfnd; +* Rename fndchr to strfnd; + # 12.2 * Improve assembly readability (save utf8enc and utf8enclen for later); diff --git a/test/source/main.c b/test/source/main.c index ce73d3a..0d5ea85 100644 --- a/test/source/main.c +++ b/test/source/main.c @@ -19,19 +19,20 @@ typedef bool (* zaptest_testtyp)(void); #include "test/bool.i" #include "test/cmp.i" -#include "test/fndbyte.i" -#include "test/fndchr.i" -#include "test/foreach.i" #include "test/memcat.i" +#include "test/memcnt.i" #include "test/memcp.i" #include "test/memeq.i" #include "test/memfill.i" +#include "test/memfnd.i" +#include "test/memfor.i" #include "test/memgen.i" #include "test/nullptr.i" #include "test/strcat.i" #include "test/strcp.i" #include "test/streq.i" #include "test/strfill.i" +#include "test/strfnd.i" #include "test/strlen.i" #include "test/utf20len.i" #include "test/utf8dec.i" @@ -42,19 +43,20 @@ typedef bool (* zaptest_testtyp)(void); static zaptest_testtyp zaptest_tests[] = { zaptest_test_bool, zaptest_test_cmp, - zaptest_test_fndbyte, - zaptest_test_fndchr, - zaptest_test_foreach, zaptest_test_memcat, + zaptest_test_memcnt, zaptest_test_memcp, zaptest_test_memeq, zaptest_test_memfill, + zaptest_test_memfnd, + zaptest_test_memfor, zaptest_test_memgen, zaptest_test_nullptr, zaptest_test_strcat, zaptest_test_strcp, zaptest_test_streq, zaptest_test_strfill, + zaptest_test_strfnd, zaptest_test_strlen, zaptest_test_utf20len, zaptest_test_utf8dec, @@ -66,19 +68,20 @@ static zaptest_testtyp zaptest_tests[] = { static char const * zaptest_testnms[] = { "bool", "cmp", - "fndbyte", - "fndchr", - "foreach", "memcat", + "memcnt", "memcp", "memeq", "memfill", + "memfnd", + "memfor", "memgen", "nullptr", "strcat", "strcp", "streq", "strfill", + "strfnd", "strlen", "utf20len", "utf8dec", diff --git a/test/source/test/memcnt.i b/test/source/test/memcnt.i new file mode 100644 index 0000000..2e333f0 --- /dev/null +++ b/test/source/test/memcnt.i @@ -0,0 +1,28 @@ +#include <stdbool.h> + +static zap_bool memcntfn(void const * const _ptr) { + unsigned int const * ptr = _ptr; + if (*ptr & 0x1u) { + return zap_false; + } + return zap_true; +} + +static bool zaptest_test_memcnt(void) { + unsigned int arr[] = { + 0x0u, + 0x1u, + 0x2u, + 0x3u, + 0x4u, + 0x5u, + 0x6u, + 0x7u, + }; + zap_sz num = zap_memcnt(arr,sizeof (arr[0x0u]),sizeof (arr) / sizeof (arr[0x0u]),memcntfn); + zaptest_chk(num,0x4u,zap_sz,"%zX"); + arr[0x0u] = 0x1u; + num = zap_memcnt(arr,sizeof (arr[0x0u]),sizeof (arr) / sizeof (arr[0x0u]),memcntfn); + zaptest_chk(num,0x3u,zap_sz,"%zX"); + return false; +} diff --git a/test/source/test/fndbyte.i b/test/source/test/memfnd.i index 56231ac..50923c6 100644 --- a/test/source/test/fndbyte.i +++ b/test/source/test/memfnd.i @@ -1,6 +1,6 @@ #include <stdbool.h> -static bool zaptest_test_fndbyte(void) { +static bool zaptest_test_memfnd(void) { unsigned char const arr[] = { 0x1u, 0x3u, @@ -11,23 +11,23 @@ static bool zaptest_test_fndbyte(void) { 0x7Fu, 0xFFu, }; - zap_sz pos = zap_fndbyte(arr,sizeof (arr),0x1u); + zap_sz pos = zap_memfnd(arr,sizeof (arr),0x1u); zaptest_chk(pos,0x0u,zap_sz,"%zX"); - pos = zap_fndbyte(arr,sizeof (arr),0x3u); + pos = zap_memfnd(arr,sizeof (arr),0x3u); zaptest_chk(pos,0x1u,zap_sz,"%zX"); - pos = zap_fndbyte(arr,sizeof (arr),0x7u); + pos = zap_memfnd(arr,sizeof (arr),0x7u); zaptest_chk(pos,0x2u,zap_sz,"%zX"); - pos = zap_fndbyte(arr,sizeof (arr),0xFu); + pos = zap_memfnd(arr,sizeof (arr),0xFu); zaptest_chk(pos,0x3u,zap_sz,"%zX"); - pos = zap_fndbyte(arr,sizeof (arr),0x1Fu); + pos = zap_memfnd(arr,sizeof (arr),0x1Fu); zaptest_chk(pos,0x4u,zap_sz,"%zX"); - pos = zap_fndbyte(arr,sizeof (arr),0x3Fu); + pos = zap_memfnd(arr,sizeof (arr),0x3Fu); zaptest_chk(pos,0x5u,zap_sz,"%zX"); - pos = zap_fndbyte(arr,sizeof (arr),0x7Fu); + pos = zap_memfnd(arr,sizeof (arr),0x7Fu); zaptest_chk(pos,0x6u,zap_sz,"%zX"); - pos = zap_fndbyte(arr,sizeof (arr),0xFFu); + pos = zap_memfnd(arr,sizeof (arr),0xFFu); zaptest_chk(pos,0x7u,zap_sz,"%zX"); - pos = zap_fndbyte(arr,sizeof (arr),0xFEu); + pos = zap_memfnd(arr,sizeof (arr),0xFEu); zaptest_chk(pos,zap_nopos,zap_sz,"%zX"); return false; } diff --git a/test/source/test/foreach.i b/test/source/test/memfor.i index e7b933d..0d6768e 100644 --- a/test/source/test/foreach.i +++ b/test/source/test/memfor.i @@ -1,20 +1,20 @@ #include <stdbool.h> -static void foreachfn(void * const _ptr) { +static void memforfn(void * const _ptr) { unsigned int * ptr = _ptr; unsigned int val = *ptr; val %= 0x2; *ptr = val; } -static bool zaptest_test_foreach(void) { +static bool zaptest_test_memfor(void) { unsigned int arr[] = { 0x0u, 0x1u, 0x2u, 0x3u, }; - zap_foreach(arr,sizeof (arr[0x0u]),sizeof (arr) / sizeof (arr[0x0u]),foreachfn); + zap_memfor(arr,sizeof (arr[0x0u]),sizeof (arr) / sizeof (arr[0x0u]),memforfn); zaptest_chk(arr[0x0u],0x0u,unsigned int,"%X"); zaptest_chk(arr[0x1u],0x1u,unsigned int,"%X"); zaptest_chk(arr[0x2u],0x0u,unsigned int,"%X"); diff --git a/test/source/test/fndchr.i b/test/source/test/strfnd.i index 8fb670a..a85f227 100644 --- a/test/source/test/fndchr.i +++ b/test/source/test/strfnd.i @@ -1,12 +1,12 @@ #include <stdbool.h> -static bool zaptest_test_fndchr(void) { +static bool zaptest_test_strfnd(void) { char const str[] = "What the drake doing?"; - zap_sz pos = zap_fndchr(str,'?'); + zap_sz pos = zap_strfnd(str,'?'); zaptest_chk(pos,0x14u,zap_sz,"%zX"); - pos = zap_fndchr(str,'t'); + pos = zap_strfnd(str,'t'); zaptest_chk(pos,0x3u,zap_sz,"%zX"); - pos = zap_fndchr(str,'!'); + pos = zap_strfnd(str,'!'); zaptest_chk(pos,zap_nopos,zap_sz,"%zX"); return false; } diff --git a/zap/GNUmakefile b/zap/GNUmakefile index e0de7c4..18f94eb 100644 --- a/zap/GNUmakefile +++ b/zap/GNUmakefile @@ -35,17 +35,18 @@ CPPFLAGS := \ # ARTEFACTS OBJS = \ - source/$(arch)/mem/fndbyte.o \ - source/$(arch)/mem/fndchr.o \ - source/$(arch)/mem/foreach.o \ source/$(arch)/mem/memcat.o \ + source/$(arch)/mem/memcnt.o \ source/$(arch)/mem/memcp.o \ source/$(arch)/mem/memeq.o \ source/$(arch)/mem/memfill.o \ + source/$(arch)/mem/memfnd.o \ + source/$(arch)/mem/memfor.o \ source/$(arch)/mem/memgen.o \ source/$(arch)/mem/strcat.o \ source/$(arch)/mem/streq.o \ source/$(arch)/mem/strfill.o \ + source/$(arch)/mem/strfnd.o \ source/$(arch)/mem/strcp.o \ source/$(arch)/mem/strlen.o \ source/$(arch)/mem/utf20len.o \ diff --git a/zap/include/zap/bs.h b/zap/include/zap/bs.h index 1204690..7e14502 100644 --- a/zap/include/zap/bs.h +++ b/zap/include/zap/bs.h @@ -30,7 +30,7 @@ typedef signed char zap_cmp; typedef unsigned long zap_sz; -#define zap_ver ((unsigned long)+0x12u) +#define zap_ver ((unsigned long)+0x13u) #if defined(__cplusplus) } diff --git a/zap/include/zap/mem.h b/zap/include/zap/mem.h index d69c45f..8bcaa0b 100644 --- a/zap/include/zap/mem.h +++ b/zap/include/zap/mem.h @@ -13,18 +13,19 @@ extern "C" { #endif -__attribute__ ((hot,nothrow,warn_unused_result)) zap_sz zap_fndbyte( void const * ptr, zap_sz num, unsigned char byte); -__attribute__ ((hot,nothrow,warn_unused_result)) zap_sz zap_fndchr( char const * str, char chr); -__attribute__ ((hot)) void zap_foreach( void * ptr, zap_sz sz, zap_sz num, void (* fn)(void *)); -__attribute__ ((hot,nothrow)) void zap_memcat( void const * lptr, zap_sz llen,void const * rptr, zap_sz rlen, void * buf); +__attribute__ ((hot,nothrow)) void zap_memcat( void const * lptr, zap_sz llen,void const * rptr, zap_sz rlen, void * buf); +__attribute__ ((hot,warn_unused_result)) zap_sz zap_memcnt( void const * ptr, zap_sz sz, zap_sz num, zap_bool (* fn)(void const *)); __attribute__ ((hot,nothrow)) void zap_memcp( void const * in, zap_sz num, void * out); __attribute__ ((hot,nothrow,warn_unused_result)) zap_bool zap_memeq( void const * lptr, zap_sz num, void const * rptr); __attribute__ ((hot,nothrow)) void zap_memfill( void * ptr, zap_sz num, unsigned char val); -__attribute__ ((hot)) void zap_memgen( void * ptr, zap_sz sz, zap_sz num, void (* fn)(zap_sz,void *)); +__attribute__ ((hot,nothrow,warn_unused_result)) zap_sz zap_memfnd( void const * ptr, zap_sz num, unsigned char byte); +__attribute__ ((hot)) void zap_memfor( void * ptr, zap_sz sz, zap_sz num, void (* fn)(void *)); +__attribute__ ((hot)) void zap_memgen( void * ptr, zap_sz sz, zap_sz num, void (* fn)(zap_sz,void *)); __attribute__ ((hot,nothrow)) void zap_strcat( char const * lstr, char const * rstr,char * buf); __attribute__ ((hot,nothrow)) zap_sz zap_strcp( char const * in, char * out); __attribute__ ((hot,nothrow,warn_unused_result)) zap_bool zap_streq( char const * lstr, char const * rstr); __attribute__ ((hot,nothrow)) zap_sz zap_strfill( char * lstr, char chr); +__attribute__ ((hot,nothrow,warn_unused_result)) zap_sz zap_strfnd( char const * str, char chr); __attribute__ ((hot,nothrow,warn_unused_result)) zap_sz zap_strlen( char const * str); __attribute__ ((hot,nothrow,warn_unused_result)) zap_sz zap_utf20len( zap_chr20 const * utf20); __attribute__ ((hot,nothrow)) void zap_utf8dec( zap_chr8 const * in, zap_chr20 * out); diff --git a/zap/source/amd64/mem/memcat.c b/zap/source/amd64/mem/memcat.c index f3e9a9b..2b50ba7 100644 --- a/zap/source/amd64/mem/memcat.c +++ b/zap/source/amd64/mem/memcat.c @@ -8,8 +8,6 @@ #include <zap/mem.h> -#include <stddef.h> - void zap_memcat(void const * const _lptr,zap_sz const _llen,void const * const _rptr,zap_sz const _rlen,void * const _buf) { zap_memcp(_lptr,_llen,_buf); zap_memcp(_rptr,_rlen,(unsigned char *)_buf + _llen); diff --git a/zap/source/amd64/mem/memcnt.c b/zap/source/amd64/mem/memcnt.c new file mode 100644 index 0000000..c22abca --- /dev/null +++ b/zap/source/amd64/mem/memcnt.c @@ -0,0 +1,22 @@ +/* + Copyright 2022 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ + +#include <zap/priv.h> + +#include <zap/mem.h> + +zap_sz zap_memcnt(void const * const _ptr,zap_sz const _sz,zap_sz const _num,zap_bool (* const _fn)(void const *)) { + unsigned char const * addr = _ptr; + zap_sz const numbyte = _num * _sz; + unsigned char const * const afterbuf = addr + numbyte; + zap_sz num = 0x0u; + for (;addr != afterbuf;addr += _sz) { + if (_fn(addr)) { + ++num; + } + } + return num; +} diff --git a/zap/source/amd64/mem/fndbyte.S b/zap/source/amd64/mem/memfnd.S index af2ee84..0b5a47e 100644 --- a/zap/source/amd64/mem/fndbyte.S +++ b/zap/source/amd64/mem/memfnd.S @@ -2,9 +2,9 @@ # This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. # If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. -.globl zap_fndbyte +.globl zap_memfnd -zap_fndbyte: +zap_memfnd: # Address of the current element: #define addr %rax # Address of the first element: diff --git a/zap/source/amd64/mem/foreach.S b/zap/source/amd64/mem/memfor.S index 6766933..be705b9 100644 --- a/zap/source/amd64/mem/foreach.S +++ b/zap/source/amd64/mem/memfor.S @@ -2,9 +2,9 @@ # This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. # If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. -.globl zap_foreach +.globl zap_memfor -zap_foreach: +zap_memfor: # Address of the current element: #define addr %rbx # Address of the element after the last input element: diff --git a/zap/source/amd64/mem/fndchr.S b/zap/source/amd64/mem/strfnd.S index 67e0ea1..d9c99f3 100644 --- a/zap/source/amd64/mem/fndchr.S +++ b/zap/source/amd64/mem/strfnd.S @@ -2,9 +2,9 @@ # This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. # If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. -.globl zap_fndchr +.globl zap_strfnd -zap_fndchr: +zap_strfnd: # Address of the first character: #define start %rdi # Character to be found: |