Skip to contentSkip to conversation

staging: s5k3e2fx.c: reduce complexity by factoring

PATCH In review Justin Madru 4 participants 8 messages Dec 6, 2009 – Dec 15, 2009 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: s5k3e2fx.c: reduce complexity by factoring

I was style fixing some code when I ran into this code.
It seems like the code could be reduced, by factoring the expression. But this results in a very simple expression.

Am I assuming something wrong? Or is this a bug in the original code? This doesn't look right because the assignment of s_move[i] has no mention of the loop counter.

Justin

------

the code was looping, seting s_move[i] to the following calculations

if (actual_step >= 0)

        s_move[i] = ((((i + 1) * gain + 0x200) - (i * gain + 0x200)) / 0x400);
else
        s_move[i] = ((((i + 1) * gain - 0x200) - (i * gain - 0x200)) / 0x400);

but, by factoring the expression, it can be shown that they both reduce to a very simple expression:

        s_move[i] = gain / 0x400;
---
 drivers/staging/dream/camera/s5k3e2fx.c |   10 +++-------
 1 files changed, 3 insertions(+), 7 deletions(-)
drivers/staging/dream/camera/s5k3e2fx.c +3 -7
index f0e49be..d205a2d 100644
@@ -1092,14 +1092,10 @@ static int32_t s5k3e2fx_move_focus(int direction, int32_t num_steps)
 
 	actual_step = step_direction * (int16_t)num_steps;
 	pos_offset = init_code + s5k3e2fx_ctrl->curr_lens_pos;
-	gain = actual_step * 0x400 / 5;
+	gain = actual_step / 5;
 
-	for (i = 0; i <= 4; i++) {
-		if (actual_step >= 0)
-			s_move[i] = ((((i+1)*gain+0x200) - (i*gain+0x200))/0x400);
-		else
-			s_move[i] = ((((i+1)*gain-0x200) - (i*gain-0x200))/0x400);
-	}
+	for (i = 0; i <= 4; i++)
+		s_move[i] = gain;
 
 	/* Ring Damping Code */
 	for (i = 0; i <= 4; i++) {
Signature · 1 line
1.6.5.4
Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Justin Madru

I was style fixing some code when I ran into this code.
It seems like the code could be reduced, by factoring the expression. But this results in a very simple expression.

Am I assuming something wrong? Or is this a bug in the original code? This doesn't look right because the assignment of s_move[i] has no mention of the loop counter.

Justin

------

the code was looping, seting s_move[i] to the following calculations

if (actual_step >= 0)

Quoted 9 lines
        s_move[i] = ((((i + 1) * gain + 0x200) - (i * gain + 0x200)) / 0x400);
else
        s_move[i] = ((((i + 1) * gain - 0x200) - (i * gain - 0x200)) / 0x400);

but, by factoring the expression, it can be shown that they both reduce to a very simple expression:

        s_move[i] = gain / 0x400;

Heh.

Looks like an improvement to me.

If gain were 16 bits instead of 32 I would think the "* 0x400 / 0x400" was designed to mask out the highest 10 bits.

regards,
dan carpenter

Quoted 35 lines
---
 drivers/staging/dream/camera/s5k3e2fx.c |   10 +++-------
 1 files changed, 3 insertions(+), 7 deletions(-)
drivers/staging/dream/camera/s5k3e2fx.c +3 -7
index f0e49be..d205a2d 100644
@@ -1092,14 +1092,10 @@ static int32_t s5k3e2fx_move_focus(int direction, int32_t num_steps)
 
 	actual_step = step_direction * (int16_t)num_steps;
 	pos_offset = init_code + s5k3e2fx_ctrl->curr_lens_pos;
-	gain = actual_step * 0x400 / 5;
+	gain = actual_step / 5;
 
-	for (i = 0; i <= 4; i++) {
-		if (actual_step >= 0)
-			s_move[i] = ((((i+1)*gain+0x200) - (i*gain+0x200))/0x400);
-		else
-			s_move[i] = ((((i+1)*gain-0x200) - (i*gain-0x200))/0x400);
-	}
+	for (i = 0; i <= 4; i++)
+		s_move[i] = gain;
 
 	/* Ring Damping Code */
 	for (i = 0; i <= 4; i++) {
Signature · 7 lines
1.6.5.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#2 lore raw
Quoted 70 lines · Dan Carpenter
Quoted 15 lines · Justin Madru

I was style fixing some code when I ran into this code.
It seems like the code could be reduced, by factoring the expression. But this results in a very simple expression.

Am I assuming something wrong? Or is this a bug in the original code? This doesn't look right because the assignment of s_move[i] has no mention of the loop counter.

Justin

------

the code was looping, seting s_move[i] to the following calculations

if (actual_step>= 0)

Quoted 10 lines
         s_move[i] = ((((i + 1) * gain + 0x200) - (i * gain + 0x200)) / 0x400);
else
         s_move[i] = ((((i + 1) * gain - 0x200) - (i * gain - 0x200)) / 0x400);

but, by factoring the expression, it can be shown that they both reduce to a very simple expression:

         s_move[i] = gain / 0x400;

Heh.

Looks like an improvement to me.

If gain were 16 bits instead of 32 I would think the "* 0x400 / 0x400" was designed to mask out the highest 10 bits.

regards,
dan carpenter

Quoted 30 lines
---
  drivers/staging/dream/camera/s5k3e2fx.c |   10 +++-------
  1 files changed, 3 insertions(+), 7 deletions(-)
drivers/staging/dream/camera/s5k3e2fx.c +3 -7
index f0e49be..d205a2d 100644
@@ -1092,14 +1092,10 @@ static int32_t s5k3e2fx_move_focus(int direction, int32_t num_steps)
 
  	actual_step = step_direction * (int16_t)num_steps;
  	pos_offset = init_code + s5k3e2fx_ctrl->curr_lens_pos;
-	gain = actual_step * 0x400 / 5;
+	gain = actual_step / 5;
 
-	for (i = 0; i<= 4; i++) {
-		if (actual_step>= 0)
-			s_move[i] = ((((i+1)*gain+0x200) - (i*gain+0x200))/0x400);
-		else
-			s_move[i] = ((((i+1)*gain-0x200) - (i*gain-0x200))/0x400);
-	}
+	for (i = 0; i<= 4; i++)
+		s_move[i] = gain;
 
  	/* Ring Damping Code */
  	for (i = 0; i<= 4; i++) {
Signature · 1 line
1.6.5.4
Quoted 6 lines
Quoted 6 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

I've yet to see a reply from GregH. Has this patch been misplaced or dropped? Or is the patch no longer valid/correct?

Justin Madru

#3 lore raw
Quoted 71 lines · Justin Madru
Quoted 70 lines · Dan Carpenter
Quoted 15 lines · Justin Madru

I was style fixing some code when I ran into this code.
It seems like the code could be reduced, by factoring the expression. But this results in a very simple expression.

Am I assuming something wrong? Or is this a bug in the original code? This doesn't look right because the assignment of s_move[i] has no mention of the loop counter.

Justin

------

the code was looping, seting s_move[i] to the following calculations

if (actual_step>= 0)

Quoted 10 lines
         s_move[i] = ((((i + 1) * gain + 0x200) - (i * gain + 0x200)) / 0x400);
else
         s_move[i] = ((((i + 1) * gain - 0x200) - (i * gain - 0x200)) / 0x400);

but, by factoring the expression, it can be shown that they both reduce to a very simple expression:

         s_move[i] = gain / 0x400;

Heh.

Looks like an improvement to me.

If gain were 16 bits instead of 32 I would think the "* 0x400 / 0x400" was designed to mask out the highest 10 bits.

regards,
dan carpenter

Quoted 30 lines
---
  drivers/staging/dream/camera/s5k3e2fx.c |   10 +++-------
  1 files changed, 3 insertions(+), 7 deletions(-)
drivers/staging/dream/camera/s5k3e2fx.c +3 -7
index f0e49be..d205a2d 100644
@@ -1092,14 +1092,10 @@ static int32_t s5k3e2fx_move_focus(int direction, int32_t num_steps)
 
  	actual_step = step_direction * (int16_t)num_steps;
  	pos_offset = init_code + s5k3e2fx_ctrl->curr_lens_pos;
-	gain = actual_step * 0x400 / 5;
+	gain = actual_step / 5;
 
-	for (i = 0; i<= 4; i++) {
-		if (actual_step>= 0)
-			s_move[i] = ((((i+1)*gain+0x200) - (i*gain+0x200))/0x400);
-		else
-			s_move[i] = ((((i+1)*gain-0x200) - (i*gain-0x200))/0x400);
-	}
+	for (i = 0; i<= 4; i++)
+		s_move[i] = gain;
 
  	/* Ring Damping Code */
  	for (i = 0; i<= 4; i++) {
Signature · 1 line
1.6.5.4
Quoted 10 lines
Quoted 6 lines
Quoted 6 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

I've yet to see a reply from GregH. Has this patch been misplaced or dropped?
Or is the patch no longer valid/correct?

It's in my "to-apply" queue. I was working on the .33-rc1 merge first. After that comes out I'll get to my patch queue.

thanks,

greg k-h

#4 lore raw
In reply to Justin Madru
Quoted 7 lines · Justin Madru

I was style fixing some code when I ran into this code.
It seems like the code could be reduced, by factoring the expression. But this results in a very simple expression.

Am I assuming something wrong? Or is this a bug in the original code? This doesn't look right because the assignment of s_move[i] has no mention of the loop counter.

Algebraic factoring is not valid in general when dealing with pure integer expressions that may contain rounding issues. I haven't checked to see if that's the case with the code you're changing, but that sort of expression you're removing is very commonly used exactly to deal with rounding issues.

#5 lore raw
Quoted 10 lines · Ray Lee
Quoted 8 lines · Justin Madru

I was style fixing some code when I ran into this code.
It seems like the code could be reduced, by factoring the expression. But this results in a very simple expression.

Am I assuming something wrong? Or is this a bug in the original code? This doesn't look right because the assignment of s_move[i] has no mention of the loop counter.

Quoted 6 lines

Algebraic factoring is not valid in general when dealing with pure integer expressions that may contain rounding issues. I haven't checked to see if that's the case with the code you're changing, but that sort of expression you're removing is very commonly used exactly to deal with rounding issues.

I know that's true and that's why I was questioning if my patch actually removed a necessary calculation. But, wouldn't you agree that if the code was suppose to deal with "rounding issues" that there's a simpler expression?

Justin

#6 lore raw
Quoted 3 lines · Justin Madru

But, wouldn't you agree that if the code was suppose to deal with "rounding issues" that there's a
simpler expression?

No, I don't agree. Five minutes of effort below shows your patch will generate different numbers than the original. If this is controlling a stepper motor trying to hit a home position, it's off now. Or, the errors in the expressions for moving near and far may have balanced each other out before, and now there may be a systematic error causing a skew over time toward one end rather than the other.

My point is that you need to run this past the guy with the actual hardware who wrote it in the first place such that it can be tested, and make sure the slapped-together expression isn't just working by accident, as ugly as it might be.

#include <stdio.h>

typedef int int32_t;
typedef short int16_t;
typedef unsigned int uint32_t;

enum {MOVE_NEAR, MOVE_FAR} move_direction;

int32_t s5k3e2fx_move_focus(int direction, int32_t num_steps)

{
        int32_t i;
        int16_t step_direction;
        int16_t actual_step;
        int16_t s_move[5], s_move_2[5];
        uint32_t gain, gain_2;

        if (direction == MOVE_NEAR)
                step_direction = 20;
        else
                step_direction = -20;

        actual_step = step_direction * (int16_t)num_steps;

        gain = actual_step * 0x400 / 5;
	gain_2 = actual_step / 5;
	
        for (i = 0; i <= 4; i++) {
                if (actual_step >= 0)
                        s_move[i] = ((((i+1)*gain+0x200) -
(i*gain+0x200))/0x400);
                else
                        s_move[i] = ((((i+1)*gain-0x200) -
(i*gain-0x200))/0x400);
        }

        for (i = 0; i <= 4; i++)
                s_move_2[i] = gain_2;

	if (memcmp(s_move, s_move_2, sizeof(s_move))) {
		printf("s1, s2 differ for direction %d, num_steps %d\n", direction,
num_steps);
		for (i=0; i<5; i++)
			printf(" [%d] %d %d", i, s_move[i], s_move_2[i]);
		printf("\n");
	}

}

int main(void) {
	int steps;
	for (steps = -65535; steps < 65536; steps++) {
		s5k3e2fx_move_focus(MOVE_NEAR, steps);
		s5k3e2fx_move_focus(MOVE_FAR, steps);
	}
}
Quoted 3 lines

Justin

#7 lore raw
Quoted 66 lines · Ray Lee
Quoted 4 lines · Justin Madru

But, wouldn't you agree that if the code was suppose to deal with "rounding issues" that there's a
simpler expression?

No, I don't agree. Five minutes of effort below shows your patch will generate different numbers than the original. If this is controlling a stepper motor trying to hit a home position, it's off now. Or, the errors in the expressions for moving near and far may have balanced each other out before, and now there may be a systematic error causing a skew over time toward one end rather than the other.

My point is that you need to run this past the guy with the actual hardware who wrote it in the first place such that it can be tested, and make sure the slapped-together expression isn't just working by accident, as ugly as it might be.

#include<stdio.h>

typedef int int32_t;
typedef short int16_t;
typedef unsigned int uint32_t;

enum {MOVE_NEAR, MOVE_FAR} move_direction;

int32_t s5k3e2fx_move_focus(int direction, int32_t num_steps)

{
         int32_t i;
         int16_t step_direction;
         int16_t actual_step;
         int16_t s_move[5], s_move_2[5];
         uint32_t gain, gain_2;

         if (direction == MOVE_NEAR)
                 step_direction = 20;
         else
                 step_direction = -20;

         actual_step = step_direction * (int16_t)num_steps;

         gain = actual_step * 0x400 / 5;
	gain_2 = actual_step / 5;
	
         for (i = 0; i<= 4; i++) {
                 if (actual_step>= 0)
                         s_move[i] = ((((i+1)*gain+0x200) -
(i*gain+0x200))/0x400);
                 else
                         s_move[i] = ((((i+1)*gain-0x200) -
(i*gain-0x200))/0x400);
         }

         for (i = 0; i<= 4; i++)
                 s_move_2[i] = gain_2;

	if (memcmp(s_move, s_move_2, sizeof(s_move))) {
		printf("s1, s2 differ for direction %d, num_steps %d\n", direction,
num_steps);
		for (i=0; i<5; i++)
			printf(" [%d] %d %d", i, s_move[i], s_move_2[i]);
		printf("\n");
	}

}

Quoted 9 lines
int main(void) {
	int steps;
	for (steps = -65535; steps<  65536; steps++) {
		s5k3e2fx_move_focus(MOVE_NEAR, steps);
		s5k3e2fx_move_focus(MOVE_FAR, steps);
	}
}

Ok, I tested the example code and it does lead to different values! But, I did some testing and came up with a new patch that has been tested this time to come up with the same values as the old code but uses less calculations.

gain = ((actual_step << 10) / 5) >> 10;
        for (i = 0; i <= 4; i++)
                s_move[i] = gain;

Greg, disregard my last patch. Instead, please accept my new patch -- pending review.

        http://lkml.org/lkml/2009/12/15/453

Justin Madru