aboutsummaryrefslogtreecommitdiffstats
path: root/src/types
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-10-22 20:32:44 +0200
committerbptato <nincsnevem662@gmail.com>2024-10-22 20:53:33 +0200
commitb0bb61e830e76169f41db186b5946ae6859f4e6a (patch)
tree1ec2d343fe6bfa3f5fdacd1ca0e69cb57c558539 /src/types
parentb9455ef09f4d33dd9e3cb291ed6e689b51301e22 (diff)
downloadchawan-b0bb61e830e76169f41db186b5946ae6859f4e6a.tar.gz
color: use 64-bit SWAR
I originally left this at 32-bits because I was on a 32-bit arch, but that is no longer the case.
Diffstat (limited to 'src/types')
-rw-r--r--src/types/color.nim49
1 files changed, 15 insertions, 34 deletions
diff --git a/src/types/color.nim b/src/types/color.nim
index 815f769f..7e9d9d8d 100644
--- a/src/types/color.nim
+++ b/src/types/color.nim
@@ -290,41 +290,22 @@ func serialize*(color: ARGBColor): string =
func `$`*(argbcolor: ARGBColor): string =
return argbcolor.serialize()
-# https://arxiv.org/pdf/2202.02864.pdf
-func fastmul*(c, ca: uint32): uint32 =
- let u = c or 0xFF000000u32
- var rb = u and 0x00FF00FFu32
- rb *= ca
- rb += 0x00800080
- rb += (rb shr 8) and 0x00FF00FFu32
- rb = rb and 0xFF00FF00u32
- var ga = (u shr 8) and 0x00FF00FFu32
- ga *= ca
- ga += 0x00800080
- ga += (ga shr 8) and 0x00FF00FFu32
- ga = ga and 0xFF00FF00u32
- return ga or (rb shr 8)
-
-# fastmul, but preserves alpha
-func fastmul1*(c, ca: uint32): uint32 =
- let u = c
- var rb = u and 0x00FF00FFu32
- rb *= ca
- rb += 0x00800080
- rb += (rb shr 8) and 0x00FF00FFu32
- rb = rb and 0xFF00FF00u32
- var ga = (u shr 8) and 0x00FF00FFu32
- ga *= ca
- ga += 0x00800080
- ga += (ga shr 8) and 0x00FF00FFu32
- ga = ga and 0xFF00FF00u32
- return ga or (rb shr 8)
-
-func fastmul1(c: ARGBColor; ca: uint32): ARGBColor =
- return ARGBColor(fastmul1(uint32(c), ca))
+# Divide each component by 255, multiply them by n, and discard the fractions.
+# See https://arxiv.org/pdf/2202.02864.pdf for details.
+func fastmul*(c: ARGBColor; n: uint32): ARGBColor =
+ var c = (uint64(c) shl 24) or uint64(c)
+ c = c and 0x00FF00FF00FF00FFu64
+ c *= n
+ c += 0x80008000800080u64
+ c += (c shr 8) and 0x00FF00FF00FF00FFu64
+ c = c and 0xFF00FF00FF00FF00u64
+ c = (c shr 32) or (c shr 8)
+ return ARGBColor(c)
func premul(c: ARGBColor): ARGBColor =
- return ARGBColor(fastmul(uint32(c), uint32(c.a)))
+ let a = uint32(c.a)
+ let c = ARGBColor(uint32(c) or 0xFF000000u32)
+ return c.fastmul(a)
# This is somewhat faster than floats or a lookup table, and is correct for
# all inputs.
@@ -342,7 +323,7 @@ func blend*(c0, c1: ARGBColor): ARGBColor =
let pc0 = c0.premul()
let pc1 = c1.premul()
let k = 255 - pc1.a
- let mc = fastmul1(pc0, uint32(k))
+ let mc = pc0.fastmul(uint32(k))
let rr = pc1.r + mc.r
let rg = pc1.g + mc.g
let rb = pc1.b + mc.b