shithub: dav1d

Download patch

ref: 6751c9803695a2f12afb8f2cf080a4f9c2c8973a
parent: 3d94fb9aff5d2837c9ee0c13fff3d4e2424623ae
author: Henrik Gramner <gramner@twoorioles.com>
date: Fri Aug 9 18:41:18 EDT 2019

Utilize the constraints in assertions to improve code generation

When compiling in release mode, instead of just deleting assertions,
use them to give hints to the compiler. This allows for slightly
better code generation in some cases.

--- a/examples/dav1dplay.c
+++ b/examples/dav1dplay.c
@@ -27,14 +27,14 @@
 #include "config.h"
 #include "vcs_version.h"
 
-#include <stdio.h>
+#include <getopt.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <string.h>
-#include <assert.h>
-#include <getopt.h>
 
 #include <SDL.h>
 
+#include "common/attributes.h"
 
 #include "dav1d/dav1d.h"
 
--- a/include/common/attributes.h
+++ b/include/common/attributes.h
@@ -92,6 +92,14 @@
 #define NOINLINE __attribute__((noinline))
 #endif /* !_MSC_VER */
 
+#if defined(NDEBUG) && defined(__GNUC__)
+#define assert(x) do { if (!(x)) __builtin_unreachable(); } while (0)
+#elif defined(NDEBUG) && defined(_MSC_VER)
+#define assert __assume
+#else
+#include <assert.h>
+#endif
+
 #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
 #    define dav1d_uninit(x) x=x
 #else
--- a/include/common/mem.h
+++ b/include/common/mem.h
@@ -28,12 +28,13 @@
 #ifndef DAV1D_COMMON_MEM_H
 #define DAV1D_COMMON_MEM_H
 
-#include <assert.h>
 #include <stdlib.h>
 
 #if defined(HAVE_ALIGNED_MALLOC) || defined(HAVE_MEMALIGN)
 #include <malloc.h>
 #endif
+
+#include "common/attributes.h"
 
 /*
  * Allocate 32-byte aligned memory. The return value can be released
--- a/src/cdef_tmpl.c
+++ b/src/cdef_tmpl.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <stdlib.h>
 
 #include "common/intops.h"
--- a/src/cdf.c
+++ b/src/cdf.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <string.h>
 
 #include "src/thread.h"
--- a/src/data.c
+++ b/src/data.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -35,6 +34,7 @@
 
 #include "dav1d/data.h"
 
+#include "common/attributes.h"
 #include "common/validate.h"
 
 #include "src/data.h"
--- a/src/env.h
+++ b/src/env.h
@@ -28,7 +28,6 @@
 #ifndef DAV1D_SRC_ENV_H
 #define DAV1D_SRC_ENV_H
 
-#include <assert.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdlib.h>
--- a/src/film_grain_tmpl.c
+++ b/src/film_grain_tmpl.c
@@ -28,7 +28,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <stdint.h>
 
 #include "common.h"
--- a/src/getbits.c
+++ b/src/getbits.c
@@ -27,8 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
-
 #include "common/intops.h"
 
 #include "src/getbits.h"
--- a/src/intra_edge.c
+++ b/src/intra_edge.c
@@ -27,8 +27,9 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <stdlib.h>
+
+#include "common/attributes.h"
 
 #include "src/intra_edge.h"
 #include "src/levels.h"
--- a/src/ipred_prepare_tmpl.c
+++ b/src/ipred_prepare_tmpl.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <stdint.h>
 #include <string.h>
 
--- a/src/ipred_tmpl.c
+++ b/src/ipred_tmpl.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 
--- a/src/itx_tmpl.c
+++ b/src/itx_tmpl.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <string.h>
--- a/src/lf_apply_tmpl.c
+++ b/src/lf_apply_tmpl.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <string.h>
 
 #include "common/intops.h"
--- a/src/lf_mask.c
+++ b/src/lf_mask.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <string.h>
 
 #include "common/intops.h"
--- a/src/mc_tmpl.c
+++ b/src/mc_tmpl.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 
--- a/src/msac.h
+++ b/src/msac.h
@@ -28,9 +28,10 @@
 #ifndef DAV1D_SRC_MSAC_H
 #define DAV1D_SRC_MSAC_H
 
-#include <assert.h>
 #include <stdint.h>
 #include <stdlib.h>
+
+#include "common/attributes.h"
 
 typedef size_t ec_win;
 
--- a/src/obu.c
+++ b/src/obu.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <limits.h>
 #include <stdio.h>
@@ -1275,8 +1274,10 @@
             c->frame_hdr_ref = dav1d_ref_create(sizeof(Dav1dFrameHeader));
             if (!c->frame_hdr_ref) return DAV1D_ERR(ENOMEM);
         }
+#ifndef NDEBUG
         // ensure that the reference is writable
         assert(dav1d_ref_is_writable(c->frame_hdr_ref));
+#endif
         c->frame_hdr = c->frame_hdr_ref->data;
         memset(c->frame_hdr, 0, sizeof(*c->frame_hdr));
         c->frame_hdr->temporal_id = temporal_id;
--- a/src/picture.c
+++ b/src/picture.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <stdint.h>
 #include <stdio.h>
--- a/src/ppc/cdef_init_tmpl.c
+++ b/src/ppc/cdef_init_tmpl.c
@@ -24,16 +24,15 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <assert.h>
 #include <stdlib.h>
 
-#include "src/ppc/types.h"
-
 #include "common/bitdepth.h"
+#include "common/intops.h"
 
-#include "src/cpu.h"
 #include "src/cdef.h"
-#include "common/intops.h"
+#include "src/cpu.h"
+
+#include "src/ppc/types.h"
 
 #if BITDEPTH == 8
 static inline i16x8 vconstrain(const i16x8 diff, const int16_t threshold,
--- a/src/ref_mvs.c
+++ b/src/ref_mvs.c
@@ -47,7 +47,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <limits.h>
 #include <stddef.h>
--- a/src/warpmv.c
+++ b/src/warpmv.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <stdlib.h>
 
 #include "common/intops.h"
--- a/tests/checkasm/mc.c
+++ b/tests/checkasm/mc.c
@@ -27,8 +27,6 @@
 
 #include "tests/checkasm/checkasm.h"
 
-#include <assert.h>
-
 #include "src/levels.h"
 #include "src/mc.h"
 
--- a/tools/dav1d.c
+++ b/tools/dav1d.c
@@ -29,7 +29,6 @@
 #include "vcs_version.h"
 #include "cli_config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <math.h>
--- a/tools/dav1d_cli_parse.c
+++ b/tools/dav1d_cli_parse.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <getopt.h>
 #include <limits.h>
 #include <math.h>
--- a/tools/input/annexb.c
+++ b/tools/input/annexb.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdint.h>
--- a/tools/input/input.c
+++ b/tools/input/input.c
@@ -27,11 +27,12 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include "common/attributes.h"
 
 #include "input/input.h"
 #include "input/demuxer.h"
--- a/tools/input/ivf.c
+++ b/tools/input/ivf.c
@@ -27,7 +27,6 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdint.h>
--- a/tools/output/output.c
+++ b/tools/output/output.c
@@ -27,11 +27,12 @@
 
 #include "config.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include "common/attributes.h"
 
 #include "output/output.h"
 #include "output/muxer.h"