
Contrast: fixed-point scaling around 128
The same RGB24 buffer is scaled around channel midpoint 128. A Q8 fixed-point factor of 384 corresponds to 1.5. XCODE expresses arithmetic and clamping; native assembly instructions execute it.
Natively checked: both variants at O0 through O3. Measured with the released V8 compiler (PBXB64 V23).
Arithmetic and memory contract
c is an input channel from 0 to 255. k is an integer Q8 factor from 0 to 1024: 128 means 0.5; 256 means 1.0; 384 means 1.5. SAR by 8 bits rounds negative results downward, matching floor. With k=0 every channel becomes 128.
Input
Output
Interactive browser explanation of the formula. It does not execute a compiler or change the native measurements documented below.
- The harness validates the factor range before entering the kernel. All intermediate values fit safely in I32 within this range. Changing the formula or input bounds requires a new overflow check.
- X.SUB moves the midpoint to zero. X.MUL applies the Q8 factor; X.SAR performs signed division by 256. X.ADD moves the midpoint back to 128.
- X.CLAMP bounds the resulting channel to 0 through 255. MOVZX and the final byte MOV remain visible ordinary assembly instructions within the XCODE block.
- Pure assembly uses the same fixed-point arithmetic with SUB, IMUL, SAR and ADD, and the same rounding. Different mathematics therefore does not distort the comparison.
- Both variants read the same source on every repetition and write a separate destination. RGB24 has no alpha channel here; BGRA32 would require explicitly preserving alpha.
- Native comparison checks all 256 inputs with k=0, 128, 256, 384 and 1024. With k=384, examples are 64 → 32, 128 → 128 and 192 → 224.
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] contrast_repeat: xor ecx, ecx contrast_channel: movzx eax, BYTE PTR [r8+rcx] sub eax, 128 imul eax, r10d sar eax, 8 add eax, 128 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 contrast_channel dec r11 jnz contrast_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.SUB V(channel), 128
X.MUL V(channel), V(parameter)
X.SAR V(channel), 8
X.ADD V(channel), 128
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
#ENDXWhy 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 assemblyMeasured runtime
Per sample: 4,000 × 2304 channel. Default parameters as above; sliders do not alter these recorded measurements.
| Variant | Median (ms) | Min–Max (ms) | ns / channel |
|---|---|---|---|
| ASM | 9.690 | 9.326–10.792 | 1.051 |
| XCODE + ASM | 46.585 | 45.928–49.295 | 5.055 |
XCODE/ASM time ratio: 4.81×. Pure assembly is faster in this measurement.
First measurement with the experimental V22 build (2026-10-01, medians): ASM 10.677 ms, XCODE 50.777 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.
Run the complete program yourself
PBXB64.exe xcode-contrast.pb -O2 -o graphics.exe .\graphics.exe ASM 4000 5 3 0 0 384 .\graphics.exe XCODE 4000 5 3 0 0 384
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 ZIPRe-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.