• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

memcpy_.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development
00002 // Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_DATA_MEMCPY__HH
00028 # define MLN_DATA_MEMCPY__HH
00029 
00035 
00036 # include <cstring>
00037 # include <mln/core/concept/image.hh>
00038 # include <mln/core/pixel.hh>
00039 # include <mln/metal/is_not_const.hh>
00040 # include <mln/opt/element.hh>
00041 
00042 
00043 
00044 namespace mln
00045 {
00046 
00047   namespace data
00048   {
00049 
00065     template <typename Pd, typename Ps>
00066     void memcpy_(Generalized_Pixel<Pd>& dest, const Generalized_Pixel<Ps>& src,
00067                  std::size_t n);
00068 
00069 
00070 # ifndef MLN_INCLUDE_ONLY
00071 
00072     namespace impl
00073     {
00074 
00075       template <typename Pd, typename Ps>
00076       inline
00077       void memcpy__(Pd& dest, const Ps& src, std::size_t n)
00078       {
00079         // trace::entering("data::impl::memcpy__");
00080 
00081         typedef mln_image(Pd) Id;
00082         typedef mln_image(Ps) Is;
00083         if (n == 0)
00084           return; // no-op
00085 
00086         if (n == 1)
00087         {
00088           dest.val() = src.val(); // one assignment
00089           return;
00090         }
00091 
00092         /* FIXME: Careful, the code generated for this function by g++
00093            4.2 with high a optimization level (`-O3') and without
00094            `-fno-strict-aliasing' might be wrong (at least with
00095            Debian's g++ 4.2 on IA-32)!  Note that Debian's g++ 4.0,
00096            4.1, 4.3 and 4.4 are fine.  */
00097 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
00098 # if __GNUC__ == 4 && __GNUC_MINOR__ == 2
00099 #  warning The code generated by g++ 4.2 on Debian GNU/Linux 5.0 for IA-32 for this function might be wrong.
00100 # endif
00101 #endif
00102         if (sizeof(mln_value(Id)) == 1)
00103         {
00104           std::memcpy((void*) (&dest.val()), // violent casts
00105                       (const void*) (&src.val()),
00106                       n);
00107         }
00108         else
00109         {
00110           mln_value(Id)* p_d = &dest.val();
00111           const mln_value(Is)* p_s = &src.val();
00112           for (std::size_t i = 0; i < n; ++i)
00113             *p_d++ = *p_s++;
00114         }
00115 
00116         // trace::exiting("data::impl::memcpy__");
00117       }
00118 
00119     }
00120 
00121     template <typename Pd, typename Ps>
00122     inline
00123     void memcpy_(Generalized_Pixel<Pd>& dest_,
00124                  const Generalized_Pixel<Ps>& src_,
00125                  std::size_t n)
00126     {
00127       // trace::entering("data::memcpy_");
00128 
00129       typedef mln_image(Pd) Id;
00130       metal::is_not_const<Id>::check();
00131       typedef mln_image(Ps) Is;
00132       Pd& dest = mln::internal::force_exact<Pd>(dest_);
00133       Ps& src  = mln::internal::force_exact<Ps>(src_);
00134 
00135       mln_precondition(sizeof(mln_value(Id)) == sizeof(mln_value(Is)));
00136       mln_precondition(dest.ima().is_valid());
00137       mln_precondition(src.ima().is_valid());
00138 
00139       mln_precondition(&dest.val() >= &opt::element(dest.ima(), 0));
00140       mln_precondition(&dest.val() < &opt::element(dest.ima(), 0) +
00141                        opt::nelements(dest.ima()));
00142 
00143       mln_precondition(&dest.val() + n <= &opt::element(dest.ima(), 0) +
00144                        opt::nelements(dest.ima()));
00145 
00146       mln_precondition(&src.val() >= &opt::element(src.ima(), 0));
00147       mln_precondition(&src.val() < &opt::element(src.ima(), 0) +
00148                        opt::nelements(src.ima()));
00149       mln_precondition(&src.val() + n <= &opt::element(src.ima(), 0) +
00150                        opt::nelements(src.ima()));
00151 
00152       impl::memcpy__(dest, src, n);
00153 
00154       // trace::exiting("data::memcpy_");
00155     }
00156 
00157 # endif // ! MLN_INCLUDE_ONLY
00158 
00159   } // end of namespace mln::data
00160 
00161 } // end of namespace mln
00162 
00163 
00164 #endif // ! MLN_DATA_MEMCPY__HH

Generated on Fri Oct 19 2012 04:16:00 for Milena (Olena) by  doxygen 1.7.1