Add an optimized FIFO writer for 32-bit words

Also improve the FIFO comments

Test: not yet tested
Change-Id: I29b645c05800491661ec81e816a38544b64fcc90
diff --git a/audio_utils/Android.bp b/audio_utils/Android.bp
index 31d25df..c6999e5 100644
--- a/audio_utils/Android.bp
+++ b/audio_utils/Android.bp
@@ -22,6 +22,7 @@
         "ErrorLog.cpp",
         "fifo.cpp",
         "fifo_index.cpp",
+        "fifo_writer32.cpp",
         "format.c",
         "limiter.c",
         "minifloat.c",
diff --git a/audio_utils/fifo_index.cpp b/audio_utils/fifo_index.cpp
index d9a200a..ebb085d 100644
--- a/audio_utils/fifo_index.cpp
+++ b/audio_utils/fifo_index.cpp
@@ -27,6 +27,7 @@
     return atomic_load_explicit(&mIndex, std::memory_order_acquire);
 }
 
+// FIXME should inline this, so that writer32 can also inline it
 void audio_utils_fifo_index::storeRelease(uint32_t value)
 {
     atomic_store_explicit(&mIndex, value, std::memory_order_release);
diff --git a/audio_utils/fifo_writer32.cpp b/audio_utils/fifo_writer32.cpp
new file mode 100644
index 0000000..7551e67
--- /dev/null
+++ b/audio_utils/fifo_writer32.cpp
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <atomic>
+#include <stdlib.h>
+#include <string.h>
+
+// TODO templatize int32_t
+
+#include <audio_utils/fifo_writer32.h>
+
+static inline void memcpyWords(int32_t *dst, const int32_t *src, uint32_t count)
+{
+    switch (count) {
+    case 0: break;
+// TODO templatize here also, but first confirm no performance regression compared to current
+#define _(n) \
+    case n: { \
+        struct s##n { int32_t a[n]; }; \
+        *(struct s##n *)dst = *(const struct s##n *)src; \
+        break; \
+    }
+    _(1) _(2) _(3) _(4) _(5) _(6) _(7) _(8) _(9) _(10) _(11) _(12) _(13) _(14) _(15) _(16)
+#undef _
+    default:
+        memcpy(dst, src, count * sizeof(int32_t));
+        break;
+    }
+}
+
+audio_utils_fifo_writer32::audio_utils_fifo_writer32(audio_utils_fifo& fifo) :
+    mLocalRear(0), mFrameCountP2(fifo.mFrameCountP2), mBuffer((int32_t *) fifo.mBuffer),
+    mWriterRear(fifo.mWriterRear)
+{
+    if (fifo.mFrameSize != sizeof(int32_t) || fifo.mFudgeFactor != 0 ||
+            ((size_t) mBuffer & ((sizeof(int32_t) - 1))) != 0) {
+        abort();
+    }
+}
+
+audio_utils_fifo_writer32::~audio_utils_fifo_writer32()
+{
+}
+
+void audio_utils_fifo_writer32::write(const int32_t *buffer, uint32_t count)
+        __attribute__((no_sanitize("integer")))     // mLocalRear += can wrap
+{
+    uint32_t availToWrite = mFrameCountP2;
+    if (availToWrite > count) {
+        availToWrite = count;
+    }
+    uint32_t rearOffset = mLocalRear & (mFrameCountP2 - 1);
+    uint32_t part1 = mFrameCountP2 - rearOffset;
+    if (part1 >  availToWrite) {
+        part1 = availToWrite;
+    }
+    memcpyWords(&mBuffer[rearOffset], buffer, part1);
+    // TODO apply this simplification to other copies of the code
+    uint32_t part2 = availToWrite - part1;
+    memcpyWords(&mBuffer[0], &buffer[part1], part2);
+    mLocalRear += availToWrite;
+}
diff --git a/audio_utils/include/audio_utils/fifo.h b/audio_utils/include/audio_utils/fifo.h
index d60a826..dab7051 100644
--- a/audio_utils/include/audio_utils/fifo.h
+++ b/audio_utils/include/audio_utils/fifo.h
@@ -139,6 +139,7 @@
 
     friend class audio_utils_fifo_reader;
     friend class audio_utils_fifo_writer;
+    friend class audio_utils_fifo_writer32;
 
 public:
 
@@ -252,6 +253,9 @@
      *
      * \return Actual number of frames available, if greater than or equal to zero.
      *         Guaranteed to be <= \p count and == iovec[0].mLength + iovec[1].mLength.
+     *         For a reader this is also guaranteed to be <= capacity.
+     *         For a writer this is also guaranteed to be <= effective buffer size,
+     *         even if there is no reader that throttles writer.
      *
      *  \retval -EIO        corrupted indices, no recovery is possible
      *  \retval -EOVERFLOW  reader doesn't throttle writer, and frames were lost because reader
@@ -360,6 +364,8 @@
      *
      * \return Actual number of frames written, if greater than or equal to zero.
      *         Guaranteed to be <= \p count.
+     *         Also guaranteed to be <= effective buffer size,
+     *         even if there is no reader that throttles writer.
      *         The actual transfer count may be zero if the FIFO is full,
      *         or partial if the FIFO was almost full.
      *  \retval -EIO       corrupted indices, no recovery is possible
@@ -480,6 +486,7 @@
      *
      * \return Actual number of frames read, if greater than or equal to zero.
      *         Guaranteed to be <= \p count.
+     *         Also guaranteed to be <= capacity.
      *         The actual transfer count may be zero if the FIFO is empty,
      *         or partial if the FIFO was almost empty.
      *  \retval -EIO        corrupted indices, no recovery is possible
diff --git a/audio_utils/include/audio_utils/fifo_writer32.h b/audio_utils/include/audio_utils/fifo_writer32.h
new file mode 100644
index 0000000..d17d58d
--- /dev/null
+++ b/audio_utils/include/audio_utils/fifo_writer32.h
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_FIFO_WRITER32_H
+#define ANDROID_AUDIO_FIFO_WRITER32_H
+
+#include <audio_utils/fifo.h>
+
+/**
+ * Optimized FIFO writer for 32-bit words.
+ *
+ * Has these restrictions compared to the ordinary FIFO writer:
+ *  - buffer must be aligned on a 32-bit boundary
+ *  - frame size must be sizeof(int32_t)
+ *  - capacity must be power-of-2
+ *  - effective size must be equal to capacity
+ *  - no support for throttling of writer by one reader, and thus no blocking writes
+ *  - does not implement the provider interface
+ *  - does not implement the ordinary writer interface
+ *  - does not unblock a reader
+ *  - return value from write methods is void
+ *  - no implied store-release; must be done explicitly
+ *  - may not be combined with ordinary writer
+ *
+ * Usage:
+ *  - construct an ordinary FIFO that follows the restrictions above
+ *  - construct an ordinary reader based on that FIFO
+ *  - construct a writer32 using the FIFO
+ *  - use a sequence of write and write1, followed by storeRelease to commit
+ */
+class audio_utils_fifo_writer32 /* : public audio_utils_fifo_provider */ {
+
+public:
+    /**
+     * Construct a writer32 from a FIFO.
+     */
+    explicit audio_utils_fifo_writer32(audio_utils_fifo& fifo);
+    /*virtual*/ ~audio_utils_fifo_writer32();
+
+    /**
+     * Write an array of int32_t to FIFO.
+     * If count is larger than capacity, then only the initial 'capacity' frames will be written.
+     * TODO Instead of a silent truncation, consider adding a size_t or ssize_t return value
+     * to indicate the actual transfer count.
+     */
+    void write(const int32_t *buffer, uint32_t count /* FIXME size_t in writer */);
+
+    /**
+     * Write one int32_t value to FIFO.
+     */
+    void write1(const int32_t value)
+            __attribute__((no_sanitize("integer")))     // mLocalRear ++ can wrap
+    {
+        mBuffer[mLocalRear++ & (mFrameCountP2 - 1)] = value;
+    }
+
+    /**
+     * Commit all previous write and write1 so that they are observable by reader(s).
+     */
+    void storeRelease() {
+        mWriterRear.storeRelease(mLocalRear);
+    }
+
+private:
+    // Accessed by writer only using ordinary operations
+    uint32_t    mLocalRear; // frame index of next frame slot available to write, or write index
+
+    // These fields are copied from fifo for better performance (avoids an extra de-reference)
+    const uint32_t                     mFrameCountP2;
+    int32_t                    * const mBuffer;
+    audio_utils_fifo_index&            mWriterRear;
+};
+
+#endif // ANDROID_AUDIO_FIFO_WRITER32_H