Skip to content

Commit

Permalink
Suppress most warnings when compiling with clang. (#62)
Browse files Browse the repository at this point in the history
* Suppress most warnings when compiling with clang.

For builds with clang aiming to use -Wall or -Werror, this commit fixes or suppresses the following warnings:
- "-Wcast-align" is suppressed through pragma.
- "-Wdeclaration-after-statement" is suppressed through pragma.
- "-Wreserved-identifier" for functions that start with '_' are prefixed now with 'svb_'.
- "-Wsign-conversion" through explicit casting, changing integer literals or, as is the case of the lookup tables, by changing type directly.
- "-Wstrict-prototypes" through adding void ("f()" -> "f(void)").
- "-Wcast-qual" through explicit casting.
Note: ARM code has not been modified yet.

* Address PR #62

- Add STREAMVBYTE_WALL
- Add STREAMVBYTE_WERROR
- Add check for missing C11 stdatomic.

* Fix STREAMVBYTE_WALL & STREAMVBYTE_WERROR.

* Fix typo.

* Fix warnings in tests + enable Wall/Werror in CI.

* Fix some MSVC warnings.

* Add MSVC Wall workaround.

* Update unit.c

* Suppress unused variable warning in tests and examples.
  • Loading branch information
Marcel Pi authored May 20, 2023
1 parent a67097b commit c43294a
Show file tree
Hide file tree
Showing 27 changed files with 487 additions and 340 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ jobs:
run: |
mkdir builddebug &&
cd builddebug &&
cmake .. -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DCMAKE_C_COMPILER=${{matrix.compiler}} &&
cmake .. -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON -DCMAKE_C_COMPILER=${{matrix.compiler}} &&
cmake --build . &&
ctest --output-on-failure
- name: Use cmake (release)
run: |
mkdir buildrelease &&
cd buildrelease &&
cmake .. -DCMAKE_BUILD_TYPE=Release -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON &&
cmake .. -DCMAKE_BUILD_TYPE=Release -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON &&
cmake --build . &&
ctest --output-on-failure
- name: Use cmake (undefined sanitizer)
run: |
mkdir buildundefsani &&
cd buildundefsani &&
cmake .. -DSTREAMVBYTE_SANITIZE_UNDEFINED=ON -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON &&
cmake .. -DSTREAMVBYTE_SANITIZE_UNDEFINED=ON -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON &&
cmake --build . &&
ctest --output-on-failure
6 changes: 3 additions & 3 deletions .github/workflows/ubuntu20.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ jobs:
run: |
mkdir builddebug &&
cd builddebug &&
cmake .. -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DCMAKE_C_COMPILER=${{matrix.compiler}} &&
cmake .. -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON -DCMAKE_C_COMPILER=${{matrix.compiler}} &&
cmake --build . &&
ctest --output-on-failure
- name: Use cmake (release)
run: |
mkdir buildrelease &&
cd buildrelease &&
cmake .. -DCMAKE_BUILD_TYPE=Release -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON &&
cmake .. -DCMAKE_BUILD_TYPE=Release -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON &&
cmake --build . &&
ctest --output-on-failure
- name: Use cmake (undefined sanitizer)
run: |
mkdir buildundefsani &&
cd buildundefsani &&
cmake .. -DSTREAMVBYTE_SANITIZE_UNDEFINED=ON -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON &&
cmake .. -DSTREAMVBYTE_SANITIZE_UNDEFINED=ON -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON &&
cmake --build . &&
ctest --output-on-failure
6 changes: 3 additions & 3 deletions .github/workflows/ubuntu22.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ jobs:
run: |
mkdir builddebug &&
cd builddebug &&
cmake .. -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DCMAKE_C_COMPILER=${{matrix.compiler}} &&
cmake .. -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DCMAKE_C_COMPILER=${{matrix.compiler}} -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON &&
cmake --build . &&
ctest --output-on-failure
- name: Use cmake (release)
run: |
mkdir buildrelease &&
cd buildrelease &&
cmake .. -DCMAKE_BUILD_TYPE=Release -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON &&
cmake .. -DCMAKE_BUILD_TYPE=Release -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON &&
cmake --build . &&
ctest --output-on-failure
- name: Use cmake (undefined sanitizer)
run: |
mkdir buildundefsani &&
cd buildundefsani &&
cmake .. -DSTREAMVBYTE_SANITIZE_UNDEFINED=ON -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON &&
cmake .. -DSTREAMVBYTE_SANITIZE_UNDEFINED=ON -DCMAKE_BUILD_TYPE=buildundefsani -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON &&
cmake --build . &&
ctest --output-on-failure
4 changes: 2 additions & 2 deletions .github/workflows/vs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: actions/checkout@v2
- name: Configure
run: |
cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} "${{matrix.mode}}" -DSIMDJSON_DEVELOPER_MODE=ON -DSIMDJSON_COMPETITION=OFF -DSIMDJSON_BUILD_STATIC=${{matrix.static}} -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -B build
cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} "${{matrix.mode}}" -DSIMDJSON_DEVELOPER_MODE=ON -DSIMDJSON_COMPETITION=OFF -DSIMDJSON_BUILD_STATIC=${{matrix.static}} -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON -B build
- name: Build Debug
run: cmake --build build --config Debug --verbose
- name: Build Release
Expand All @@ -44,4 +44,4 @@ jobs:
- name: Run Debug tests
run: |
cd build
ctest -C Debug --output-on-failure
ctest -C Debug --output-on-failure
4 changes: 2 additions & 2 deletions .github/workflows/vs16.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: actions/checkout@v2
- name: Configure
run: |
cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} "${{matrix.mode}}" -DSIMDJSON_DEVELOPER_MODE=ON -DSIMDJSON_COMPETITION=OFF -DSIMDJSON_BUILD_STATIC=${{matrix.static}} -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -B build
cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} "${{matrix.mode}}" -DSIMDJSON_DEVELOPER_MODE=ON -DSIMDJSON_COMPETITION=OFF -DSIMDJSON_BUILD_STATIC=${{matrix.static}} -DSTREAMVBYTE_ENABLE_EXAMPLES=ON -DSTREAMVBYTE_ENABLE_TESTS=ON -DSTREAMVBYTE_WALL=ON -DSTREAMVBYTE_WERROR=ON -B build
- name: Build Debug
run: cmake --build build --config Debug --verbose
- name: Build Release
Expand All @@ -44,4 +44,4 @@ jobs:
- name: Run Debug tests
run: |
cd build
ctest -C Debug --output-on-failure
ctest -C Debug --output-on-failure
53 changes: 53 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,58 @@ set(STREAMVBYTE_LIB_SOVERSION "1" CACHE STRING "streamvbyte library soversion")
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

include(CheckCCompilerFlag)

option(STREAMVBYTE_WERROR "Treat warnings as errors." OFF)
option(STREAMVBYTE_WALL "Emit all warnings during compilation." ON)

if(STREAMVBYTE_WERROR)
if(MSVC)
check_c_compiler_flag("/WX" WERROR_MSVC_SUPPORTED)
if(WERROR_MSVC_SUPPORTED)
add_compile_options(/WX)
endif()
# Wall + WX may throw errors from the corecrt headers. Workaround:
if(STREAMVBYTE_WALL)
check_c_compiler_flag("/W3" W3_MSVC_SUPPORTED)
if(W3_MSVC_SUPPORTED)
add_compile_options(/W3)
endif()
check_c_compiler_flag("/w34714" SUPPRESS_C34714_MSVC_SUPPORTED)
if(SUPPRESS_C34714_MSVC_SUPPORTED)
add_compile_options(/w34714)
endif()
check_c_compiler_flag("/wd5045" SUPPRESS_SPECTRE_MSVC_SUPPORTED)
if(SUPPRESS_SPECTRE_MSVC_SUPPORTED)
add_compile_options(/wd5045)
endif()
check_c_compiler_flag("/sdl" SDL_MSVC_SUPPORTED)
if(SDL_MSVC_SUPPORTED)
add_compile_options(/sdl)
endif()
endif()
else()
check_c_compiler_flag(-Werror WERROR_GNU_SUPPORTED)
if(WERROR_GNU_SUPPORTED)
add_compile_options(-Werror)
endif()
endif()
endif()

if(STREAMVBYTE_WALL AND NOT(STREAMVBYTE_WERROR))
if(MSVC)
check_c_compiler_flag("/Wall" WALL_MSVC_SUPPORTED)
if(WALL_MSVC_SUPPORTED)
add_compile_options(/Wall)
endif()
else()
check_c_compiler_flag(-Wall WALL_GNU_SUPPORTED)
if(WALL_GNU_SUPPORTED)
add_compile_options(-Wall)
endif()
endif()
endif()

option(STREAMVBYTE_SANITIZE "Sanitize addresses" OFF)

if(NOT CMAKE_BUILD_TYPE)
Expand Down Expand Up @@ -124,6 +176,7 @@ if(STREAMVBYTE_ENABLE_TESTS)
# unit
add_executable(unit ${PROJECT_SOURCE_DIR}/tests/unit.c)
target_link_libraries(unit streamvbyte)
target_include_directories(unit PRIVATE ${PROJECT_SOURCE_DIR}/src)

enable_testing()

Expand Down
11 changes: 8 additions & 3 deletions examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

#include "streamvbyte.h"

int main() {
int N = 5000;
#ifdef __clang__
#pragma clang diagnostic ignored "-Wdeclaration-after-statement"
#pragma clang diagnostic ignored "-Wunused-variable"
#endif

int main(void) {
uint32_t N = 5000U;
uint32_t * datain = malloc(N * sizeof(uint32_t));
uint8_t * compressedbuffer = malloc(streamvbyte_max_compressedbytes(N));
uint32_t * recovdata = malloc(N * sizeof(uint32_t));
for (int k = 0; k < N; ++k)
for (uint32_t k = 0; k < N; ++k)
datain[k] = 120;
size_t compsize = streamvbyte_encode(datain, N, compressedbuffer); // encoding
// here the result is stored in compressedbuffer using compsize bytes
Expand Down
85 changes: 44 additions & 41 deletions src/streamvbyte_0124_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

#include <string.h> // for memcpy


#ifdef __clang__
#pragma clang diagnostic ignored "-Wcast-align"
#pragma clang diagnostic ignored "-Wdeclaration-after-statement"
#endif

#ifdef STREAMVBYTE_X64
STREAMVBYTE_TARGET_SSE41
static inline __m128i _decode_sse41(uint32_t key,
static inline __m128i svb_decode_sse41(uint32_t key,
const uint8_t *__restrict__ *dataPtrPtr) {
uint8_t len;
__m128i Data = _mm_loadu_si128((__m128i *)*dataPtrPtr);
__m128i Data = _mm_loadu_si128((const __m128i *)*dataPtrPtr);
uint8_t *pshuf = (uint8_t *) &shuffleTable[key];
__m128i Shuf = *(__m128i *)pshuf;
len = lengthTable[key];
Expand All @@ -25,14 +28,14 @@ static inline __m128i _decode_sse41(uint32_t key,
STREAMVBYTE_UNTARGET_REGION

STREAMVBYTE_TARGET_SSE41
static inline void _write_sse41(uint32_t *out, __m128i Vec) {
static inline void svb_write_sse41(uint32_t *out, __m128i Vec) {
_mm_storeu_si128((__m128i *)out, Vec);
}
STREAMVBYTE_UNTARGET_REGION

#endif // STREAMVBYTE_X64

static inline uint32_t _decode_data(const uint8_t **dataPtrPtr, uint8_t code) {
static inline uint32_t svb_decode_data(const uint8_t **dataPtrPtr, uint8_t code) {
const uint8_t *dataPtr = *dataPtrPtr;
uint32_t val;

Expand Down Expand Up @@ -67,7 +70,7 @@ static const uint8_t *svb_decode_scalar(uint32_t *outPtr, const uint8_t *keyPtr,
shift = 0;
key = *keyPtr++;
}
uint32_t val = _decode_data(&dataPtr, (key >> shift) & 0x3);
uint32_t val = svb_decode_data(&dataPtr, (key >> shift) & 0x3);
*outPtr++ = val;
shift += 2;
}
Expand Down Expand Up @@ -95,56 +98,56 @@ static const uint8_t *svb_decode_sse41_simple(uint32_t *out,
uint64_t keys = nextkeys;
memcpy(&nextkeys, keyPtr64 + Offset + 1, sizeof(nextkeys));

Data = _decode_sse41((keys & 0xFF), &dataPtr);
_write_sse41(out, Data);
Data = _decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
_write_sse41(out + 4, Data);
Data = svb_decode_sse41((keys & 0xFF), &dataPtr);
svb_write_sse41(out, Data);
Data = svb_decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
svb_write_sse41(out + 4, Data);

keys >>= 16;
Data = _decode_sse41((keys & 0xFF), &dataPtr);
_write_sse41(out + 8, Data);
Data = _decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
_write_sse41(out + 12, Data);
Data = svb_decode_sse41((keys & 0xFF), &dataPtr);
svb_write_sse41(out + 8, Data);
Data = svb_decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
svb_write_sse41(out + 12, Data);

keys >>= 16;
Data = _decode_sse41((keys & 0xFF), &dataPtr);
_write_sse41(out + 16, Data);
Data = _decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
_write_sse41(out + 20, Data);
Data = svb_decode_sse41((keys & 0xFF), &dataPtr);
svb_write_sse41(out + 16, Data);
Data = svb_decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
svb_write_sse41(out + 20, Data);

keys >>= 16;
Data = _decode_sse41((keys & 0xFF), &dataPtr);
_write_sse41(out + 24, Data);
Data = _decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
_write_sse41(out + 28, Data);
Data = svb_decode_sse41((keys & 0xFF), &dataPtr);
svb_write_sse41(out + 24, Data);
Data = svb_decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
svb_write_sse41(out + 28, Data);

out += 32;
}
{
uint64_t keys = nextkeys;

Data = _decode_sse41((keys & 0xFF), &dataPtr);
_write_sse41(out, Data);
Data = _decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
_write_sse41(out + 4, Data);
Data = svb_decode_sse41((keys & 0xFF), &dataPtr);
svb_write_sse41(out, Data);
Data = svb_decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
svb_write_sse41(out + 4, Data);

keys >>= 16;
Data = _decode_sse41((keys & 0xFF), &dataPtr);
_write_sse41(out + 8, Data);
Data = _decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
_write_sse41(out + 12, Data);
Data = svb_decode_sse41((keys & 0xFF), &dataPtr);
svb_write_sse41(out + 8, Data);
Data = svb_decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
svb_write_sse41(out + 12, Data);

keys >>= 16;
Data = _decode_sse41((keys & 0xFF), &dataPtr);
_write_sse41(out + 16, Data);
Data = _decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
_write_sse41(out + 20, Data);
Data = svb_decode_sse41((keys & 0xFF), &dataPtr);
svb_write_sse41(out + 16, Data);
Data = svb_decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
svb_write_sse41(out + 20, Data);

keys >>= 16;
Data = _decode_sse41((keys & 0xFF), &dataPtr);
_write_sse41(out + 24, Data);
Data = _decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
_write_sse41(out + 28, Data);
Data = svb_decode_sse41((keys & 0xFF), &dataPtr);
svb_write_sse41(out + 24, Data);
Data = svb_decode_sse41((keys & 0xFF00) >> 8, &dataPtr);
svb_write_sse41(out + 28, Data);

out += 32;
}
Expand All @@ -171,12 +174,12 @@ size_t streamvbyte_decode_0124(const uint8_t *in, uint32_t *out, uint32_t count)
#ifdef STREAMVBYTE_X64
if(streamvbyte_sse41()) {
dataPtr = svb_decode_sse41_simple(out, keyPtr, dataPtr, count);
out += count & ~ 31;
keyPtr += (count/4) & ~ 7;
out += count & ~ 31U;
keyPtr += (count/4) & ~ 7U;
count &= 31;
}
#endif

return svb_decode_scalar(out, keyPtr, dataPtr, count) - in;
return (size_t)(svb_decode_scalar(out, keyPtr, dataPtr, count) - in);

}
12 changes: 8 additions & 4 deletions src/streamvbyte_0124_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

#include <string.h> // for memcpy

#ifdef __clang__
#pragma clang diagnostic ignored "-Wcast-align"
#pragma clang diagnostic ignored "-Wdeclaration-after-statement"
#endif

static uint8_t _encode_data(uint32_t val, uint8_t *__restrict__ *dataPtrPtr) {
static uint8_t svb_encode_data(uint32_t val, uint8_t *__restrict__ *dataPtrPtr) {
uint8_t *dataPtr = *dataPtrPtr;
uint8_t code;

Expand Down Expand Up @@ -43,7 +47,7 @@ static uint8_t *svb_encode_scalar(const uint32_t *in,
key = 0;
}
uint32_t val = in[c];
uint8_t code = _encode_data(val, &dataPtr);
uint8_t code = svb_encode_data(val, &dataPtr);
key |= code << shift;
shift += 2;
}
Expand Down Expand Up @@ -82,7 +86,7 @@ STREAMVBYTE_UNTARGET_REGION

STREAMVBYTE_TARGET_SSE41
static size_t streamvbyte_encode_quad(const uint32_t *in, uint8_t *outData, uint8_t *outKey) {
__m128i vin = _mm_loadu_si128((__m128i *) in );
__m128i vin = _mm_loadu_si128((const __m128i *) in );
return streamvbyte_encode4(vin, outData, outKey);
}
STREAMVBYTE_UNTARGET_REGION
Expand All @@ -104,6 +108,6 @@ size_t streamvbyte_encode_0124(const uint32_t *in, uint32_t count, uint8_t *out)
}
}
#endif
return svb_encode_scalar(in, keyPtr, dataPtr, count) - out;
return (size_t)(svb_encode_scalar(in, keyPtr, dataPtr, count) - out);

}
Loading

0 comments on commit c43294a

Please sign in to comment.