Compare commits

3 Commits

Author SHA1 Message Date
9d71467182 rwonce.h: __error__ attribute fix 2022-06-01 17:55:48 +02:00
d6eccb2caa bits.h: add 32 bits macros 2022-06-01 17:54:01 +02:00
97aea8ddf2 sync.sh: some shellcheck fixes 2022-06-01 17:46:57 +02:00
4 changed files with 118 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# sync-conf-example.sh - a "sync.sh" configuration file example.
#
@@ -22,9 +22,9 @@
# SOURCEDIR=""
# SERVER=""
# DESTDIR=""
SOURCEDIR=/example-srcdir
SERVER=root@backuphost
DESTDIR=/mnt/nas1/example-destdir
export SOURCEDIR=/example-srcdir
export SERVER=root@backuphost
export DESTDIR=/mnt/nas1/example-destdir
###### backups to keep
# NYEARS=3
@@ -96,7 +96,7 @@ aftersync() {
log -s -t "calling user aftersync"
}
# For Emacs, shell-mode:
# For Emacs, shell-script-mode:
# Local Variables:
# mode: shell-script
# End:

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
#
# sync.sh - a backup utility using ssh/rsync facilities.
#
@@ -480,7 +480,7 @@ parse_opts() {
printf "%s: Cannot open $CONFIG file. Exiting.\n" "$CMDNAME"
exit 9
fi
# shellcheck source=/dev/null
# shellcheck source=sync-conf-example.sh
source "$CONFIG"
LOCKDIR="/tmp/$CMDNAME-$HOSTNAME-${CONFIG##*/}.lock"

111
c/bits.h
View File

@@ -77,6 +77,28 @@ static inline int ctz64(u64 n)
# endif
}
static inline int ctz32(u32 n)
{
# if __has_builtin(__builtin_ctz)
# ifdef DEBUG_BITS
log_f(1, "builtin ctz.\n");
# endif
return __builtin_ctzl(n);
# elif __has_builtin(__builtin_clz)
# ifdef DEBUG_BITS
log_f(1, "builtin clz.\n");
# endif
return __WORDSIZE - (__builtin_clz(n & -n) + 1);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount32((n & n) 1);
# endif
}
/* count leading zeroes : 00101000 -> 2
* ^^
*/
@@ -100,7 +122,30 @@ static inline int clz64(u64 n)
q = (n > 0xF ) << 2; n >>= q; r |= q;
q = (n > 0x3 ) << 1; n >>= q; r |= q;
r |= (n >> 1);
return __WORDSIZE - r - 1;
return 64 - r - 1;
# endif
}
static inline int clz32(u32 n)
{
# if __has_builtin(__builtin_clz)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_clz(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
u32 r, q;
r = (n > 0xFFFF) << 4; n >>= r;
q = (n > 0xFF ) << 3; n >>= q; r |= q;
q = (n > 0xF ) << 2; n >>= q; r |= q;
q = (n > 0x3 ) << 1; n >>= q; r |= q;
r |= (n >> 1);
return 32 - r - 1;
# endif
}
@@ -113,7 +158,7 @@ static inline uint ffs64(u64 n)
# ifdef DEBUG_BITS
log_f(1, "builtin ffsl.\n");
# endif
return __builtin_ffsll(n);
return __builtin_ffsl(n);
# elif __has_builtin(__builtin_ctzl)
# ifdef DEBUG_BITS
@@ -131,6 +176,33 @@ static inline uint ffs64(u64 n)
# endif
}
static inline uint ffs32(u32 n)
{
# if __has_builtin(__builtin_ffs)
# ifdef DEBUG_BITS
log_f(1, "builtin ffs.\n");
# endif
return __builtin_ffs(n);
# elif __has_builtin(__builtin_ctz)
# ifdef DEBUG_BITS
log_f(1, "builtin ctz.\n");
# endif
if (n == 0)
return (0);
return __builtin_ctz(n) + 1;
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount32(n ^ ~-n);
# endif
}
/* count set bits: 10101000 -> 3
* ^ ^ ^
*/
static inline int popcount64(u64 n)
{
# if __has_builtin(__builtin_popcountl)
@@ -152,10 +224,31 @@ static inline int popcount64(u64 n)
# endif
}
/** bit_for_each64 - iterate over an u64 bits
static inline int popcount32(u32 n)
{
# if __has_builtin(__builtin_popcount)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_popcount(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
return count;
# endif
}
/** bit_for_each - iterate over an u64/u32 bits
* @pos: an int used as current bit
* @tmp: a temp u64 used as temporary storage
* @ul: the u64 to loop over
* @tmp: a temp u64/u32 used as temporary storage
* @ul: the u64/u32 to loop over
*
* Usage:
* u64 u=139, _t; // u=b10001011
@@ -170,9 +263,15 @@ static inline int popcount64(u64 n)
#define bit_for_each64(pos, tmp, ul) \
for (tmp = ul, pos = ffs64(tmp); tmp; tmp &= (tmp - 1), pos = ffs64(tmp))
#define bit_for_each32(pos, tmp, ul) \
for (tmp = ul, pos = ffs32(tmp); tmp; tmp &= (tmp - 1), pos = ffs32(tmp))
/** or would it be more useful (counting bits from zero instead of 1) ?
*/
#define bit_for_each64_2(pos, tmp, ul) \
for (tmp = ul, pos = ctz64(tmp); tmp; tmp ^= 1UL<<pos, pos = ctz64(tmp))
for (tmp = ul, pos = ctz64(tmp); tmp; tmp ^= 1UL << pos, pos = ctz64(tmp))
#define bit_for_each32_2(pos, tmp, ul) \
for (tmp = ul, pos = ctz32(tmp); tmp; tmp ^= 1U << pos, pos = ctz32(tmp))
#endif /* BITS_H */

View File

@@ -24,8 +24,12 @@
#ifndef __BR_RWONCE_H
#define __BR_RWONCE_H
/************ originally in <include/linux/compiler_types.h> */
# define __compiletime_error(message) __attribute__((error(message)))
/************ originally in <include/linux/compiler_attributes.h> */
#if __has_attribute(__error__)
# define __compiletime_error(msg) __attribute__((__error__(msg)))
#else
# define __compiletime_error(msg)
#endif
/************ originally in <include/linux/compiler_types.h> */
/*