An RGB24 buffer contains 2304 color channels for 32 x 24 pixels. Each R, G and B value receives the same offset, +40 here. XCODE expresses the handwritten bounds with X.CLAMP; memory accesses remain assembly.
Scientific workshop board with German and English labels. Open at full size

Brightness: clamped color-channel addition

An RGB24 buffer contains 2304 color channels for 32 x 24 pixels. Each R, G and B value receives the same offset, +40 here. XCODE expresses the handwritten bounds with X.CLAMP; memory accesses remain assembly.

Natively checked: both variants at O0 through O3. Measured with the released V8 compiler (PBXB64 V23).

Arithmetic and memory contract

c′ = clamp(c + b, 0, 255)

c is a byte color channel from 0 to 255 and b a brightness offset from -255 to +255. clamp bounds the result to the byte range: negative values become 0 and values above 255 become 255. This operates on stored color values, not physical luminance.

Channel transfer curve006464128128192192255255cc′

Input

Output

Interactive browser explanation of the formula. It does not execute a compiler or change the native measurements documented below.

  1. RGB24 has three consecutive bytes per pixel and no alpha channel. The source buffer stays unchanged; the destination receives the result. Rows have no padding here.
  2. MOVZX inside X.RAW reads a channel as an I32 value from 0 to 255. V(source) and V(index) are replaced with assigned registers.
  3. X.ADD adds the offset. X.CLAMP requires virtual registers of the same type: channel, minimum and maximum are therefore all I32; bounds 0 and 255 are initialized beforehand.
  4. MOV with V8(channel) writes only the low eight bits of the already clamped value. Pure assembly uses CMP and CMOVL/CMOVG for bounds without branching per channel.
  5. X.NEXT always closes the innermost open X.FOR. The inner loop processes 2304 channels; the outer loop repeats the unchanged input for measurement. Brightness is not repeatedly applied to the previous result.
  6. Native comparison checks all 256 input values with -255, -40, 0, +40 and +255. With +40, examples are 0 → 40, 128 → 168 and 240 → 255.

Pure x64 assembly

The following kernel appears in full in the downloadable program. The shared BASIC harness supplies buffers and timing.

#ASM
mov r8, QWORD PTR [input_addr]
 mov r9, QWORD PTR [buffer_addr]
 mov r10d, DWORD PTR [parameter]
 mov r11, QWORD PTR [rounds]
brightness_repeat:
 xor ecx, ecx
brightness_channel:
 movzx eax, BYTE PTR [r8+rcx]
 add eax, r10d
 xor edx, edx
 cmp eax, edx
 cmovl eax, edx
 mov edx, 255
 cmp eax, edx
 cmovg eax, edx
 mov BYTE PTR [r9+rcx], al
 inc rcx
 cmp rcx, 2304
 jb brightness_channel
 dec r11
 jnz brightness_repeat
#ENDASM

Typed XCODE instructions mixed with regular assembly

The following kernel appears in full in the downloadable program. The shared BASIC harness supplies buffers and timing.

#XCODE OPT=2, SPILL=AUTO, FEATURES=BASELINE
X.VREG source:PTR
 X.VREG destination:PTR
 X.VREG channel:I32
 X.VREG parameter:I32
 X.VREG minimum:I32
 X.VREG maximum:I32
 X.VREG index:U64
 X.VREG pass:U64
 X.VREG limit:U64
 X.LOAD V(source), QWORD PTR [input_addr]
 X.LOAD V(destination), QWORD PTR [buffer_addr]
 X.LOAD V(parameter), DWORD PTR [parameter]
 X.LOAD V(limit), QWORD PTR [rounds]
 X.MOVE V(minimum), 0
 X.MOVE V(maximum), 255
 X.FOR V(pass), 0, V(limit), 1
  X.FOR V(index), 0, 2304, 1
   X.RAW
    movzx V(channel), BYTE PTR [V(source)+V(index)]
   X.ENDRAW
   X.ADD V(channel), V(parameter)
   X.CLAMP V(channel), V(minimum), V(maximum)
   X.RAW
    mov BYTE PTR [V(destination)+V(index)], V8(channel)
   X.ENDRAW
  X.NEXT
 X.NEXT
#ENDX

Why does this remain native assembly?

XCODE is lowered to concrete x64 instructions during compilation. X.RAW contains regular assembly; V(name) and V8(name) refer to assigned physical registers or their byte view. There is no XCODE runtime interpreter here. Types, loops and X.CLAMP simplify expressing intent but do not guarantee the speed of a hand-tuned kernel.

Open the actual expanded XCODE assembly

Measured runtime

Per sample: 4,000 × 2304 channel. Default parameters as above; sliders do not alter these recorded measurements.

VariantMedian (ms)Min–Max (ms)ns / channel
ASM6.7636.558–7.3980.734
XCODE + ASM46.30145.817–47.4865.024

XCODE/ASM time ratio: 6.85×. Pure assembly is faster in this measurement.

First measurement with the experimental V22 build (2026-10-01, medians): ASM 7.006 ms, XCODE 48.640 ms. The V8 compiler generates the same machine instructions for these kernels; the differences of a few percent are within measurement scatter and are not a compiler speed gain.

Measured on 2026-10-05 using Intel(R) Core(TM) i9-7980XE CPU @ 2.60GHz, Windows build 26300. QPC measures the kernel call including small call/dispatch costs; buffer initialization, process startup and output are excluded. Two warm-up pairs are discarded, followed by nine measured pairs in alternating order. Both variants use identical inputs and are checked byte-for-byte against the same reference result.

These scalar measurements repeat small hot buffers: the same point, line or 2304 source channels. They do not measure general image processing, SIMD performance or graphics devices. Other system work was not disabled; min/max and raw samples are provided. Nanoseconds per element are calculated from measured batch time.

PBXB64 version 23.0.0-V23 from the released PBX-Compiler V8 package (PBX-Basic_08.zip, digitally signed); SHA-256 465c6d67dae523e4c3c75ae8aa3975c602a5839b3e6e82e8a7cad9a5c67fe3e9. The checks cover these tutorial examples; they are not a general compiler qualification.

Raw measurements, test cases and compiler/program hashes

Run the complete program yourself

PBXB64.exe xcode-brightness.pb -O2 -o graphics.exe
.\graphics.exe ASM 4000 5 3 0 0 40
.\graphics.exe XCODE 4000 5 3 0 0 40

Arguments: variant, repetitions, start-x, start-y, target-x, target-y, brightness/factor. The harness requires 1 through 100000000 repetitions and validates the brightness/factor range. Exit0 checks guard bytes; TIMING contains QPC ticks and frequency, BYTE contains result bytes. Compare the whole buffer as well as the exit code.

Download both variants and timing harnessAll four graphics examples as ZIP

Re-checked with the V8 compiler on 2026-10-05: 24 O0–O3 program variants of the four graphics examples and all 276 published output and boundary cases produce byte-identical results.

More graphics examples

PBXB64 is part of the PBX-Compiler package. Free for private use, cleanly licensed for business.