sox-devel@lists.sourceforge.net unofficial mirror
 help / color / mirror / code / Atom feed
From: "Måns Rullgård" <mans@mansr.com>
To: Eric Wong <normalperson@yhbt.net>
Cc: sox-devel@lists.sourceforge.net
Subject: Re: [PATCH 4/6] Add macros for increasing data alignment
Date: Mon, 21 Dec 2015 11:37:42 +0000	[thread overview]
Message-ID: <yw1x1tag6ni1.fsf@unicorn.mansr.com> (raw)
In-Reply-To: <20151221105504.GA30660@dcvr.yhbt.net> (Eric Wong's message of "Mon, 21 Dec 2015 10:55:04 +0000")

Eric Wong <normalperson@yhbt.net> writes:

> Måns Rullgård <mans@mansr.com> wrote:
>> That CPU doesn't have AVX so 16-byte alignment is enough, and plain
>> malloc usually provides that.  It obviously doesn't hurt to add support
>> for memalign as well even though it is considered obsolete.  If you do
>> that, you should also take care of #including malloc.h.
>
> Yes, added memalign(3) and posix_memalign(3) fallbacks.
> I also went ahead and stole a bit from Ruby to provide a
> fallback when none of the 3 functions exist.
>
> Will still need to deal with systems without LSX_ALIGN...

Almost all compilers support either the GNU or the MSVC syntax.  The few
that don't probably don't support AVX anyhow.  Mostly it's compilers for
obscure DSPs.

> Anyways, pushed the following to git://bogomips.org/sox ew/align
>
> -----------------8<------------------
> Subject: [PATCH] always support aligned heap allocation
>
> The new sdm effect will not work correctly on AVX systems without
> 32-byte alignment; so we need to support older systems without
> C11 aligned_alloc.
>
> The fallback emulation is based on code found in gc.c in Ruby 2.0+
> (BSD-licensed)
> ---
>  configure.ac | 11 ++++++++++-
>  src/util.h   | 31 +++++++++++++++++++++++++++++--
>  2 files changed, 39 insertions(+), 3 deletions(-)
>
> diff --git a/configure.ac b/configure.ac
> index 8570638..017a9dd 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -207,7 +207,16 @@ AC_HEADER_STDC
>  AC_CHECK_HEADERS(fcntl.h unistd.h byteswap.h sys/stat.h sys/time.h sys/timeb.h sys/types.h sys/utsname.h termios.h glob.h fenv.h)
>
>  dnl Checks for library functions.
> -AC_CHECK_FUNCS(strcasecmp strdup popen vsnprintf gettimeofday mkstemp fmemopen aligned_alloc)
> +AC_CHECK_FUNCS(strcasecmp strdup popen vsnprintf gettimeofday mkstemp fmemopen)
> +
> +dnl aligned alloc required for sdm using AVX (32-byte) or SSE2 (16-byte)
> +AC_CHECK_FUNCS(aligned_alloc)
> +AS_IF([test "x$ac_cv_func_aligned_alloc" != xyes], [
> +	AC_CHECK_FUNCS([memalign])
> +	AS_IF([test "x$ac_cv_func_memalign" != xyes], [
> +		AC_CHECK_FUNCS([posix_memalign])
> +	])
> +])

Why don't you just add (posix_)memalign to the existing list of
functions?  It's not an error if some of them don't exist, and you're
checking the resulting HAVE_ macros in the same order anyway.

>  dnl Check if math library is needed.
>  AC_SEARCH_LIBS([pow], [m])
> diff --git a/src/util.h b/src/util.h
> index b5cc9b8..3a9686d 100644
> --- a/src/util.h
> +++ b/src/util.h
> @@ -196,12 +196,39 @@
>
>  #ifdef HAVE_ALIGNED_ALLOC
>    #define aligned_free(p) free(p)
> +#elif defined(HAVE_MEMALIGN)
> +  #include <malloc.h>
> +  #define aligned_alloc(a, s) memalign(a, s)
> +  #define aligned_free(p) free(p)
> +#elif defined(HAVE_POSIX_MEMALIGN)
> +  #include <errno.h>
> +static inline void *sox_aligned_alloc_pm(size_t align, size_t size)
> +{
> +  void *ptr;
> +  int err = posix_memalign(&ptr, align, size);
> +
> +  if (!err) return ptr;
> +  errno = err;
> +  return 0;
> +}
> +  #define aligned_alloc(a, s) sox_aligned_alloc_pm(a, s)
> +  #define aligned_free(p) free(p)
>  #elif defined _MSC_VER
>    #define aligned_alloc(a, s) _aligned_malloc(s, a)
>    #define aligned_free(p) _aligned_free(p)
>  #else
> -  #define aligned_alloc(a, s) malloc(s)
> -  #define aligned_free(p) free(p)
> +static inline void *sox_aligned_alloc_m(size_t align, size_t size)
> +{
> +  void *res = malloc(align + size + sizeof(void *));
> +  char *aligned = (char *)res + align + sizeof(void *);
> +
> +  aligned -= ((size_t)aligned & (align - 1));

Use uintptr_t rather than size_t there.  Although they are usually the
same underlying type, there is no such guarantee, especially for systems
bizarre enough not to have an aligned allocation function.

> +  ((void **)aligned)[-1] = res;
> +  return (void *)aligned;
> +}
> +
> +  #define aligned_alloc(a, s) sox_aligned_alloc_m(a, s)
> +  #define aligned_free(p) free(((void**)p)[-1]);
>  #endif
>
>  /*------------------------------- Maths stuff --------------------------------*/
> -- 
> EW

-- 
Måns Rullgård

------------------------------------------------------------------------------

  reply	other threads:[~2015-12-21 11:38 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-16 14:16 [PATCH 1/6] Add SOX_ENCODING_DSD Mans Rullgard
2015-09-16 14:16 ` [PATCH 2/6] Add DSF file support Mans Rullgard
2015-09-16 14:16 ` [PATCH 3/6] Add support for reading DSDIFF files Mans Rullgard
2015-09-16 14:16 ` [PATCH 4/6] Add macros for increasing data alignment Mans Rullgard
2015-12-20 12:41   ` Eric Wong
2015-12-20 13:54     ` Måns Rullgård
2015-12-21 10:55       ` Eric Wong
2015-12-21 11:37         ` Måns Rullgård [this message]
2015-12-21 18:33           ` Eric Wong
2015-12-21 18:37             ` Måns Rullgård
2015-09-16 14:16 ` [PATCH 5/6] Add a sigma-delta modulator for DSD encoding Mans Rullgard
2015-10-03 22:31   ` Eric Wong
2015-10-03 22:39     ` Måns Rullgård
2015-09-16 14:16 ` [PATCH 6/6] Add DSD over PCM (dop) effect Mans Rullgard
2015-12-19 12:09   ` Eric Wong
2015-12-19 12:14     ` Måns Rullgård
2015-12-20  5:04       ` Eric Wong
2015-12-20 13:44         ` Måns Rullgård

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.sourceforge.net/lists/listinfo/sox-devel

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=yw1x1tag6ni1.fsf@unicorn.mansr.com \
    --to=sox-devel@lists.sourceforge.net \
    --cc=normalperson@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/sox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).