From c14be0eb905eea524ed4d07b2a8eeb71b6c9784c Mon Sep 17 00:00:00 2001 From: eddyem Date: Mon, 9 Feb 2015 08:34:17 +0300 Subject: [PATCH] fixed log_2 for ARM arch --- B-trees.c | 8 +- Trinamic/client/client.c | 8 +- Trinamic/websock_simple/test.c | 8 +- erosion-dilation/main.c | 4 +- randline.c | 167 +++++++++++++++++++++++++++++++++ usefull_macros.c | 14 +++ 6 files changed, 189 insertions(+), 20 deletions(-) create mode 100644 randline.c diff --git a/B-trees.c b/B-trees.c index aba8516..1f7a39d 100644 --- a/B-trees.c +++ b/B-trees.c @@ -56,12 +56,8 @@ void *my_alloc(size_t N, size_t S){ #endif static inline uint32_t log_2(const uint32_t x) { - uint32_t y; - asm ( "\tbsr %1, %0\n" - : "=r"(y) - : "r" (x) - ); - return y; + if(x == 0) return 0; + return (31 - __builtin_clz (x)); } // later this function maybe mode difficult diff --git a/Trinamic/client/client.c b/Trinamic/client/client.c index 643543f..1bf1a1c 100644 --- a/Trinamic/client/client.c +++ b/Trinamic/client/client.c @@ -455,12 +455,8 @@ void copy_buf_to_file(uint8_t *buffer, int *cmd){ } static inline uint32_t log_2(const uint32_t x) { - uint32_t y; - asm ( "\tbsr %1, %0\n" - : "=r"(y) - : "r" (x) - ); - return y; + if(x == 0) return 0; + return (31 - __builtin_clz (x)); } #define log2(x) ((int)log_2((uint32_t)x)) diff --git a/Trinamic/websock_simple/test.c b/Trinamic/websock_simple/test.c index f9a9df3..341fc6f 100644 --- a/Trinamic/websock_simple/test.c +++ b/Trinamic/websock_simple/test.c @@ -117,12 +117,8 @@ size_t read_tty(){ } static inline uint32_t log_2(const uint32_t x){ - uint32_t y; - asm ( "\tbsr %1, %0\n" - : "=r"(y) - : "r" (x) - ); - return y; + if(x == 0) return 0; + return (31 - __builtin_clz (x)); } int send_command(uint8_t *ninebytes); diff --git a/erosion-dilation/main.c b/erosion-dilation/main.c index 3aaff7a..da610ca 100644 --- a/erosion-dilation/main.c +++ b/erosion-dilation/main.c @@ -19,15 +19,15 @@ * MA 02110-1301, USA. */ -#include #include +#include #include "binmorph.h" // these includes are for randini #include #include -#include #include +#include /* * Generate a quasy-random number to initialize PRNG * name: throw_random_seed diff --git a/randline.c b/randline.c new file mode 100644 index 0000000..1d639e8 --- /dev/null +++ b/randline.c @@ -0,0 +1,167 @@ +/* + * randline.c - shows N lines with random number from choosen file + * + * COMPILE: gcc -lm -Wall -Werror -Wextra randline.c -o randline + * + * Copyright 2014 Edward V. Emelianoff + * + * 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; either version 2 of the License, or + * (at your option) any later version. + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ERR(...) do{fprintf(stderr, __VA_ARGS__); return NULL;}while(0) +#define _(s) s +#define MALLOC(type, size) ((type *)my_alloc(size, sizeof(type))) + +// mmap file +typedef struct{ + char *data; + size_t len; +} mmapbuf; + +/** + * function for different purposes that need to know time intervals + * @return double value: time in seconds + */ +double dtime(){ + double t; + struct timeval tv; + gettimeofday(&tv, NULL); + t = tv.tv_sec + ((double)tv.tv_usec)/1e6; + return t; +} +/* + * Generate a quasy-random number to initialize PRNG + * name: throw_random_seed + * @return value for srand48 + */ +long throw_random_seed(){ + long r_ini; + int fail = 0; + int fd = open("/dev/random", O_RDONLY); + do{ + if(-1 == fd){ + perror(_("Can't open /dev/random")); + fail = 1; break; + } + if(sizeof(long) != read(fd, &r_ini, sizeof(long))){ + perror(_("Can't read /dev/random")); + fail = 1; + } + close(fd); + }while(0); + if(fail){ + double tt = dtime() * 1e6; + double mx = (double)LONG_MAX; + r_ini = (long)(tt - mx * floor(tt/mx)); + } + return (r_ini); +} + + +void* my_alloc(size_t N, size_t S){ + void *p = calloc(N, S); + if(!p){ + perror(_("can't allocate memory")); + exit(-1); + } + return p; +} +/** + * Mmap file to a memory area + * + * @param filename (i) - name of file to mmap + * @return stuct with mmap'ed file or die + */ +mmapbuf *My_mmap(char *filename){ + int fd; + char *ptr; + size_t Mlen; + struct stat statbuf; + if(!filename) ERR(_("No filename given!")); + if((fd = open(filename, O_RDONLY)) < 0) + ERR(_("Can't open %s for reading"), filename); + if(fstat (fd, &statbuf) < 0) + ERR(_("Can't stat %s"), filename); + Mlen = statbuf.st_size; + if((ptr = mmap (0, Mlen, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) + ERR(_("Mmap error for input")); + if(close(fd)) ERR(_("Can't close mmap'ed file")); + mmapbuf *ret = MALLOC(mmapbuf, 1); + ret->data = ptr; + ret->len = Mlen; + return ret; +} + +void My_munmap(mmapbuf *b){ + if(munmap(b->data, b->len)) + fprintf(stderr, _("Can't munmap")); + free(b); + b = NULL; +} + +int main(int argc, char **argv){ + char *endptr, *buf, *startptr; + mmapbuf *mbuf; + ptrdiff_t MAX; + size_t N_strings; + void usage(){ + fprintf(stderr, _("USAGE: %s filename Nlines\n"), argv[0]); + } + if(argc != 3){ + usage(); + return -1; + } + if(!(mbuf = My_mmap(argv[1]))) return -2; + MAX = mbuf->len; + buf = mbuf->data; + N_strings = strtol(argv[2], &endptr, 0); + if(endptr == argv[2] || *endptr != '\0'){ + usage(); + return -3; + } + srand48(throw_random_seed()); + while(N_strings--){ + size_t R = lrand48() % MAX; + startptr = strchr(buf+R, '\n'); + if(!startptr || ((++startptr-buf) >= MAX)){ + N_strings++; + continue; + } + endptr = strchr(startptr, '\n'); + if(!endptr || endptr <= startptr){ + N_strings++; + continue; + } + write(1, startptr, endptr-startptr); + write(1, "\n", 1); + } + My_munmap(mbuf); + return 0; +} + + diff --git a/usefull_macros.c b/usefull_macros.c index 524c771..d6f5b2a 100644 --- a/usefull_macros.c +++ b/usefull_macros.c @@ -21,6 +21,20 @@ #include "usefull_macros.h" + +#include +/** + * function for different purposes that need to know time intervals + * @return double value: time in seconds + */ +double dtime(){ + double t; + struct timeval tv; + gettimeofday(&tv, NULL); + t = tv.tv_sec + ((double)tv.tv_usec)/1e6; + return t; +} + /******************************************************************************\ * Coloured terminal \******************************************************************************/