Index: branches/SHM-dev/source/Lib/TLibCommon/Debug.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibCommon/Debug.cpp	(revision 1397)
+++ branches/SHM-dev/source/Lib/TLibCommon/Debug.cpp	(revision 1398)
@@ -180,4 +180,5 @@
 
   PRINT_CONSTANT(ME_ENABLE_ROUNDING_OF_MVS,                                         settingNameWidth, settingValueWidth);
+  PRINT_CONSTANT(U0040_MODIFIED_WEIGHTEDPREDICTION_WITH_BIPRED_AND_CLIPPING,        settingNameWidth, settingValueWidth);
 
   //------------------------------------------------
Index: branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.h	(revision 1397)
+++ branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.h	(revision 1398)
@@ -79,4 +79,6 @@
 
   Bool                  bApplyWeight;     // whether weighted prediction is used or not
+  Bool                  bIsBiPred;
+
   const WPScalingParam *wpCur;           // weighted prediction scaling parameters for current ref
   ComponentID           compIdx;
@@ -98,4 +100,5 @@
      bitDepth(0),
      bApplyWeight(false),
+     bIsBiPred(false),
      wpCur(NULL),
      compIdx(MAX_NUM_COMPONENT),
Index: branches/SHM-dev/source/Lib/TLibCommon/TComRdCostWeightPrediction.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibCommon/TComRdCostWeightPrediction.cpp	(revision 1397)
+++ branches/SHM-dev/source/Lib/TLibCommon/TComRdCostWeightPrediction.cpp	(revision 1398)
@@ -74,4 +74,5 @@
   Distortion uiSum = 0;
 
+#if !U0040_MODIFIED_WEIGHTEDPREDICTION_WITH_BIPRED_AND_CLIPPING
   for(Int iRows = pcDtParam->iRows; iRows != 0; iRows-- )
   {
@@ -91,4 +92,135 @@
 
   pcDtParam->compIdx = MAX_NUM_COMPONENT;  // reset for DEBUG (assert test)
+#else
+  // Default weight
+  if (w0 == 1 << shift)
+  {
+    // no offset
+    if (offset == 0)
+    {
+      for(Int iRows = pcDtParam->iRows; iRows != 0; iRows-- )
+      {
+        for (Int n = 0; n < iCols; n++ )
+        {
+          uiSum += abs( piOrg[n] - piCur[n] );
+        }
+        if (pcDtParam->m_maximumDistortionForEarlyExit <  ( uiSum >> distortionShift))
+        {
+          return uiSum >> distortionShift;
+        }
+        piOrg += iStrideOrg;
+        piCur += iStrideCur;
+      }
+    }
+    else
+    {
+      // Lets not clip for the bipredictive case since clipping should be done after
+      // combining both elements. Unfortunately the code uses the suboptimal "subtraction"
+      // method, which is faster but introduces the clipping issue (therefore Bipred is suboptimal).
+      if (pcDtParam->bIsBiPred)
+      {
+        for(Int iRows = pcDtParam->iRows; iRows != 0; iRows-- )
+        {
+          for (Int n = 0; n < iCols; n++ )
+          {
+            uiSum += abs( piOrg[n] - (piCur[n] + offset) );
+          }
+          if (pcDtParam->m_maximumDistortionForEarlyExit <  ( uiSum >> distortionShift))
+          {
+            return uiSum >> distortionShift;
+          }
+
+          piOrg += iStrideOrg;
+          piCur += iStrideCur;
+        }
+      }
+      else
+      {
+        const Pel iMaxValue = (Pel) ((1 << pcDtParam->bitDepth) - 1);
+        for(Int iRows = pcDtParam->iRows; iRows != 0; iRows-- )
+        {
+          for (Int n = 0; n < iCols; n++ )
+          {
+            const Pel pred = Clip3((Pel) 0, iMaxValue, (Pel) (piCur[n] + offset)) ;
+
+            uiSum += abs( piOrg[n] - pred );
+          }
+          if (pcDtParam->m_maximumDistortionForEarlyExit <  ( uiSum >> distortionShift))
+          {
+            return uiSum >> distortionShift;
+          }
+          piOrg += iStrideOrg;
+          piCur += iStrideCur;
+        }
+      }
+    }
+  }
+  else
+  {
+    // Lets not clip for the bipredictive case since clipping should be done after
+    // combining both elements. Unfortunately the code uses the suboptimal "subtraction"
+    // method, which is faster but introduces the clipping issue (therefore Bipred is suboptimal).
+    if (pcDtParam->bIsBiPred)
+    {
+      for(Int iRows = pcDtParam->iRows; iRows != 0; iRows-- )
+      {
+        for (Int n = 0; n < iCols; n++ )
+        {
+          const Pel pred = ( (w0*piCur[n] + round) >> shift ) + offset ;
+          uiSum += abs( piOrg[n] - pred );
+        }
+        if (pcDtParam->m_maximumDistortionForEarlyExit <  ( uiSum >> distortionShift))
+        {
+          return uiSum >> distortionShift;
+        }
+
+        piOrg += iStrideOrg;
+        piCur += iStrideCur;
+      }
+    }
+    else
+    {
+      const Pel iMaxValue = (Pel) ((1 << pcDtParam->bitDepth) - 1);
+
+      if (offset == 0)
+      {
+        for(Int iRows = pcDtParam->iRows; iRows != 0; iRows-- )
+        {
+          for (Int n = 0; n < iCols; n++ )
+          {
+            const Pel pred = Clip3((Pel) 0, iMaxValue, (Pel) (( (w0*piCur[n] + round) >> shift ))) ;
+
+            uiSum += abs( piOrg[n] - pred );
+          }
+          if (pcDtParam->m_maximumDistortionForEarlyExit <  ( uiSum >> distortionShift))
+          {
+            return uiSum >> distortionShift;
+          }
+          piOrg += iStrideOrg;
+          piCur += iStrideCur;
+        }
+      }
+      else
+      {
+        for(Int iRows = pcDtParam->iRows; iRows != 0; iRows-- )
+        {
+          for (Int n = 0; n < iCols; n++ )
+          {
+            const Pel pred = Clip3((Pel) 0, iMaxValue, (Pel) (( (w0*piCur[n] + round) >> shift ) + offset)) ;
+
+            uiSum += abs( piOrg[n] - pred );
+          }
+          if (pcDtParam->m_maximumDistortionForEarlyExit <  ( uiSum >> distortionShift))
+          {
+            return uiSum >> distortionShift;
+          }
+          piOrg += iStrideOrg;
+          piCur += iStrideCur;
+        }
+      }
+    }
+  }
+  //pcDtParam->compIdx = MAX_NUM_COMPONENT;  // reset for DEBUG (assert test)
+#endif
 
   return uiSum >> distortionShift;
@@ -124,4 +256,5 @@
   Distortion sum = 0;
 
+#if !U0040_MODIFIED_WEIGHTEDPREDICTION_WITH_BIPRED_AND_CLIPPING
   for(Int iRows = pcDtParam->iRows ; iRows != 0; iRows-- )
   {
@@ -137,4 +270,38 @@
 
   pcDtParam->compIdx = MAX_NUM_COMPONENT; // reset for DEBUG (assert test)
+#else
+  if (pcDtParam->bIsBiPred)
+  {
+    for(Int iRows = pcDtParam->iRows ; iRows != 0; iRows-- )
+    {
+      for (Int n = 0; n < iCols; n++ )
+      {
+        const Pel pred     = ( (w0*piCur[n] + round) >> shift ) + offset ;
+        const Pel residual = piOrg[n] - pred;
+        sum += ( Distortion(residual) * Distortion(residual) ) >> distortionShift;
+      }
+      piOrg += iStrideOrg;
+      piCur += iStrideCur;
+    }
+  }
+  else
+  {
+    const Pel iMaxValue = (Pel) ((1 << pcDtParam->bitDepth) - 1);
+
+    for(Int iRows = pcDtParam->iRows ; iRows != 0; iRows-- )
+    {
+      for (Int n = 0; n < iCols; n++ )
+      {
+        const Pel pred     = Clip3((Pel) 0, iMaxValue, (Pel) (( (w0*piCur[n] + round) >> shift ) + offset)) ;
+        const Pel residual = piOrg[n] - pred;
+        sum += ( Distortion(residual) * Distortion(residual) ) >> distortionShift;
+      }
+      piOrg += iStrideOrg;
+      piCur += iStrideCur;
+    }
+  }
+
+  //pcDtParam->compIdx = MAX_NUM_COMPONENT; // reset for DEBUG (assert test)
+#endif
 
   return sum;
Index: branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h	(revision 1397)
+++ branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h	(revision 1398)
@@ -165,4 +165,5 @@
 #define DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES  1 ///< TODO: integrate this macro into a broader conformance checking system.
 #define T0196_SELECTIVE_RDOQ                              1 ///< selective RDOQ
+#define U0040_MODIFIED_WEIGHTEDPREDICTION_WITH_BIPRED_AND_CLIPPING 1
 
 // ====================================================================================================================
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncSearch.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncSearch.cpp	(revision 1397)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncSearch.cpp	(revision 1398)
@@ -3903,4 +3903,5 @@
     fWeight = 0.5;
   }
+  m_cDistParam.bIsBiPred = bBi;
 
   //  Search key pattern initialization
