Skip to content

Commit

Permalink
Refine __builtin_clz compatibility check
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaByte committed Nov 15, 2015
1 parent 9c24ac1 commit 2270ffe
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/zopfli/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ Author: [email protected] (Jyrki Alakuijala)
#include <stdio.h>
#include <stdlib.h>

/* __has_builtin available in clang */
#ifdef __has_builtin
# if __has_builtin(__builtin_clz)
# define HAS_BUILTIN_CLZ
# endif
/* __builtin_clz available beginning with GCC 3.4 */
#elif __GNUC__ * 100 + __GNUC_MINOR__ >= 304
# define HAS_BUILTIN_CLZ
#endif

int ZopfliGetDistExtraBits(int dist) {
#ifdef __GNUC__
#ifdef HAS_BUILTIN_CLZ
if (dist < 5) return 0;
return (31 ^ __builtin_clz(dist - 1)) - 1; /* log2(dist - 1) - 1 */
#else
Expand All @@ -48,7 +58,7 @@ int ZopfliGetDistExtraBits(int dist) {
}

int ZopfliGetDistExtraBitsValue(int dist) {
#ifdef __GNUC__
#ifdef HAS_BUILTIN_CLZ
if (dist < 5) {
return 0;
} else {
Expand All @@ -74,7 +84,7 @@ int ZopfliGetDistExtraBitsValue(int dist) {
}

int ZopfliGetDistSymbol(int dist) {
#ifdef __GNUC__
#ifdef HAS_BUILTIN_CLZ
if (dist < 5) {
return dist - 1;
} else {
Expand Down

0 comments on commit 2270ffe

Please sign in to comment.