[libcxx] r187587 - Taking another swing at correctly optimizing fill_n.

Howard Hinnant hhinnant at apple.com
Thu Aug 1 10:29:29 PDT 2013


Author: hhinnant
Date: Thu Aug  1 12:29:28 2013
New Revision: 187587

URL: http://llvm.org/viewvc/llvm-project?rev=187587&view=rev
Log:
Taking another swing at correctly optimizing fill_n.

Modified:
    libcxx/trunk/include/algorithm
    libcxx/trunk/test/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp

Modified: libcxx/trunk/include/algorithm
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=187587&r1=187586&r2=187587&view=diff
==============================================================================
--- libcxx/trunk/include/algorithm (original)
+++ libcxx/trunk/include/algorithm Thu Aug  1 12:29:28 2013
@@ -1987,17 +1987,23 @@ replace_copy_if(_InputIterator __first,
 template <class _OutputIterator, class _Size, class _Tp>
 inline _LIBCPP_INLINE_VISIBILITY
 _OutputIterator
-__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, false_type)
+__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
 {
     for (; __n > 0; ++__first, --__n)
         *__first = __value_;
     return __first;
 }
 
-template <class _OutputIterator, class _Size, class _Tp>
+template <class _Tp, class _Size, class _Up>
 inline _LIBCPP_INLINE_VISIBILITY
-_OutputIterator
-__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, true_type)
+typename enable_if
+<
+    is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
+    !is_same<_Tp, bool>::value &&
+    is_integral<_Up>::value && sizeof(_Up) == 1,
+    _Tp*
+>::type
+__fill_n(_Tp* __first, _Size __n,_Up __value_)
 {
     if (__n > 0)
         _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
@@ -2009,11 +2015,7 @@ inline _LIBCPP_INLINE_VISIBILITY
 _OutputIterator
 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
 {
-   return _VSTD::__fill_n(__first, __n, __value_, integral_constant<bool,
-                                              is_pointer<_OutputIterator>::value &&
-                                              is_trivially_assignable<typename remove_pointer<_OutputIterator>::type, _Tp>::value &&
-                                              is_convertible<_Tp, unsigned char>::value &&
-                                              sizeof(typename remove_pointer<_OutputIterator>::type) == 1>());
+   return _VSTD::__fill_n(__first, __n, __value_);
 }
 
 // fill

Modified: libcxx/trunk/test/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp?rev=187587&r1=187586&r2=187587&view=diff
==============================================================================
--- libcxx/trunk/test/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp (original)
+++ libcxx/trunk/test/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp Thu Aug  1 12:29:28 2013
@@ -94,6 +94,44 @@ test_struct_array()
     assert(test1a[3].c == 11);
 }
 
+class A
+{
+    char a_;
+public:
+    A() {}
+    explicit A(char a) : a_(a) {}
+    operator unsigned char() const {return 'b';}
+
+    friend bool operator==(const A& x, const A& y)
+        {return x.a_ == y.a_;}
+};
+
+void
+test5()
+{
+    A a[3];
+    assert(std::fill_n(&a[0], 3, A('a')) == a+3);
+    assert(a[0] == A('a'));
+    assert(a[1] == A('a'));
+    assert(a[2] == A('a'));
+}
+
+struct Storage
+{
+  union
+  {
+    unsigned char a;
+    unsigned char b;
+  };
+};
+ 
+void test6()
+{
+  Storage foo[5];
+  std::fill_n(&foo[0], 5, Storage());
+}
+
+
 int main()
 {
     test_char<forward_iterator<char*> >();
@@ -109,4 +147,7 @@ int main()
     test_int_array();
     test_int_array_struct_source();
     test_struct_array();
+
+    test5();
+    test6();
 }





More information about the cfe-commits mailing list