cgen: refactor patch_pixconv.go

This commit is contained in:
Nigel Tao committed 2023-06-09 17:47:55 +10:00
1 parent 311ea1a1f4
commit aec9003ea0
1 file changed
+72 -88
+72 -88
View File
@@ -38,6 +38,8 @@ func insertBasePixConvSubmoduleYcckC(buf *buffer) error {
} }
stripCFuncBangComments(buf, prefix) stripCFuncBangComments(buf, prefix)
// ----
const dstIterEtc = "" + const dstIterEtc = "" +
"size_t dst_stride = dst->private_impl.planes[0].stride;\n" + "size_t dst_stride = dst->private_impl.planes[0].stride;\n" +
"uint8_t* dst_iter =\n" + "uint8_t* dst_iter =\n" +
@@ -56,88 +58,53 @@ func insertBasePixConvSubmoduleYcckC(buf *buffer) error {
"src_ptr1 += stride1;\n" + "src_ptr1 += stride1;\n" +
"src_ptr2 += stride2;\n" "src_ptr2 += stride2;\n"
// ---- generalBoxFilterPatches := []struct {
newFuncNameLine string
patchMap map[string]string
}{{
if err := patchCFunc(buf, generalBoxFilter, [][2]string{{
"wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n",
"wuffs_base__pixel_swizzler__swizzle_ycc__bgrx__box_filter(\n", "wuffs_base__pixel_swizzler__swizzle_ycc__bgrx__box_filter(\n",
map[string]string{
"// ¡ dst_iter = etc\n": dstIterEtc,
"// ¡ BEGIN set_color_u32_at\n": strings.Replace(setColorU32At, "_FLAVOR", "", 1),
},
}, { }, {
" // ¡ dst_iter = etc\n",
dstIterEtc,
}, {
" // ¡ BEGIN set_color_u32_at\n",
strings.Replace(setColorU32At, "_FLAVOR", "", 1),
}}); err != nil {
return err
}
// ----
if err := patchCFunc(buf, generalBoxFilter, [][2]string{{
"wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n",
"wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__box_filter(\n", "wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__box_filter(\n",
map[string]string{
"// ¡ dst_iter = etc\n": dstIterEtc,
"// ¡ BEGIN set_color_u32_at\n": strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1),
},
}, { }, {
" // ¡ dst_iter = etc\n",
dstIterEtc,
}, {
" // ¡ BEGIN set_color_u32_at\n",
strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1),
}}); err != nil {
return err
}
// ----
if err := patchCFunc(buf, generalBoxFilter, [][2]string{{
"wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n",
"wuffs_base__pixel_swizzler__swizzle_ycc__bgrx__hv11(\n", "wuffs_base__pixel_swizzler__swizzle_ycc__bgrx__hv11(\n",
map[string]string{
"// ¡ dst_iter = etc\n": dstIterEtc,
"// ¡ BEGIN set_color_u32_at\n": strings.Replace(setColorU32At, "_FLAVOR", "", 1),
"// ¡ BEGIN declare iy\n": "",
"// ¡ BEGIN declare ix\n": "",
"// ¡ BEGIN use ix\n": useIxHv11,
"// ¡ BEGIN use iy\n": useIyHv11,
},
}, { }, {
" // ¡ dst_iter = etc\n",
dstIterEtc, "wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__hv11(\n",
}, { map[string]string{
" // ¡ BEGIN set_color_u32_at\n", "// ¡ dst_iter = etc\n": dstIterEtc,
strings.Replace(setColorU32At, "_FLAVOR", "", 1), "// ¡ BEGIN set_color_u32_at\n": strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1),
}, { "// ¡ BEGIN declare iy\n": "",
" // ¡ BEGIN declare iy\n", "// ¡ BEGIN declare ix\n": "",
"", "// ¡ BEGIN use ix\n": useIxHv11,
}, { "// ¡ BEGIN use iy\n": useIyHv11,
" // ¡ BEGIN declare ix\n", },
"", }}
}, {
" // ¡ BEGIN use ix\n", for _, p := range generalBoxFilterPatches {
useIxHv11, const oldFuncNameLine = "wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n"
}, { p.patchMap[oldFuncNameLine] = p.newFuncNameLine
" // ¡ BEGIN use iy\n", if err := patchCFunc(buf, generalBoxFilter, oldFuncNameLine, p.patchMap); err != nil {
useIyHv11,
}}); err != nil {
return err return err
} }
// ----
if err := patchCFunc(buf, generalBoxFilter, [][2]string{{
"wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n",
"wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__hv11(\n",
}, {
" // ¡ dst_iter = etc\n",
dstIterEtc,
}, {
" // ¡ BEGIN set_color_u32_at\n",
strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1),
}, {
" // ¡ BEGIN declare iy\n",
"",
}, {
" // ¡ BEGIN declare ix\n",
"",
}, {
" // ¡ BEGIN use ix\n",
useIxHv11,
}, {
" // ¡ BEGIN use iy\n",
useIyHv11,
}}); err != nil {
return err
} }
// ---- // ----
@@ -195,13 +162,11 @@ func stripCFuncBangComments(buf *buffer, s string) {
buf.writes(s) buf.writes(s)
} }
func patchCFunc(buf *buffer, src string, patches [][2]string) error { func patchCFunc(buf *buffer, src string, oldFuncNameLine string, patchMap map[string]string) error {
srcFuncNameParenNewline := patches[0][0]
buf.writes("// Specializes ") buf.writes("// Specializes ")
buf.writes(srcFuncNameParenNewline[:len(srcFuncNameParenNewline)-2]) buf.writes(oldFuncNameLine[:len(oldFuncNameLine)-2])
buf.writes(".\n") buf.writes(".\n")
loop:
for src != "" { for src != "" {
line := "" line := ""
if i := strings.IndexByte(src, '\n'); i < 0 { if i := strings.IndexByte(src, '\n'); i < 0 {
@@ -210,12 +175,26 @@ loop:
line, src = src[:i+1], src[i+1:] line, src = src[:i+1], src[i+1:]
} }
for _, patch := range patches { key, indent := line, ""
if line != patch[0] { for i := 0; ; i++ {
if i == len(line) {
key, indent = "", line
break
} else if line[i] != ' ' {
key, indent = line[i:], line[:i]
break
}
}
val, ok := patchMap[key]
if !ok {
if !isCFuncComment(line) {
buf.writes(line)
}
continue
}
indentCFunc(buf, indent, val)
if !strings.Contains(key, "¡ BEGIN") {
continue continue
} else if !strings.Contains(line, "¡ BEGIN") {
buf.writes(patch[1])
continue loop
} }
// Extend forwards past (including) the next '\n'. // Extend forwards past (including) the next '\n'.
@@ -229,14 +208,6 @@ loop:
j++ j++
} }
src = src[j:] src = src[j:]
buf.writes(patch[1])
continue loop
}
if !isCFuncComment(line) {
buf.writes(line)
}
} }
buf.writes("\n") buf.writes("\n")
return nil return nil
@@ -247,3 +218,16 @@ func isCFuncComment(s string) bool {
} }
return (len(s) >= 2) && (s[0] == '/') && (s[1] == '/') return (len(s) >= 2) && (s[0] == '/') && (s[1] == '/')
} }
func indentCFunc(buf *buffer, indent string, s string) {
for s != "" {
line := ""
if i := strings.IndexByte(s, '\n'); i < 0 {
line, s = s, ""
} else {
line, s = s[:i+1], s[i+1:]
}
buf.writes(indent)
buf.writes(line)
}
}