From 40a6b2d753fafee6800f4f3e9a620962d28e8a00 Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Tue, 5 Jul 2016 07:32:57 -0700 Subject: [PATCH] Native code cleanups Changed toolchain to "clang", set "-Ofast" so that the code is compiled with proper optimization, and fixed some code errors uncovered by clang's more aggressive optimization. --- app/build.gradle | 2 ++ app/src/main/jni/neon_fir.s | 4 ++-- app/src/main/jni/ringbuffer.cc | 12 ++++++------ app/src/main/jni/ringbuffer.h | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a00706a..78ffe07 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,6 +20,8 @@ model { } ndk { moduleName "synth" + toolchain "clang" + cppFlags.add('-Ofast') ldLibs.addAll(['log', 'OpenSLES']) } abis { diff --git a/app/src/main/jni/neon_fir.s b/app/src/main/jni/neon_fir.s index c0bcb20..61b99e2 100644 --- a/app/src/main/jni/neon_fir.s +++ b/app/src/main/jni/neon_fir.s @@ -334,7 +334,7 @@ neon_halfrate_split1: pop {r4} bx lr - .size neon_halfrate_split, .-.neon_halfrate_split + .size neon_halfrate_split, .-neon_halfrate_split .align 2 .global neon_halfrate_combine @@ -364,4 +364,4 @@ neon_halfrate_combine1: pop {r4} bx lr - .size neon_halfrate_combine, .-.neon_halfrate_combine + .size neon_halfrate_combine, .-neon_halfrate_combine diff --git a/app/src/main/jni/ringbuffer.cc b/app/src/main/jni/ringbuffer.cc index a59361e..de4e395 100644 --- a/app/src/main/jni/ringbuffer.cc +++ b/app/src/main/jni/ringbuffer.cc @@ -36,7 +36,7 @@ int RingBuffer::WriteBytesAvailable() { int RingBuffer::Read(int size, uint8_t *bytes) { int rd_ix = rd_ix_; SynthMemoryBarrier(); // read barrier, make sure data is committed before ix - int fragment_size = min(size, kBufSize - rd_ix); + unsigned int fragment_size = min((unsigned int)size, kBufSize - rd_ix); memcpy(bytes, buf_ + rd_ix, fragment_size); if (size > fragment_size) { memcpy(bytes + fragment_size, buf_, size - fragment_size); @@ -46,20 +46,20 @@ int RingBuffer::Read(int size, uint8_t *bytes) { return size; } -int RingBuffer::Write(const uint8_t *bytes, int size) { - int remaining = size; +void RingBuffer::Write(const uint8_t *bytes, int size) { + unsigned int remaining = (unsigned int)size; while (remaining > 0) { int rd_ix = rd_ix_; int wr_ix = wr_ix_; - int space_available = (rd_ix - wr_ix - 1) & (kBufSize - 1); + unsigned int space_available = (rd_ix - wr_ix - 1) & (kBufSize - 1); if (space_available == 0) { struct timespec sleepTime; sleepTime.tv_sec = 0; sleepTime.tv_nsec = 1000000; nanosleep(&sleepTime, NULL); } else { - int wr_size = min(remaining, space_available); - int fragment_size = min(wr_size, kBufSize - wr_ix); + unsigned int wr_size = min(remaining, space_available); + unsigned int fragment_size = min(wr_size, kBufSize - wr_ix); memcpy(buf_ + wr_ix, bytes, fragment_size); if (wr_size > fragment_size) { memcpy(buf_, bytes + fragment_size, wr_size - fragment_size); diff --git a/app/src/main/jni/ringbuffer.h b/app/src/main/jni/ringbuffer.h index c8c3e72..f52c8b4 100644 --- a/app/src/main/jni/ringbuffer.h +++ b/app/src/main/jni/ringbuffer.h @@ -33,9 +33,9 @@ class RingBuffer { // Writes bytes into the buffer. If the buffer is full, the method will // block until space is available. - int Write(const uint8_t *bytes, int size); + void Write(const uint8_t *bytes, int size); private: - static const int kBufSize = 8192; + static const unsigned int kBufSize = 65536; uint8_t buf_[kBufSize]; volatile unsigned int rd_ix_; volatile unsigned int wr_ix_;