Skip to content

Commit bf8faaf

Browse files
NiLuJeFrenzie
authored andcommitted
Switch back to if ladders for inner loops (koreader#936)
Potentially very slightly faster on decent CPUs (branch prediciton?).
1 parent 30f005f commit bf8faaf

File tree

1 file changed

+34
-51
lines changed

1 file changed

+34
-51
lines changed

blitbuffer.c

+34-51
Original file line numberDiff line numberDiff line change
@@ -114,59 +114,42 @@ static const char*
114114
// just so that new buffer can be used for the memcpy-based fast paths...
115115

116116
#define BB_GET_PIXEL(bb, rotation, COLOR, x, y, pptr) \
117-
switch (rotation) { \
118-
case 0: \
119-
*pptr = (COLOR*)(bb->data + y * bb->pitch) + x; \
120-
break; \
121-
case 1: \
122-
*pptr = (COLOR*)(bb->data + x * bb->pitch) + bb->w - y - 1; \
123-
break; \
124-
case 2: \
125-
*pptr = (COLOR*)(bb->data + (bb->h - y - 1) * bb->pitch) + bb->w - x - 1; \
126-
break; \
127-
case 3: \
128-
*pptr = (COLOR*)(bb->data + (bb->h - x - 1) * bb->pitch) + y; \
129-
break; \
130-
}
117+
({ \
118+
if (rotation == 0) { \
119+
*pptr = (COLOR*)(bb->data + y * bb->pitch) + x; \
120+
} else if (rotation == 1) { \
121+
*pptr = (COLOR*)(bb->data + x * bb->pitch) + bb->w - y - 1; \
122+
} else if (rotation == 2) { \
123+
*pptr = (COLOR*)(bb->data + (bb->h - y - 1) * bb->pitch) + bb->w - x - 1; \
124+
} else if (rotation == 3) { \
125+
*pptr = (COLOR*)(bb->data + (bb->h - x - 1) * bb->pitch) + y; \
126+
} \
127+
})
131128

132129
#define SET_ALPHA_FROM_A(bb, bb_type, bb_rotation, x, y, alpha) \
133-
switch (bb_type) { \
134-
case TYPE_BB8: \
135-
{ \
136-
Color8 *srcptr; \
137-
BB_GET_PIXEL(bb, bb_rotation, Color8, x, y, &srcptr); \
138-
*alpha = srcptr->a; \
139-
} \
140-
break; \
141-
case TYPE_BB8A: \
142-
{ \
143-
Color8A *srcptr; \
144-
BB_GET_PIXEL(bb, bb_rotation, Color8A, x, y, &srcptr); \
145-
*alpha = srcptr->a; \
146-
} \
147-
break; \
148-
case TYPE_BBRGB16: \
149-
{ \
150-
ColorRGB16 *srcptr; \
151-
BB_GET_PIXEL(bb, bb_rotation, ColorRGB16, x, y, &srcptr); \
152-
*alpha = ColorRGB16_To_A(srcptr->v); \
153-
} \
154-
break; \
155-
case TYPE_BBRGB24: \
156-
{ \
157-
ColorRGB24 *srcptr; \
158-
BB_GET_PIXEL(bb, bb_rotation, ColorRGB24, x, y, &srcptr); \
159-
*alpha = RGB_To_A(srcptr->r, srcptr->g, srcptr->b); \
160-
} \
161-
break; \
162-
case TYPE_BBRGB32: \
163-
{ \
164-
ColorRGB32 *srcptr; \
165-
BB_GET_PIXEL(bb, bb_rotation, ColorRGB32, x, y, &srcptr); \
166-
*alpha = RGB_To_A(srcptr->r, srcptr->g, srcptr->b); \
167-
} \
168-
break; \
169-
}
130+
({ \
131+
if (bb_type == TYPE_BB8) { \
132+
Color8 *srcptr; \
133+
BB_GET_PIXEL(bb, bb_rotation, Color8, x, y, &srcptr); \
134+
*alpha = srcptr->a; \
135+
} else if (bb_type == TYPE_BB8A) { \
136+
Color8A *srcptr; \
137+
BB_GET_PIXEL(bb, bb_rotation, Color8A, x, y, &srcptr); \
138+
*alpha = srcptr->a; \
139+
} else if (bb_type == TYPE_BBRGB16) { \
140+
ColorRGB16 *srcptr; \
141+
BB_GET_PIXEL(bb, bb_rotation, ColorRGB16, x, y, &srcptr); \
142+
*alpha = ColorRGB16_To_A(srcptr->v); \
143+
} else if (bb_type == TYPE_BBRGB24) { \
144+
ColorRGB24 *srcptr; \
145+
BB_GET_PIXEL(bb, bb_rotation, ColorRGB24, x, y, &srcptr); \
146+
*alpha = RGB_To_A(srcptr->r, srcptr->g, srcptr->b); \
147+
} else if (bb_type == TYPE_BBRGB32) { \
148+
ColorRGB32 *srcptr; \
149+
BB_GET_PIXEL(bb, bb_rotation, ColorRGB32, x, y, &srcptr); \
150+
*alpha = RGB_To_A(srcptr->r, srcptr->g, srcptr->b); \
151+
} \
152+
})
170153

171154
void BB_fill_rect(BlitBuffer *bb, int x, int y, int w, int h, uint8_t v) {
172155
int rotation = GET_BB_ROTATION(bb);

0 commit comments

Comments
 (0)