1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LIBTEXTCLASSIFIER_COMMON_SIMPLE_ADDER_H_ 18 #define LIBTEXTCLASSIFIER_COMMON_SIMPLE_ADDER_H_ 19 20 #include "util/base/integral_types.h" 21 #include "util/base/port.h" 22 23 namespace libtextclassifier { 24 namespace nlp_core { 25 26 // Implements add and scaleadd in the most straight-forward way, and it doesn't 27 // have any additional requirement on the alignment and array size. 28 class SimpleAdder { 29 public: SimpleAdder(float * dest,int num_floats)30 TC_ATTRIBUTE_ALWAYS_INLINE SimpleAdder(float *dest, int num_floats) 31 : dest_(dest), num_floats_(num_floats) {} 32 LazyAdd(const float * source)33 TC_ATTRIBUTE_ALWAYS_INLINE void LazyAdd(const float *source) const { 34 AddImpl(source, num_floats_, dest_); 35 } 36 LazyScaleAdd(const float * source,const float scale)37 TC_ATTRIBUTE_ALWAYS_INLINE void LazyScaleAdd(const float *source, 38 const float scale) const { 39 ScaleAddImpl(source, num_floats_, scale, dest_); 40 } 41 42 // Simple fast while loop to implement dest += source. AddImpl(const float * __restrict source,uint32 size,float * __restrict dest)43 TC_ATTRIBUTE_ALWAYS_INLINE static void AddImpl(const float *__restrict source, 44 uint32 size, 45 float *__restrict dest) { 46 for (uint32 i = 0; i < size; ++i) { 47 dest[i] += source[i]; 48 } 49 } 50 51 // Simple fast while loop to implement dest += scale * source. ScaleAddImpl(const float * __restrict source,uint32 size,const float scale,float * __restrict dest)52 TC_ATTRIBUTE_ALWAYS_INLINE static void ScaleAddImpl( 53 const float *__restrict source, uint32 size, const float scale, 54 float *__restrict dest) { 55 for (uint32 i = 0; i < size; ++i) { 56 dest[i] += source[i] * scale; 57 } 58 } 59 60 private: 61 float *dest_; 62 int num_floats_; 63 }; 64 65 } // namespace nlp_core 66 } // namespace libtextclassifier 67 68 #endif // LIBTEXTCLASSIFIER_COMMON_SIMPLE_ADDER_H_ 69