[v6,09/24] ipa: libipa: exposure_mode_helper: Remove `lastStageGain`
diff mbox series

Message ID 20261102101610.877390-10-barnabas.pocze@ideasonboard.com
State Superseded
Headers show
Series
  • ipa: libipa: agc: Take exposure margin into account
Related show

Commit Message

Barnabás Pőcze Nov. 2, 2026, 10:15 a.m. UTC
There is no need for a separate variable, `stageGain` already contains
the previous stage's gain at the beginning of the loop. Moving the
assignment to a later point removes the need for a separate variable.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 src/ipa/libipa/exposure_mode_helper.cpp | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

Comments

Stefan Klug Nov. 5, 2026, 8:57 a.m. UTC | #1
Hi Barnabás,

Thank you for the patch.

Quoting Barnabás Pőcze (2026-11-02 11:15:55)
> There is no need for a separate variable, `stageGain` already contains
> the previous stage's gain at the beginning of the loop. Moving the
> assignment to a later point removes the need for a separate variable.

Technically this is all correct. Still I think we should drop this
change. It is an optimization that will not bring any performance
difference but makes the code more difficult to parse.

If you work on such an algorithm, you don't want to keep track from what
point on a given variable has the right value. From a logical point of
view you iterate over the stages and apply some logic on every
stage, thereby using the gain of the last stage or the gain of the
current stage. Mixing these two gains in one variable makes it more
difficult to parse and has a tendency to pop up as unexpected values in
debug log statements or so.

Best regards,
Stefan

> 
> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> ---
>  src/ipa/libipa/exposure_mode_helper.cpp | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
> index 4ec31f54c3..4076d6f385 100644
> --- a/src/ipa/libipa/exposure_mode_helper.cpp
> +++ b/src/ipa/libipa/exposure_mode_helper.cpp
> @@ -241,12 +241,10 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>         }
>  
>         double stageGain = clampGain(1.0);
> -       double lastStageGain = stageGain;
>  
>         for (const auto &stage : stages_) {
>                 utils::Duration stageExposureTime = clampExposureTime(stage.exposureTime,
>                                                                       &quantGain);
> -               stageGain = clampGain(stage.gain);
>  
>                 /*
>                  * We perform the clamping on both exposure time and gain in
> @@ -256,9 +254,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>                  * the stage limits are initialised.
>                  */
>  
> -               /* Clamp the gain to lastStageGain and regulate exposureTime. */
> -               if (stageExposureTime * lastStageGain >= exposure) {
> -                       exposureTime = clampExposureTime(exposure / lastStageGain, &quantGain);
> +               /* Clamp the gain to that of the last stage and regulate exposure time. */
> +               if (stageExposureTime * stageGain >= exposure) {
> +                       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
>                         gain = clampGain(exposure / exposureTime, &quantGain2);
>                         quantGain *= quantGain2;
>  
> @@ -266,7 +264,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>                                  exposure / (exposureTime * gain * quantGain) };
>                 }
>  
> -               /* Clamp the exposureTime to stageExposureTime and regulate gain. */
> +               /* Clamp the exposure time to stageExposureTime and regulate gain. */
> +               stageGain = clampGain(stage.gain);
> +
>                 if (stageExposureTime * stageGain >= exposure) {
>                         exposureTime = stageExposureTime;
>                         gain = clampGain(exposure / exposureTime, &quantGain2);
> @@ -275,8 +275,6 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>                         return { exposureTime, gain, quantGain,
>                                  exposure / (exposureTime * gain * quantGain) };
>                 }
> -
> -               lastStageGain = stageGain;
>         }
>  
>         /*
> -- 
> 2.56.0
>
Barnabás Pőcze Nov. 5, 2026, 1:57 p.m. UTC | #2
2026. 11. 05. 9:57 keltezéssel, Stefan Klug írta:
> Hi Barnabás,
> 
> Thank you for the patch.
> 
> Quoting Barnabás Pőcze (2026-11-02 11:15:55)
>> There is no need for a separate variable, `stageGain` already contains
>> the previous stage's gain at the beginning of the loop. Moving the
>> assignment to a later point removes the need for a separate variable.
> 
> Technically this is all correct. Still I think we should drop this
> change. It is an optimization that will not bring any performance
> difference but makes the code more difficult to parse.

That's a fair point; I really liked the kind of "symmetry" after the next change.


> 
> If you work on such an algorithm, you don't want to keep track from what
> point on a given variable has the right value. From a logical point of
> view you iterate over the stages and apply some logic on every
> stage, thereby using the gain of the last stage or the gain of the
> current stage. Mixing these two gains in one variable makes it more
> difficult to parse and has a tendency to pop up as unexpected values in
> debug log statements or so.
> 
> Best regards,
> Stefan
> 
>>
>> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
>> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
>> ---
>>   src/ipa/libipa/exposure_mode_helper.cpp | 14 ++++++--------
>>   1 file changed, 6 insertions(+), 8 deletions(-)
>>
>> diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
>> index 4ec31f54c3..4076d6f385 100644
>> --- a/src/ipa/libipa/exposure_mode_helper.cpp
>> +++ b/src/ipa/libipa/exposure_mode_helper.cpp
>> @@ -241,12 +241,10 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>          }
>>   
>>          double stageGain = clampGain(1.0);
>> -       double lastStageGain = stageGain;
>>   
>>          for (const auto &stage : stages_) {
>>                  utils::Duration stageExposureTime = clampExposureTime(stage.exposureTime,
>>                                                                        &quantGain);
>> -               stageGain = clampGain(stage.gain);
>>   
>>                  /*
>>                   * We perform the clamping on both exposure time and gain in
>> @@ -256,9 +254,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>                   * the stage limits are initialised.
>>                   */
>>   
>> -               /* Clamp the gain to lastStageGain and regulate exposureTime. */
>> -               if (stageExposureTime * lastStageGain >= exposure) {
>> -                       exposureTime = clampExposureTime(exposure / lastStageGain, &quantGain);
>> +               /* Clamp the gain to that of the last stage and regulate exposure time. */
>> +               if (stageExposureTime * stageGain >= exposure) {
>> +                       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
>>                          gain = clampGain(exposure / exposureTime, &quantGain2);
>>                          quantGain *= quantGain2;
>>   
>> @@ -266,7 +264,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>                                   exposure / (exposureTime * gain * quantGain) };
>>                  }
>>   
>> -               /* Clamp the exposureTime to stageExposureTime and regulate gain. */
>> +               /* Clamp the exposure time to stageExposureTime and regulate gain. */
>> +               stageGain = clampGain(stage.gain);
>> +
>>                  if (stageExposureTime * stageGain >= exposure) {
>>                          exposureTime = stageExposureTime;
>>                          gain = clampGain(exposure / exposureTime, &quantGain2);
>> @@ -275,8 +275,6 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>                          return { exposureTime, gain, quantGain,
>>                                   exposure / (exposureTime * gain * quantGain) };
>>                  }
>> -
>> -               lastStageGain = stageGain;
>>          }
>>   
>>          /*
>> -- 
>> 2.56.0
>>
Barnabás Pőcze Nov. 6, 2026, 7:46 a.m. UTC | #3
2026. 11. 05. 14:57 keltezéssel, Barnabás Pőcze írta:
> 2026. 11. 05. 9:57 keltezéssel, Stefan Klug írta:
>> Hi Barnabás,
>>
>> Thank you for the patch.
>>
>> Quoting Barnabás Pőcze (2026-11-02 11:15:55)
>>> There is no need for a separate variable, `stageGain` already contains
>>> the previous stage's gain at the beginning of the loop. Moving the
>>> assignment to a later point removes the need for a separate variable.
>>
>> Technically this is all correct. Still I think we should drop this
>> change. It is an optimization that will not bring any performance
>> difference but makes the code more difficult to parse.
> 
> That's a fair point; I really liked the kind of "symmetry" after the next change.

I'm not sure you will like my first alternative.

diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
index b3e75f6358..d9ecf44a8d 100644
--- a/src/ipa/libipa/exposure_mode_helper.cpp
+++ b/src/ipa/libipa/exposure_mode_helper.cpp
@@ -233,13 +233,11 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
                          exposure / (exposureTime * gain * quantGain) };
         }
  
-       double stageGain = clampGain(1.0);
-       double lastStageGain = stageGain;
+       utils::Duration stageExposureTime = {};
+       double lastStageGain = clampGain(1.0);
  
         for (unsigned int stage = 0; stage < gains_.size(); stage++) {
-               utils::Duration stageExposureTime = clampExposureTime(exposureTimes_[stage],
-                                                                     &quantGain);
-               stageGain = clampGain(gains_[stage]);
+               stageExposureTime = clampExposureTime(exposureTimes_[stage], &quantGain);
  
                 /*
                  * We perform the clamping on both exposure time and gain in
@@ -260,6 +258,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
                 }
  
                 /* Clamp the exposureTime to stageExposureTime and regulate gain. */
+               double stageGain = clampGain(gains_[stage], &quantGain2);
+
                 if (stageExposureTime * stageGain >= exposure) {
                         exposureTime = stageExposureTime;
                         gain = clampGain(exposure / exposureTime, &quantGain2);
@@ -279,8 +279,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
          * stages to use then the default stageGain of 1.0 is used so that
          * exposure time is maxed before gain is touched at all.
          */
-       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
-       gain = clampGain(exposure / exposureTime, &quantGain2);
+       exposureTime = stageExposureTime;
+       gain = lastStageGain;
         quantGain *= quantGain2;
  
         return { exposureTime, gain, quantGain,


Note that the above is on the master branch, but it shouldn't be significantly different.
I would say that now the "provenance" of `quantGain2` is even more obscure.

A third alternative would be to simply re-do the clamping at the end. This is what I had
in a previous version. But it is wholly unnecessary since by the time the loop ends, all
parameters will have been clamped. But maybe it's worth it for simplicity's sake.


> 
> 
>>
>> If you work on such an algorithm, you don't want to keep track from what
>> point on a given variable has the right value. From a logical point of
>> view you iterate over the stages and apply some logic on every
>> stage, thereby using the gain of the last stage or the gain of the
>> current stage. Mixing these two gains in one variable makes it more
>> difficult to parse and has a tendency to pop up as unexpected values in
>> debug log statements or so.
>>
>> Best regards,
>> Stefan
>>
>>>
>>> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
>>> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
>>> ---
>>>   src/ipa/libipa/exposure_mode_helper.cpp | 14 ++++++--------
>>>   1 file changed, 6 insertions(+), 8 deletions(-)
>>>
>>> diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
>>> index 4ec31f54c3..4076d6f385 100644
>>> --- a/src/ipa/libipa/exposure_mode_helper.cpp
>>> +++ b/src/ipa/libipa/exposure_mode_helper.cpp
>>> @@ -241,12 +241,10 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>          }
>>>          double stageGain = clampGain(1.0);
>>> -       double lastStageGain = stageGain;
>>>          for (const auto &stage : stages_) {
>>>                  utils::Duration stageExposureTime = clampExposureTime(stage.exposureTime,
>>>                                                                        &quantGain);
>>> -               stageGain = clampGain(stage.gain);
>>>                  /*
>>>                   * We perform the clamping on both exposure time and gain in
>>> @@ -256,9 +254,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>                   * the stage limits are initialised.
>>>                   */
>>> -               /* Clamp the gain to lastStageGain and regulate exposureTime. */
>>> -               if (stageExposureTime * lastStageGain >= exposure) {
>>> -                       exposureTime = clampExposureTime(exposure / lastStageGain, &quantGain);
>>> +               /* Clamp the gain to that of the last stage and regulate exposure time. */
>>> +               if (stageExposureTime * stageGain >= exposure) {
>>> +                       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
>>>                          gain = clampGain(exposure / exposureTime, &quantGain2);
>>>                          quantGain *= quantGain2;
>>> @@ -266,7 +264,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>                                   exposure / (exposureTime * gain * quantGain) };
>>>                  }
>>> -               /* Clamp the exposureTime to stageExposureTime and regulate gain. */
>>> +               /* Clamp the exposure time to stageExposureTime and regulate gain. */
>>> +               stageGain = clampGain(stage.gain);
>>> +
>>>                  if (stageExposureTime * stageGain >= exposure) {
>>>                          exposureTime = stageExposureTime;
>>>                          gain = clampGain(exposure / exposureTime, &quantGain2);
>>> @@ -275,8 +275,6 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>                          return { exposureTime, gain, quantGain,
>>>                                   exposure / (exposureTime * gain * quantGain) };
>>>                  }
>>> -
>>> -               lastStageGain = stageGain;
>>>          }
>>>          /*
>>> -- 
>>> 2.56.0
>>>
>
Barnabás Pőcze Nov. 6, 2026, 2:53 p.m. UTC | #4
2026. 11. 06. 8:46 keltezéssel, Barnabás Pőcze írta:
> 2026. 11. 05. 14:57 keltezéssel, Barnabás Pőcze írta:
>> 2026. 11. 05. 9:57 keltezéssel, Stefan Klug írta:
>>> Hi Barnabás,
>>>
>>> Thank you for the patch.
>>>
>>> Quoting Barnabás Pőcze (2026-11-02 11:15:55)
>>>> There is no need for a separate variable, `stageGain` already contains
>>>> the previous stage's gain at the beginning of the loop. Moving the
>>>> assignment to a later point removes the need for a separate variable.
>>>
>>> Technically this is all correct. Still I think we should drop this
>>> change. It is an optimization that will not bring any performance
>>> difference but makes the code more difficult to parse.
>>
>> That's a fair point; I really liked the kind of "symmetry" after the next change.
> 
> I'm not sure you will like my first alternative.
> 
> diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
> index b3e75f6358..d9ecf44a8d 100644
> --- a/src/ipa/libipa/exposure_mode_helper.cpp
> +++ b/src/ipa/libipa/exposure_mode_helper.cpp
> @@ -233,13 +233,11 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>                           exposure / (exposureTime * gain * quantGain) };
>          }
> 
> -       double stageGain = clampGain(1.0);
> -       double lastStageGain = stageGain;
> +       utils::Duration stageExposureTime = {};
> +       double lastStageGain = clampGain(1.0);
> 
>          for (unsigned int stage = 0; stage < gains_.size(); stage++) {
> -               utils::Duration stageExposureTime = clampExposureTime(exposureTimes_[stage],
> -                                                                     &quantGain);
> -               stageGain = clampGain(gains_[stage]);
> +               stageExposureTime = clampExposureTime(exposureTimes_[stage], &quantGain);
> 
>                  /*
>                   * We perform the clamping on both exposure time and gain in
> @@ -260,6 +258,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>                  }
> 
>                  /* Clamp the exposureTime to stageExposureTime and regulate gain. */
> +               double stageGain = clampGain(gains_[stage], &quantGain2);
> +
>                  if (stageExposureTime * stageGain >= exposure) {
>                          exposureTime = stageExposureTime;
>                          gain = clampGain(exposure / exposureTime, &quantGain2);
> @@ -279,8 +279,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>           * stages to use then the default stageGain of 1.0 is used so that
>           * exposure time is maxed before gain is touched at all.
>           */
> -       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
> -       gain = clampGain(exposure / exposureTime, &quantGain2);
> +       exposureTime = stageExposureTime;
> +       gain = lastStageGain;
>          quantGain *= quantGain2;
> 
>          return { exposureTime, gain, quantGain,
> 
> 
> Note that the above is on the master branch, but it shouldn't be significantly different.
> I would say that now the "provenance" of `quantGain2` is even more obscure.
> 
> A third alternative would be to simply re-do the clamping at the end. This is what I had
> in a previous version. But it is wholly unnecessary since by the time the loop ends, all
> parameters will have been clamped. But maybe it's worth it for simplicity's sake.
> 

Or maybe just keeping `stageGain` out of the loop makes things clearer.


> 
>>
>>
>>>
>>> If you work on such an algorithm, you don't want to keep track from what
>>> point on a given variable has the right value. From a logical point of
>>> view you iterate over the stages and apply some logic on every
>>> stage, thereby using the gain of the last stage or the gain of the
>>> current stage. Mixing these two gains in one variable makes it more
>>> difficult to parse and has a tendency to pop up as unexpected values in
>>> debug log statements or so.
>>>
>>> Best regards,
>>> Stefan
>>>
>>>>
>>>> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
>>>> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
>>>> ---
>>>>   src/ipa/libipa/exposure_mode_helper.cpp | 14 ++++++--------
>>>>   1 file changed, 6 insertions(+), 8 deletions(-)
>>>>
>>>> diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
>>>> index 4ec31f54c3..4076d6f385 100644
>>>> --- a/src/ipa/libipa/exposure_mode_helper.cpp
>>>> +++ b/src/ipa/libipa/exposure_mode_helper.cpp
>>>> @@ -241,12 +241,10 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>>          }
>>>>          double stageGain = clampGain(1.0);
>>>> -       double lastStageGain = stageGain;
>>>>          for (const auto &stage : stages_) {
>>>>                  utils::Duration stageExposureTime = clampExposureTime(stage.exposureTime,
>>>>                                                                        &quantGain);
>>>> -               stageGain = clampGain(stage.gain);
>>>>                  /*
>>>>                   * We perform the clamping on both exposure time and gain in
>>>> @@ -256,9 +254,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>>                   * the stage limits are initialised.
>>>>                   */
>>>> -               /* Clamp the gain to lastStageGain and regulate exposureTime. */
>>>> -               if (stageExposureTime * lastStageGain >= exposure) {
>>>> -                       exposureTime = clampExposureTime(exposure / lastStageGain, &quantGain);
>>>> +               /* Clamp the gain to that of the last stage and regulate exposure time. */
>>>> +               if (stageExposureTime * stageGain >= exposure) {
>>>> +                       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
>>>>                          gain = clampGain(exposure / exposureTime, &quantGain2);
>>>>                          quantGain *= quantGain2;
>>>> @@ -266,7 +264,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>>                                   exposure / (exposureTime * gain * quantGain) };
>>>>                  }
>>>> -               /* Clamp the exposureTime to stageExposureTime and regulate gain. */
>>>> +               /* Clamp the exposure time to stageExposureTime and regulate gain. */
>>>> +               stageGain = clampGain(stage.gain);
>>>> +
>>>>                  if (stageExposureTime * stageGain >= exposure) {
>>>>                          exposureTime = stageExposureTime;
>>>>                          gain = clampGain(exposure / exposureTime, &quantGain2);
>>>> @@ -275,8 +275,6 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>>                          return { exposureTime, gain, quantGain,
>>>>                                   exposure / (exposureTime * gain * quantGain) };
>>>>                  }
>>>> -
>>>> -               lastStageGain = stageGain;
>>>>          }
>>>>          /*
>>>> -- 
>>>> 2.56.0
>>>>
>>
>
Stefan Klug Nov. 6, 2026, 3:31 p.m. UTC | #5
Hi Barnabás,

Quoting Barnabás Pőcze (2026-11-06 15:53:03)
> 2026. 11. 06. 8:46 keltezéssel, Barnabás Pőcze írta:
> > 2026. 11. 05. 14:57 keltezéssel, Barnabás Pőcze írta:
> >> 2026. 11. 05. 9:57 keltezéssel, Stefan Klug írta:
> >>> Hi Barnabás,
> >>>
> >>> Thank you for the patch.
> >>>
> >>> Quoting Barnabás Pőcze (2026-11-02 11:15:55)
> >>>> There is no need for a separate variable, `stageGain` already contains
> >>>> the previous stage's gain at the beginning of the loop. Moving the
> >>>> assignment to a later point removes the need for a separate variable.
> >>>
> >>> Technically this is all correct. Still I think we should drop this
> >>> change. It is an optimization that will not bring any performance
> >>> difference but makes the code more difficult to parse.
> >>
> >> That's a fair point; I really liked the kind of "symmetry" after the next change.
> > 
> > I'm not sure you will like my first alternative.
> > 
> > diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
> > index b3e75f6358..d9ecf44a8d 100644
> > --- a/src/ipa/libipa/exposure_mode_helper.cpp
> > +++ b/src/ipa/libipa/exposure_mode_helper.cpp
> > @@ -233,13 +233,11 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
> >                           exposure / (exposureTime * gain * quantGain) };
> >          }
> > 
> > -       double stageGain = clampGain(1.0);
> > -       double lastStageGain = stageGain;
> > +       utils::Duration stageExposureTime = {};
> > +       double lastStageGain = clampGain(1.0);
> > 
> >          for (unsigned int stage = 0; stage < gains_.size(); stage++) {
> > -               utils::Duration stageExposureTime = clampExposureTime(exposureTimes_[stage],
> > -                                                                     &quantGain);
> > -               stageGain = clampGain(gains_[stage]);
> > +               stageExposureTime = clampExposureTime(exposureTimes_[stage], &quantGain);
> > 
> >                  /*
> >                   * We perform the clamping on both exposure time and gain in
> > @@ -260,6 +258,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
> >                  }
> > 
> >                  /* Clamp the exposureTime to stageExposureTime and regulate gain. */
> > +               double stageGain = clampGain(gains_[stage], &quantGain2);
> > +
> >                  if (stageExposureTime * stageGain >= exposure) {
> >                          exposureTime = stageExposureTime;
> >                          gain = clampGain(exposure / exposureTime, &quantGain2);
> > @@ -279,8 +279,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
> >           * stages to use then the default stageGain of 1.0 is used so that
> >           * exposure time is maxed before gain is touched at all.
> >           */
> > -       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
> > -       gain = clampGain(exposure / exposureTime, &quantGain2);
> > +       exposureTime = stageExposureTime;
> > +       gain = lastStageGain;
> >          quantGain *= quantGain2;
> > 
> >          return { exposureTime, gain, quantGain,
> > 
> > 
> > Note that the above is on the master branch, but it shouldn't be significantly different.
> > I would say that now the "provenance" of `quantGain2` is even more obscure.
> > 
> > A third alternative would be to simply re-do the clamping at the end. This is what I had
> > in a previous version. But it is wholly unnecessary since by the time the loop ends, all
> > parameters will have been clamped. But maybe it's worth it for simplicity's sake.
> > 

It took me astonishingly long to wrap my head around the various
options. Yes, I would prefer simplicity over optimizing the double clamp
away.

> 
> Or maybe just keeping `stageGain` out of the loop makes things clearer.
> 

Yes, I think that would be better. It was unclear to me why you
access lastStageGain after the loop, just to realize it was already
updated to stageGain.

If I'm not mistaken that would look something like this:

	double lastStageGain = clampGain(1.0);
	utils::Duration stageExposureTime;
	double stageGain;

	for (const auto &stage : stages_) {
		/*
		 * We perform the clamping on both exposure time and gain in
		 * case the helper has had limits set that prevent those values
		 * being lowered beyond a certain minimum...this can happen at
		 * runtime for various reasons and so would not be known when
		 * the stage limits are initialised.
		 */
		stageExposureTime = clampExposureTime(stage.exposureTime, &quantGain);
		stageGain = clampGain(stage.gain, &quantGain2);

		/* Clamp the gain to that of the last stage and regulate exposure time. */
		if (stageExposureTime * lastStageGain >= exposure) {
			exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
			gain = clampGain(exposure / exposureTime, &quantGain2);
			quantGain *= quantGain2;

			return { exposureTime, gain, quantGain,
				 exposure / (exposureTime * gain * quantGain) };
		}

		/* Clamp the exposure time to stageExposureTime and regulate gain. */
		if (stageExposureTime * stageGain >= exposure) {
			exposureTime = stageExposureTime;
			gain = clampGain(exposure / exposureTime, &quantGain2);
			quantGain *= quantGain2;

			return { exposureTime, gain, quantGain,
				 exposure / (exposureTime * gain * quantGain) };
		}
	}

	/*
	 * If no stages could satisfy the desired exposure, then use the last
	 * stage's exposure time and gain, which is the maximum allowed.
	 */
	exposureTime = stageExposureTime;
	gain = stageGain;
	quantGain *= quantGain2;




At least to me this is easier to parse.

Best regards,
Stefan


> 
> > 
> >>
> >>
> >>>
> >>> If you work on such an algorithm, you don't want to keep track from what
> >>> point on a given variable has the right value. From a logical point of
> >>> view you iterate over the stages and apply some logic on every
> >>> stage, thereby using the gain of the last stage or the gain of the
> >>> current stage. Mixing these two gains in one variable makes it more
> >>> difficult to parse and has a tendency to pop up as unexpected values in
> >>> debug log statements or so.
> >>>
> >>> Best regards,
> >>> Stefan
> >>>
> >>>>
> >>>> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
> >>>> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> >>>> ---
> >>>>   src/ipa/libipa/exposure_mode_helper.cpp | 14 ++++++--------
> >>>>   1 file changed, 6 insertions(+), 8 deletions(-)
> >>>>
> >>>> diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
> >>>> index 4ec31f54c3..4076d6f385 100644
> >>>> --- a/src/ipa/libipa/exposure_mode_helper.cpp
> >>>> +++ b/src/ipa/libipa/exposure_mode_helper.cpp
> >>>> @@ -241,12 +241,10 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
> >>>>          }
> >>>>          double stageGain = clampGain(1.0);
> >>>> -       double lastStageGain = stageGain;
> >>>>          for (const auto &stage : stages_) {
> >>>>                  utils::Duration stageExposureTime = clampExposureTime(stage.exposureTime,
> >>>>                                                                        &quantGain);
> >>>> -               stageGain = clampGain(stage.gain);
> >>>>                  /*
> >>>>                   * We perform the clamping on both exposure time and gain in
> >>>> @@ -256,9 +254,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
> >>>>                   * the stage limits are initialised.
> >>>>                   */
> >>>> -               /* Clamp the gain to lastStageGain and regulate exposureTime. */
> >>>> -               if (stageExposureTime * lastStageGain >= exposure) {
> >>>> -                       exposureTime = clampExposureTime(exposure / lastStageGain, &quantGain);
> >>>> +               /* Clamp the gain to that of the last stage and regulate exposure time. */
> >>>> +               if (stageExposureTime * stageGain >= exposure) {
> >>>> +                       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
> >>>>                          gain = clampGain(exposure / exposureTime, &quantGain2);
> >>>>                          quantGain *= quantGain2;
> >>>> @@ -266,7 +264,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
> >>>>                                   exposure / (exposureTime * gain * quantGain) };
> >>>>                  }
> >>>> -               /* Clamp the exposureTime to stageExposureTime and regulate gain. */
> >>>> +               /* Clamp the exposure time to stageExposureTime and regulate gain. */
> >>>> +               stageGain = clampGain(stage.gain);
> >>>> +
> >>>>                  if (stageExposureTime * stageGain >= exposure) {
> >>>>                          exposureTime = stageExposureTime;
> >>>>                          gain = clampGain(exposure / exposureTime, &quantGain2);
> >>>> @@ -275,8 +275,6 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
> >>>>                          return { exposureTime, gain, quantGain,
> >>>>                                   exposure / (exposureTime * gain * quantGain) };
> >>>>                  }
> >>>> -
> >>>> -               lastStageGain = stageGain;
> >>>>          }
> >>>>          /*
> >>>> -- 
> >>>> 2.56.0
> >>>>
> >>
> > 
>
Barnabás Pőcze Nov. 6, 2026, 3:54 p.m. UTC | #6
2026. 11. 06. 16:31 keltezéssel, Stefan Klug írta:
> Hi Barnabás,
> 
> Quoting Barnabás Pőcze (2026-11-06 15:53:03)
>> 2026. 11. 06. 8:46 keltezéssel, Barnabás Pőcze írta:
>>> 2026. 11. 05. 14:57 keltezéssel, Barnabás Pőcze írta:
>>>> 2026. 11. 05. 9:57 keltezéssel, Stefan Klug írta:
>>>>> Hi Barnabás,
>>>>>
>>>>> Thank you for the patch.
>>>>>
>>>>> Quoting Barnabás Pőcze (2026-11-02 11:15:55)
>>>>>> There is no need for a separate variable, `stageGain` already contains
>>>>>> the previous stage's gain at the beginning of the loop. Moving the
>>>>>> assignment to a later point removes the need for a separate variable.
>>>>>
>>>>> Technically this is all correct. Still I think we should drop this
>>>>> change. It is an optimization that will not bring any performance
>>>>> difference but makes the code more difficult to parse.
>>>>
>>>> That's a fair point; I really liked the kind of "symmetry" after the next change.
>>>
>>> I'm not sure you will like my first alternative.
>>>
>>> diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
>>> index b3e75f6358..d9ecf44a8d 100644
>>> --- a/src/ipa/libipa/exposure_mode_helper.cpp
>>> +++ b/src/ipa/libipa/exposure_mode_helper.cpp
>>> @@ -233,13 +233,11 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>                            exposure / (exposureTime * gain * quantGain) };
>>>           }
>>>
>>> -       double stageGain = clampGain(1.0);
>>> -       double lastStageGain = stageGain;
>>> +       utils::Duration stageExposureTime = {};
>>> +       double lastStageGain = clampGain(1.0);
>>>
>>>           for (unsigned int stage = 0; stage < gains_.size(); stage++) {
>>> -               utils::Duration stageExposureTime = clampExposureTime(exposureTimes_[stage],
>>> -                                                                     &quantGain);
>>> -               stageGain = clampGain(gains_[stage]);
>>> +               stageExposureTime = clampExposureTime(exposureTimes_[stage], &quantGain);
>>>
>>>                   /*
>>>                    * We perform the clamping on both exposure time and gain in
>>> @@ -260,6 +258,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>                   }
>>>
>>>                   /* Clamp the exposureTime to stageExposureTime and regulate gain. */
>>> +               double stageGain = clampGain(gains_[stage], &quantGain2);
>>> +
>>>                   if (stageExposureTime * stageGain >= exposure) {
>>>                           exposureTime = stageExposureTime;
>>>                           gain = clampGain(exposure / exposureTime, &quantGain2);
>>> @@ -279,8 +279,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>            * stages to use then the default stageGain of 1.0 is used so that
>>>            * exposure time is maxed before gain is touched at all.
>>>            */
>>> -       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
>>> -       gain = clampGain(exposure / exposureTime, &quantGain2);
>>> +       exposureTime = stageExposureTime;
>>> +       gain = lastStageGain;
>>>           quantGain *= quantGain2;
>>>
>>>           return { exposureTime, gain, quantGain,
>>>
>>>
>>> Note that the above is on the master branch, but it shouldn't be significantly different.
>>> I would say that now the "provenance" of `quantGain2` is even more obscure.
>>>
>>> A third alternative would be to simply re-do the clamping at the end. This is what I had
>>> in a previous version. But it is wholly unnecessary since by the time the loop ends, all
>>> parameters will have been clamped. But maybe it's worth it for simplicity's sake.
>>>
> 
> It took me astonishingly long to wrap my head around the various
> options. Yes, I would prefer simplicity over optimizing the double clamp
> away.
> 
>>
>> Or maybe just keeping `stageGain` out of the loop makes things clearer.
>>
> 
> Yes, I think that would be better. It was unclear to me why you
> access lastStageGain after the loop, just to realize it was already
> updated to stageGain.
> 
> If I'm not mistaken that would look something like this:
> 
> 	double lastStageGain = clampGain(1.0);
> 	utils::Duration stageExposureTime;
> 	double stageGain;
> 
> 	for (const auto &stage : stages_) {
> 		/*
> 		 * We perform the clamping on both exposure time and gain in
> 		 * case the helper has had limits set that prevent those values
> 		 * being lowered beyond a certain minimum...this can happen at
> 		 * runtime for various reasons and so would not be known when
> 		 * the stage limits are initialised.
> 		 */
> 		stageExposureTime = clampExposureTime(stage.exposureTime, &quantGain);
> 		stageGain = clampGain(stage.gain, &quantGain2);
> 
> 		/* Clamp the gain to that of the last stage and regulate exposure time. */
> 		if (stageExposureTime * lastStageGain >= exposure) {
> 			exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
> 			gain = clampGain(exposure / exposureTime, &quantGain2);
> 			quantGain *= quantGain2;
> 
> 			return { exposureTime, gain, quantGain,
> 				 exposure / (exposureTime * gain * quantGain) };
> 		}
> 
> 		/* Clamp the exposure time to stageExposureTime and regulate gain. */
> 		if (stageExposureTime * stageGain >= exposure) {
> 			exposureTime = stageExposureTime;
> 			gain = clampGain(exposure / exposureTime, &quantGain2);
> 			quantGain *= quantGain2;
> 
> 			return { exposureTime, gain, quantGain,
> 				 exposure / (exposureTime * gain * quantGain) };
> 		}

This is missing `lastStageGain = stageGain`, but this was indeed what I had in mind.
As far as I understand this seems acceptable, so I'll go with this.



> 	}
> 
> 	/*
> 	 * If no stages could satisfy the desired exposure, then use the last
> 	 * stage's exposure time and gain, which is the maximum allowed.
> 	 */
> 	exposureTime = stageExposureTime;
> 	gain = stageGain;
> 	quantGain *= quantGain2;
> 
> 
> 
> 
> At least to me this is easier to parse.
> 
> Best regards,
> Stefan
> 
> 
>>
>>>
>>>>
>>>>
>>>>>
>>>>> If you work on such an algorithm, you don't want to keep track from what
>>>>> point on a given variable has the right value. From a logical point of
>>>>> view you iterate over the stages and apply some logic on every
>>>>> stage, thereby using the gain of the last stage or the gain of the
>>>>> current stage. Mixing these two gains in one variable makes it more
>>>>> difficult to parse and has a tendency to pop up as unexpected values in
>>>>> debug log statements or so.
>>>>>
>>>>> Best regards,
>>>>> Stefan
>>>>>
>>>>>>
>>>>>> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
>>>>>> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
>>>>>> ---
>>>>>>    src/ipa/libipa/exposure_mode_helper.cpp | 14 ++++++--------
>>>>>>    1 file changed, 6 insertions(+), 8 deletions(-)
>>>>>>
>>>>>> diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
>>>>>> index 4ec31f54c3..4076d6f385 100644
>>>>>> --- a/src/ipa/libipa/exposure_mode_helper.cpp
>>>>>> +++ b/src/ipa/libipa/exposure_mode_helper.cpp
>>>>>> @@ -241,12 +241,10 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>>>>           }
>>>>>>           double stageGain = clampGain(1.0);
>>>>>> -       double lastStageGain = stageGain;
>>>>>>           for (const auto &stage : stages_) {
>>>>>>                   utils::Duration stageExposureTime = clampExposureTime(stage.exposureTime,
>>>>>>                                                                         &quantGain);
>>>>>> -               stageGain = clampGain(stage.gain);
>>>>>>                   /*
>>>>>>                    * We perform the clamping on both exposure time and gain in
>>>>>> @@ -256,9 +254,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>>>>                    * the stage limits are initialised.
>>>>>>                    */
>>>>>> -               /* Clamp the gain to lastStageGain and regulate exposureTime. */
>>>>>> -               if (stageExposureTime * lastStageGain >= exposure) {
>>>>>> -                       exposureTime = clampExposureTime(exposure / lastStageGain, &quantGain);
>>>>>> +               /* Clamp the gain to that of the last stage and regulate exposure time. */
>>>>>> +               if (stageExposureTime * stageGain >= exposure) {
>>>>>> +                       exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
>>>>>>                           gain = clampGain(exposure / exposureTime, &quantGain2);
>>>>>>                           quantGain *= quantGain2;
>>>>>> @@ -266,7 +264,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>>>>                                    exposure / (exposureTime * gain * quantGain) };
>>>>>>                   }
>>>>>> -               /* Clamp the exposureTime to stageExposureTime and regulate gain. */
>>>>>> +               /* Clamp the exposure time to stageExposureTime and regulate gain. */
>>>>>> +               stageGain = clampGain(stage.gain);
>>>>>> +
>>>>>>                   if (stageExposureTime * stageGain >= exposure) {
>>>>>>                           exposureTime = stageExposureTime;
>>>>>>                           gain = clampGain(exposure / exposureTime, &quantGain2);
>>>>>> @@ -275,8 +275,6 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
>>>>>>                           return { exposureTime, gain, quantGain,
>>>>>>                                    exposure / (exposureTime * gain * quantGain) };
>>>>>>                   }
>>>>>> -
>>>>>> -               lastStageGain = stageGain;
>>>>>>           }
>>>>>>           /*
>>>>>> -- 
>>>>>> 2.56.0
>>>>>>
>>>>
>>>
>>

Patch
diff mbox series

diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
index 4ec31f54c3..4076d6f385 100644
--- a/src/ipa/libipa/exposure_mode_helper.cpp
+++ b/src/ipa/libipa/exposure_mode_helper.cpp
@@ -241,12 +241,10 @@  ExposureModeHelper::splitExposure(utils::Duration exposure) const
 	}
 
 	double stageGain = clampGain(1.0);
-	double lastStageGain = stageGain;
 
 	for (const auto &stage : stages_) {
 		utils::Duration stageExposureTime = clampExposureTime(stage.exposureTime,
 								      &quantGain);
-		stageGain = clampGain(stage.gain);
 
 		/*
 		 * We perform the clamping on both exposure time and gain in
@@ -256,9 +254,9 @@  ExposureModeHelper::splitExposure(utils::Duration exposure) const
 		 * the stage limits are initialised.
 		 */
 
-		/* Clamp the gain to lastStageGain and regulate exposureTime. */
-		if (stageExposureTime * lastStageGain >= exposure) {
-			exposureTime = clampExposureTime(exposure / lastStageGain, &quantGain);
+		/* Clamp the gain to that of the last stage and regulate exposure time. */
+		if (stageExposureTime * stageGain >= exposure) {
+			exposureTime = clampExposureTime(exposure / stageGain, &quantGain);
 			gain = clampGain(exposure / exposureTime, &quantGain2);
 			quantGain *= quantGain2;
 
@@ -266,7 +264,9 @@  ExposureModeHelper::splitExposure(utils::Duration exposure) const
 				 exposure / (exposureTime * gain * quantGain) };
 		}
 
-		/* Clamp the exposureTime to stageExposureTime and regulate gain. */
+		/* Clamp the exposure time to stageExposureTime and regulate gain. */
+		stageGain = clampGain(stage.gain);
+
 		if (stageExposureTime * stageGain >= exposure) {
 			exposureTime = stageExposureTime;
 			gain = clampGain(exposure / exposureTime, &quantGain2);
@@ -275,8 +275,6 @@  ExposureModeHelper::splitExposure(utils::Duration exposure) const
 			return { exposureTime, gain, quantGain,
 				 exposure / (exposureTime * gain * quantGain) };
 		}
-
-		lastStageGain = stageGain;
 	}
 
 	/*