Vector forms: SSE2
This document says which procedures of the system compute with vectors, in which form, what stays scalar, and how to measure each one. The terms are in Glossary (scalar form, vector form) and Glossary (usable instruction set, extended state).
The idea in plain words. A processor has registers that hold several numbers side by side, and instructions that compute on all of them at once. A procedure that works on many pixels or many words can use them: it does the work of four values with one instruction. That is its vector form. The same procedure written with one value for each instruction is its scalar form. In this system a procedure that has a vector form also keeps its scalar form, and the two must give the same bytes for every input: the vector form is only faster, never different. Host tests compare the two, so a fault of the vector form shows as a wrong byte, not as a wrong picture.
What has a vector form today. Eight procedures, all with 128-bit registers (SSE2 on x86-64):
What this document is not. It is not the rule on the vector registers of the kernel. The kernel has no vector instruction at all, and it saves the registers of each thread: Architecture: The extended state of the processor. It is not the design of the blend in linear light either: The text of the system: a vector font with subpixel bands: The blend in linear light.
For whom. A reader who writes or changes a vector form, and a reader who measures one.
The map of this document. Section 1: the rule of the same bytes. Section 2: why one form, SSE2. Sections 3 to 6: the blend of a glyph, the compositor, BLAKE3, the check of a text. Section 7: how to measure on the machine. Section 8: limits. Section 9: the 16-tag group match. Section 10: the fuzzy match. Section 11: the triangles of the WebGPU subset.
1. The rule of the same bytes
The rule: a vector form gives the same bytes as its scalar form, for every input. Two facts of the system need it. The screenshots of the system are compared byte for byte. One source gives one module, and the hash of a module is its identity: a hash that depends on the form is not a hash of the content.
How a vector form keeps the rule:
- Integers only. A vector form does the arithmetic of the scalar form in lanes (a lane is the part of a register that holds one value), with the same widths of the intermediate values. It rounds nowhere where the scalar form does not.
- The scalar form stays beside each vector form. It has three uses: it is the reference of the host tests, it is the code of a target that is not amd64, and it is the code of the last values of a row that do not fill a group of the vector form.
- No load and no store outside the slice of the caller. A vector form loads and stores a group of values only when the whole group is inside the slice. The columns after the last whole group go through the scalar form. It allocates nothing.
- One public procedure. The caller does not choose a form. The public procedure chooses at compile time, by the target:
blend_premultiplied_alpha_row :: #force_inline proc "contextless" (to: [^]Color, from: [^]u32, count: int) {
when ODIN_ARCH == .amd64 {
blend_premultiplied_alpha_row_sse2(to, from, count)
} else {
blend_premultiplied_alpha_row_scalar(to, from, count)
}
}
The host tests compare each vector form with its scalar form on inputs from a generator with a fixed seed, so each host gets the same inputs:
- every length from 0 to 67;
- every place of the input against a boundary of 16 bytes (a pixel is 4 bytes, so 4 places for a row of pixels; 16 places for the bytes of a hash);
- the edge values often: 0, 1, 254 and 255 in each byte, whole words of 0, of all ones and of full coverage;
- guard words before and after each row: a form that writes outside its columns changes them.
When a test fails, it gives the first wrong pixel, the value of each form, and the count of pixels changed outside the drawn columns. The tests of the blends are in src/library/modus/graphics/blend_vectors_test_amd64.odin (coverage_forms_give_the_bytes_of_the_scalar_form, coverage_forms_meet_each_channel_value, premultiplied_forms_give_the_bytes_of_the_scalar_form, premultiplied_forms_meet_each_channel_value, straight_alpha_forms_give_the_bytes_of_the_scalar_form, straight_alpha_forms_meet_each_channel_value, coverage_forms_with_alpha_give_the_bytes_of_the_scalar_form). The tests of the hash are in src/library/modus/contract/blake3/chunks_side_by_side_test_amd64.odin (sse2_form_gives_the_hash_of_the_scalar_form, subtrees_side_by_side_at_each_counter). The tests of the check of a text are in src/library/modus/contract/text_encoding/text_valid_sixteen_bytes_at_a_time_test.odin (section 6). The test of the 16-tag group match is in src/library/modus/hash_table/hash_table_test.odin (group_forms_give_the_mask_of_the_scalar_form). The tests of the fuzzy match are in src/library/modus/line_editor/fuzzy_match_vectors_test.odin (vector_form_at_the_edges, vector_form_gives_the_result_of_the_scalar_form). modustool check runs them.
2. One form: SSE2
The vector forms use the 128-bit registers of SSE2. Two facts make this form free of conditions:
- Every x86-64 processor has SSE2. It is a part of the architecture, not an extension that one processor has and another lacks.
- The kernel saves these registers for every thread, at each switch of threads, on every processor (Architecture: The extended state of the processor).
So a vector form needs no test of the processor and no choice at the start of a process: on amd64 the public procedure runs the SSE2 form, and on another target the scalar form.
How a module is compiled. A module is compiled for the plain x86-64 processor, with SSE2: for a module, llc gets no -mattr and no -mcpu (build_module in tools/modustool/build/modules.odin). The kernel is the one exception, in the other direction: it is compiled with every vector feature off (KERNEL_TARGET_FEATURES), and the build refuses a kernel that has a vector instruction outside the save and the restore of the extended state (tools/modustool/build/vector_check.odin). No vector form runs in the kernel.
How a vector form is written. The arithmetic is written with base:intrinsics on #simd[N]T types: simd_shuffle, simd_select, simd_lanes_eq, simd_shr, simd_shl, simd_bit_and, simd_bit_or, simd_bit_xor, simd_reduce_or, simd_reduce_and, simd_extract, unaligned_load, unaligned_store, the operators + - *, and cast between element widths. The compiler lowers them to SSE2 instructions. No form names an instruction of the processor, and no form is written in assembler.
What SSE2 is not here. It is not the only instruction set that a process can use. A process can read its usable instruction sets with src/library/modus/instruction_sets (usable_instruction_sets) and run AVX2 code where the processor has it and the kernel saves its registers. No vector form of the system does that today: the measures below say why.
What was measured about AVX2
An AVX2 form of each procedure, behind one choice at the start of a process (CPUID and XGETBV as instruction templates), was measured by the host tests on a host with AVX2. The host load was 10, above the limit of 8, so these times rank the forms and are not valid measures of speed:
Measure (host, -o:speed) | Scalar | SSE2 | AVX2 |
|---|---|---|---|
| One glyph from the glyph cache | 301 ns | 193 ns | 244 ns |
| BLAKE3 of 390 624 bytes | 835 us | 200 us | 383 us |
The build with no optimisation (the build of the host tests of modustool check) gave the same order: one glyph 1382 / 813 / 4716 ns in a first run, BLAKE3 5920 / 1904 / 2002 us.
AVX2 lost to SSE2 in both places, and its form of the blend of a glyph gave wrong bytes in the build with no optimisation. So the system has the scalar form and the SSE2 form only: no AVX2 form, and no vector form that chooses at the start of a process.
What an AVX2 form needs
- 1. The registers saved, and a way to know it. This part exists. The kernel saves the extended state of a thread at each switch of threads: with
xsaveoptwhen the processor has it, with theymmregisters of AVX and the registers of AVX-512 when the processor has them; withfxsaveelse (src/modulus/extended_state.odin; Architecture: The extended state of the processor). A process reads whether AVX2 is usable withsrc/library/modus/instruction_sets, once at its start: CPUID is slow in a virtual machine. - 2. A form that wins by measure on the machine, with optimisation, on a host with a load under 8. The blend of a glyph reads a table at an index for each channel. The AVX2 gather (
vpgatherdd) is not faster than single loads on every processor, and on processors from Skylake to Ice Lake and Tiger Lake the correction against Gather Data Sampling (2023) makes it several times slower. A form to measure first: the arithmetic inymmregisters with the reads of one byte of the SSE2 form, and no gather. - 3. The cause of the wrong bytes understood. The facts:
- In the build with no optimisation, the AVX2 form gave the byte 03 in some channels. 03 is entry 0 of the table of square roots, so the gather read index 0 in those lanes: the vector of indices that the gather received had zeros where the indices were not 0. The build with optimisation gave the right bytes.
- One cause is known: an AVX2 procedure that passes vectors of 256 bits by value to procedures without the target feature (section "A lesson"). With the indices passed to the gather procedure through a pointer, the fault stayed.
- The probable cause: the same kind of fault at another place, where a vector of more than 128 bits passes between two procedures in a build that puts nothing inline. Candidates: the call of the binding
x86._mm256_i32gather_epi32(a procedure of the C convention that takes and gives a vector of 256 bits), or the vector of 512 bits that the shared arithmetic returned. A fault of the compiler in that build is also possible. - The way to know: read the LLVM IR of the test build for the gather procedure and its callers, and compare the vector of indices that is stored with the one that the gather instruction receives.
A lesson for later: vectors of more than 128 bits across procedures
A procedure with the target feature avx2 and a procedure without it pass a vector of more than 128 bits in different places: one ymm register, against two xmm registers or memory. A call by value between the two gives the callee other bytes. A build with no optimisation puts no procedure inline, so each such call is real.
The rule for a later AVX2 form:
- Such a vector crosses between the two kinds of procedure only through a pointer.
- A host test of the AVX2 form runs in the build with no optimisation and in the build with optimisation.
A vector of 128 bits or less is passed in one xmm register by both kinds. That is why the SSE2 forms have no such fault.
SSSE3, SSE4.1 and AVX-512
No form uses SSSE3 or SSE4.1. pshufb (SSSE3) does a rotation of 8 bits in one instruction in place of three in BLAKE3; pmulld and pblendvb (SSE4.1) have SSE2 forms of one to three instructions that the compiler writes. The gain is a few instructions in loops whose cost is elsewhere (the reads of the table, the 7 rounds). Such a form needs a test of the processor at the start of a process (src/library/modus/instruction_sets).
AVX-512 gives 512-bit registers (16 pixels, 16 chunks of BLAKE3 side by side), mask registers (the last pixels of a row with no scalar tail), and a gather of 16 lanes. The kernel already saves its three state components on a processor that has them: the extended state area of each thread has 2688 bytes for that (EXTENDED_STATE_CAPACITY). A form still needs the same test of the processor as AVX2, and a measure first: on some processors a 512-bit instruction lowers the clock of the core for a while.
3. The blend of a glyph (src/library/modus/graphics)
What it is. A glyph of the vector font is a rectangle of coverage words from the private glyph cache: for each pixel, how much of each color stripe of the screen the shape covers. The blend mixes the text color over the pixels of the canvas with that coverage, in linear light (The text of the system: a vector font with subpixel bands: Subpixel bands; The text of the system: a vector font with subpixel bands: The blend in linear light). It is the cost of every text of the system.
The hot loop is blend_coverage_row_in_order (src/library/modus/graphics/blend.odin): for each pixel with partial coverage, three channels, each square_root[(d * d * (255 - a) + c * c * a) >> 12], with d the pixel, c the color and a the coverage. The text of the system: a vector font with subpixel bands: The glyph cache, gives its cost: a glyph from the glyph cache costs about 9 times a glyph of Spleen.
The SSE2 form is blend_coverage_row_sse2 in src/library/modus/graphics/blend_vectors_amd64.odin, 4 pixels at a time. Each byte of the four pixels gets one lane of 16 bits (16 lanes, which the compiler splits in two registers), because the product of two bytes fits in 16 bits.
| Part | Form |
|---|---|
| The split of 4 pixels and 4 coverage words into 16 lanes of 16 bits (one lane for each byte) | vector: a widening conversion |
| The order of the stripes (red-green-blue, blue-green-red, none) | vector: one shuffle of lanes, chosen at compile time |
d * d, 255 - a, the two products, the sum, >> 12 | vector: 16-bit squares, 32-bit products and sum |
The read of square_root | 12 reads of one byte for 4 pixels: SSE2 has no gather |
| The choice of each channel (coverage 0: the pixel; 255: the color; else the table) and of each pixel (a word of 0: the pixel whole; a word that covers the three stripes: the color whole) | vector: comparisons and selects, no branch |
| A group of 4 words of 0, or of full coverage | one test for the group, before the arithmetic |
| The last 0 to 3 columns of a row | the scalar form |
The table is kept: 12 reads of one byte cost less than a square root computed in lanes (a search of 8 steps for each channel).
The blend of colors with straight alpha is the same arithmetic, with the alpha of each color as the coverage of its three channels and the channels of each color in place of one color: blend_straight_alpha_row_sse2, 4 pixels at a time. A group of four colors with alpha 0 is not read; a group of four opaque colors is written whole, with the high byte 0. A draw record of one color with alpha (graphics.blend_fill) blends a row of that color, 64 pixels at a time; an opaque record is a fill and takes no blend. The host test straight_alpha_forms_meet_each_channel_value also checks that the scalar form gives the bytes of the blend of a glyph whose word covers the three channels with the alpha: one rule for both.
The blend of a glyph with alpha, for a text whose color has an alpha below 255: each coverage is first scaled, round(coverage * alpha / 255) (coverage_with_alpha, t = c * a + 128, then (t + t >> 8) >> 8, exact for each product of two bytes, in lanes of 16 bits in the SSE2 form). No scaled coverage reaches 255, so the SSE2 form drops the test of a group covered whole, and keeps the test of a group of four words of 0. The blend tables give the result of each coverage, so the scaled coverage reads the table of the opaque color of the same channels. The host test coverage_forms_with_alpha_give_the_bytes_of_the_scalar_form compares the two forms for six alphas, and fast_blend_with_alpha_is_the_reference (text_test.odin) compares the blend tables with the reference.
The flag of the scalar form. The scalar form keeps the channels of the previous pixel, with a flag for "no previous pixel" (previous_known). The value 0xFFFFFFFF cannot mean "no previous pixel": a first pixel 0xFFFFFFFF is then blended as black. A canvas of the system holds 0x00RRGGBB only, but the blend must be right for every word. The host test coverage_scalar_form_reads_a_pixel_of_all_ones keeps the flag.
The blend table is a third path, not a vector form. For a text over one flat color, graphics.blend_glyph_rows reads the result of each channel from a blend table: three loads and a store for a pixel, with no arithmetic (The text of the system: a vector font with subpixel bands: Where the time goes, and the fast blend). The two paths meet in this way:
- A row of a glyph whose two end pixels have one color takes the blend table of that color.
- A row whose two ends differ (a gradient across, a picture) takes the form of the target: the SSE2 form on amd64.
- A lone pixel of another color inside a row of the table takes the scalar form: the SSE2 form gives a lone pixel to the scalar form anyway.
The three give the same bytes: the host test three_blends_give_the_same_bytes_for_each_cache compares the blend table, the SSE2 form alone and the scalar form alone.
Fill and copy
They have no vector form of their own:
graphics.copycallsintrinsics.mem_copy_non_overlapping, which iscopy_bytes_without_overlapofsrc/runtime/memory.asm. That procedure already moves 16 bytes at a time inxmmregisters: two or four moves that overlap for 16 to 64 bytes, a loop of 64 bytes above, andrep movsbfrom 2048 bytes to 1 MiB. It uses no AVX: the same file runs in the boot program and on a processor where AVX is not usable.graphics.fillis a loop of 32-bit stores, and the compiler makes it a vector loop: the code of the compositor haspshufd $0x0(the color in the four lanes) and stores of 16 bytes ininterface::execute_in_rectangle(objdump -d build/modules/compositor/linked.elf). A fill of 32-bit pixels is not a fill of bytes:fill_bytes_with_valuedoes not apply.
The kernel links another form of src/runtime/memory.asm (WITHOUT_VECTOR_REGISTERS): general registers, rep movsb and rep stosb only.
4. The compositor
The compositor draws each frame of the screen. This table says, for each kind of work of a frame, which form does it:
| Work of a frame | Form |
|---|---|
| The copy of the background and of a surface into the frame, and of the frame into the screen | copy_bytes_without_overlap of src/runtime/memory.asm: already vector moves |
| The swap of red and blue when the screen is blue-green-red | the compiler makes it a vector loop (psrld $0x10, pand, por on 16 bytes, in entry of the compositor) |
The blend of the pointer picture (blend_premultiplied_alpha) | SSE2 form, blend_premultiplied_alpha_row_sse2 |
Opaque rectangles on whole pixels (Rectangle records), opaque gradients down (a fill for each line) | graphics.fill: a vector loop of the compiler |
| Rectangles inside the pixels: at most nine parts, each of one covered alpha | SSE2 form, blend_straight_alpha_row_sse2, one blend_fill for each part: no coverage is computed for each pixel |
| Gradients across (one row of colors for each span of rows) and at an angle (one row for each line) | the row of colors is scalar: the level of each pixel, one division, and its mix; an opaque gradient across covered whole copies its row into each line (copy_bytes_without_overlap), else the SSE2 form blends the row, and a group of four opaque colors is a store |
Rectangles, gradients and pictures with alpha (graphics.blend_fill, graphics.blend_copy) | SSE2 form, blend_straight_alpha_row_sse2 |
| A text with alpha | the blend of a glyph with each coverage times the alpha: SSE2 form blend_coverage_row_sse2, and the blend tables, which the scaled coverage reads; Spleen and the replacement box blend each pixel of ink with blend_fill |
The blend of the pointer picture. A pointer picture has one alpha for each pixel, premultiplied (Pointer pictures: The format of the system: one pointer theme file). For each channel, with d the pixel and a the alpha of the source:
t = d * (255 - a) + 128, then(t + t >> 8) >> 8: a division by 255 that is exact for each 16-bit product.- The SSE2 form computes each channel in its own lane of 16 bits. The scalar form computes red and blue in two lanes of 16 bits of one word, and green alone: the lanes have the same values.
- The source is then added as words of 32 bits, as in the scalar form: a source that is not premultiplied carries from one channel into the next in both forms. The validation of a pointer theme file refuses such a pixel, so the compositor never draws one.
- A group of four transparent pixels is not read. A source with alpha 255 needs no case of its own in the SSE2 form:
255 - ais 0, and the result is the source without its alpha.
5. BLAKE3 (src/library/modus/contract/blake3)
Where the hash runs. The root service checks the hash of each resource before it gives it to a process, and the loader checks the hash of each module that it loads. The hash of a module is its identity, so every start of a process pays this cost.
How BLAKE3 cuts its input. The input is cut in chunks of 1024 bytes. Each chunk gives a chaining value of 8 words, and the chaining values are joined two by two in a tree, up to the root. The chunks do not depend on each other: several can be compressed at the same time.
The form is the one of the reference implementation of BLAKE3 for many chunks: the chunks of a subtree side by side, one chunk in each lane. The other candidate, the compression of one block of one chunk with the 16 words in 4 vectors (one row in each), gives less: the message words must be moved between lanes at each round.
src/library/modus/contract/blake3/chunks_side_by_side_amd64.odin(four_chunk_values_side_by_side): the 16 words of the state of 4 chunks in 16 vectors of 4 words. A round is the 8 mixes of the scalar form, each on whole vectors (mix_lanes). The words of a block of the 4 chunks are turned with a transpose of 4 x 4 words (shuffles). The counter of each chunk is computed for each lane in 64 bits, so a low word that passes 2^32 between two lanes is right.subtree_valuetakes this path for a subtree of exactly 4 chunks. The left part of a tree is always a power of two of whole chunks, so every input of more than 4096 bytes has such subtrees. The parents of the chunk values are the scalar form.- Scalar: an input of 4096 bytes or less, the last part of a tree that is not a power of two of chunks, the parents, the root.
blake3.hash and blake3.subtree_chaining_value take the SSE2 form on amd64 with no parameter of the caller. The host tests reach both forms through hash_in_form, which is private to the package.
The rotations by 16, 12, 8 and 7 bits are written as two shifts and an or (rotate_lanes_right); the compiler can write the rotation by 16 as shuffles of 16-bit lanes. The object code is not read yet.
6. The check of a text (src/library/modus/contract/text_encoding)
Where the check runs. Each border that takes a text from another party calls text_valid: the palette table, a store name, a history file, a paste, a display text. The rule is UTF-8 with no control character (Text encoding: UTF-8 in every contract in the repository).
The form is the algorithm of John Keiser and Daniel Lemire, "Validating UTF-8 In Less Than One Instruction Per Byte" (2021), with comparisons in place of its three lookups in tables of 16 bytes: SSE2 has no such lookup. The design and the measure that chose it: The check of a text, sixteen bytes at a time in the repository.
text_valid_sixteen_bytes_at_a_time: a group of 16 bytes and the group before it. A group of ASCII checks only its controls. Another group checks the count of continuations after each lead, the leads that start nothing, the bounds of the second byte after E0, ED, F0 and F4, and the controls.- The last bytes, fewer than 16, are copied into a group of spaces and checked as a group: no load reads past the slice.
- The form stops at the first group with an error.
Three differences from the other vector forms.
- The source has no
_amd64suffix andtext_validcalls it on each target: each of its operations (compares, saturating subtraction, shuffles by constant places, the mask of bit 7) compiles for each vector unit. Some take more than one instruction: SSE2 has no unsigned compare of bytes, and NEON has no mask of bit 7. - The last bytes go through the vector form, in a group of spaces, not through the scalar form.
- The kernel compiles the package in and calls none of it: it replaces invalid sequences with
invalid_sequences_replace, which is scalar. The build refuses a vector instruction in the kernel.
The host tests (text_valid_sixteen_bytes_at_a_time_test.odin): each length around a group (0, 1, 15, 16, 17, 31, 32, 33, 47, 48, 49, 63, 64) with a control at the end; each valid and refused sequence at each place of texts of each length up to 50, so each sequence crosses each boundary of a group at each of its bytes; a sequence cut by the end of the slice; slices that start at each place 1 to 15 of a larger buffer; each window of three bytes at the places 13, 14 and 15 of a text of 32 bytes, one window in 251 in each run and all 2^24 with -define:EXHAUSTIVE=true (8 s with no optimisation); each lead F0 to F4 at the places 12 to 15 with each second byte and the edge classes of the third and fourth bytes; a lead cut at the end of a group that is not ASCII, before groups of ASCII and with no last bytes; and 400 000 random texts of 0 to 300 bytes (random bytes, edge bytes, valid texts, valid texts with one, two or three bytes changed, inserted or removed) against the scalar form, with a fixed seed that the failure prints. The fuzz target text_encoding compares the two forms too.
The measure on the host, with the target of a module. The test is opt-in: it runs only with -define:MEASURE=true. It prints the time of each form, and fails only when the forms give different answers:
nix develop -c odin test src/library/modus/contract/text_encoding -o:speed -microarch:x86-64 -vet -collection:modus=src/library/modus -define:MEASURE=true -define:ODIN_TEST_NAMES=text_encoding.text_valid_speed_of_each_form
The numbers of the last measure, with a load of 1.7, the least of 5 runs:
| Text | Scalar form | Vector form |
|---|---|---|
| ASCII, 64 bytes | 482 MiB/s | 11 505 MiB/s |
| ASCII, 4 KiB | 482 MiB/s | 17 316 MiB/s |
| ASCII, 1 MiB | 480 MiB/s | 17 742 MiB/s |
| French and CJK, 63 bytes | 591 MiB/s | 3 243 MiB/s |
| French and CJK, 4 KiB | 609 MiB/s | 5 891 MiB/s |
| French and CJK, 1 MiB | 341 MiB/s | 5 949 MiB/s |
| Random bytes, 64 bytes, refused | 4.6 ns | 4.0 ns |
7. How to measure on the machine
A measure is a line that a process of the system writes on the log. Each command runs the main machine test; each line is in build/test_output.txt. A measure is valid when uptime shows a load under 8. No test fails on a time: a measure is printed, never asserted (Developer tools: The bounds of the tests).
flock -o <the lock of the worktree> nix develop -c modustool test main
grep "vector_glyphs: one glyph" build/test_output.txt
grep "compositor: the blend of the arrow" build/test_output.txt
grep "root: the resource font_outlines" build/test_output.txt
| Line | The number | The scalar form |
|---|---|---|
vector_glyphs: one glyph ... | after "from the private glyph cache:": one glyph in ns, the least of 50 batches; after "Spleen 8x16:": one glyph of Spleen | 309 ns from the glyph cache of the boot image before it left, Spleen 35 ns (The text of the system: a vector font with subpixel bands: The glyph cache) |
compositor: the blend of the arrow of 32x32 ... | the blend of the arrow in ns, and a copy of the same size | 824 to 1008 ns, a copy 151 to 153 ns |
On the host, with the optimisation of a module. Each test prints the time of each form, and fails only when the forms give different bytes:
nix develop -c odin test src/library/modus/graphics -o:speed -vet -collection:modus=src/library/modus -define:ODIN_TEST_NAMES=graphics.glyph_draw_cost_of_each_form
nix develop -c odin test src/library/modus/contract/blake3 -o:speed -vet -collection:modus=src/library/modus -define:ODIN_TEST_NAMES=blake3.hash_cost_of_each_form
nix develop -c odin test src/library/modus/line_editor -o:speed -microarch:x86-64 -vet -collection:modus=src/library/modus -define:MEASURE=true -define:ODIN_TEST_NAMES=line_editor.fuzzy_match_speed_of_each_form
graphics.text_speed_of_each_blend gives the time of one glyph with the blend table, with the SSE2 form alone and with the scalar form alone, in the same way.
8. Limits
- The row of colors of a gradient across or at an angle (a division and a mix for each pixel) and the outline renderer (
src/library/modus/vector_font, at build time) stay scalar. - The object code of the SSE2 forms is not read yet: the measures say if the compiler lowered the 16-bit products to
pmullwandpmulhuw. - The cryptography of the system has no vector form: it keeps the portable implementations of its origin (Cryptography).
- No vector form uses a usable instruction set above SSE2. The package
src/library/modus/instruction_setshas two users, the test modulesextended_stateandreturn_fault.
9. The 16-tag group match
What it is. A Swiss table (src/library/modus/hash_table; intent in Swiss tables and the fast hash in the repository) groups 16 slots together with 16 consecutive 1-byte control tags (Empty = 0x00, Deleted = 0x01, or full 0x80 | (hash & 0x7F)). Each probe step tests all 16 control bytes of a group at once and returns a 16-bit mask where bit i marks lane i.
The SSE2 form (src/library/modus/hash_table/group_match_amd64.odin) loads 16 control bytes as #simd[16]u8 and uses base:intrinsics only:
| Operation | Intrinsics | Lowered SSE2 instructions |
|---|---|---|
group_match_tag_sse2(controls, tag) | unaligned_load, simd_lanes_eq, simd_extract_msbs | movdqu, pcmpeqb, pmovmskb |
group_match_empty_sse2(controls) | unaligned_load, simd_lanes_eq against 0x00, simd_extract_msbs | movdqu, pxor, pcmpeqb, pmovmskb |
group_match_free_sse2(controls) | unaligned_load, ~simd_extract_msbs (bit 7 clear) | movdqu, pmovmskb, not |
The public procedures (group_match_tag, group_match_empty, group_match_free in group_match.odin) pick the SSE2 form on amd64 and the scalar loops on other targets. The host test group_forms_give_the_mask_of_the_scalar_form compares all three SSE2 procedures against their scalar forms for every byte value 0..255 in each of the 16 lanes and for uniform edge groups (0x00, 0x01, 0x7F, 0x80, 0xFF).
How to measure on the host. Both timing tests are opt-in (-define:MEASURE=true) and require -o:speed -microarch:x86-64:
nix develop -c odin test src/library/modus/hash_table -o:speed -microarch:x86-64 -vet -collection:modus=src/library/modus -define:MEASURE=true -define:ODIN_TEST_NAMES=hash_table.lookup_cost_against_the_chained_table
nix develop -c odin test src/library/modus/fast_hash -o:speed -microarch:x86-64 -vet -collection:modus=src/library/modus -define:MEASURE=true -define:ODIN_TEST_NAMES=fast_hash.hash_cost_of_short_keys
Measured on the host (-o:speed -microarch:x86-64, host load 3.71, least of 15 rounds for hash_table, least of 20 batches of 100 000 for fast_hash):
| Table size (entries) | Random hit | Random miss | Glyph hit | Glyph miss |
|---|---|---|---|---|
| 16 | 1.46 ns (687 million/s) | 1.29 ns (776 million/s) | 1.36 ns (736 million/s) | 1.17 ns (855 million/s) |
| 1 000 | 2.21 ns (452 million/s) | 1.82 ns (549 million/s) | 1.91 ns (524 million/s) | 2.10 ns (476 million/s) |
| 4 096 | 2.06 ns (485 million/s) | 1.70 ns (589 million/s) | 2.07 ns (482 million/s) | 1.28 ns (780 million/s) |
| 100 000 | 4.11 ns (243 million/s) | 5.88 ns (170 million/s) | 3.43 ns (292 million/s) | 1.89 ns (530 million/s) |
Key length (fast_hash, rapidhash V3) | Time for one hash | Rate |
|---|---|---|
| 8 bytes | 2.68 ns | 373 million/s |
| 16 bytes | 2.68 ns | 373 million/s |
| 32 bytes | 3.71 ns | 270 million/s |
| 64 bytes | 6.19 ns | 162 million/s |
10. The fuzzy match
Where it runs. The console completion and the ranking thread of the palette match each candidate name against the typed query (fuzzy_match, rank_candidates). The scoring rule is in The line of the console: The fuzzy match, and the design is in The fuzzy match and the frecency in the repository.
The portable vector form is fuzzy_match_vectors in src/library/modus/line_editor/fuzzy_match_vectors.odin. It uses #simd[16]u8 vectors and base:intrinsics only, so it compiles on every architecture without a target guard: SSE2 on x86-64, NEON on ARM64.
- Blocks of 16 bytes. The name is read in at most 8 blocks (
NAME_SCAN_LIMIT = 128). A partial last block is copied into zeros so no load reads past the slice of the caller. - ASCII check. All blocks are OR-ed and
simd_extract_msbschecks bit 7. Any name or query with a byte past ASCII falls back tofuzzy_match_scalar. - Lowering in lanes.
AtoZbecomeatoz, and control bytes (< 0x20or0x7F) become0x80(CONTROL_MARK), matching how the scalar form decodes them as the replacement character. - Bitmask prefilter and two passes. For each query character, comparing against each lowered block and extracting MSBs yields one
u128mask (clipped to the length of the name). A zero mask above the previous position stops the match at once. The forward pass takescount_trailing_zerosto find the earliest end; the backward pass takescount_leading_zerosto find the latest start of the shortest window.
Verification. vector_form_at_the_edges tests empty inputs, block borders (15, 16, 17, 31, 32, 33 bytes), scan and query bounds (64/65 and 127/128/129 bytes), zero bytes against padding, control bytes, and non-ASCII UTF-8. vector_form_gives_the_result_of_the_scalar_form compares fuzzy_match_vectors against fuzzy_match_scalar on 200 000 deterministic cases (seed 0x9E3779B97F4A7C15), checking exact equality of matched, score, and places.
Host measure (-o:speed -microarch:x86-64 -define:MEASURE=true, host load 6.90, four queries over realistic exports, module names, and file paths):
| Candidates | Query (matches) | Scalar (M names/s) | Vector (M names/s) | Speedup |
|---|---|---|---|---|
| 100 | mcnt (5) | 8.4 | 60.6 | 7.2x |
| 100 | stwr (9) | 8.7 | 51.9 | 6.0x |
| 100 | srclinedit (0) | 7.6 | 52.1 | 6.8x |
| 100 | zqj (0) | 8.0 | 53.0 | 6.7x |
| 10 000 | mcnt (1130) | 7.4 | 35.7 | 4.8x |
| 10 000 | stwr (472) | 7.0 | 36.5 | 5.2x |
| 10 000 | srclinedit (59) | 5.8 | 32.4 | 5.6x |
| 10 000 | zqj (0) | 8.1 | 66.2 | 8.2x |
| 100 000 | mcnt (11 419) | 8.0 | 37.7 | 4.7x |
| 100 000 | stwr (4894) | 7.4 | 35.7 | 4.9x |
| 100 000 | srclinedit (606) | 6.8 | 34.3 | 5.1x |
| 100 000 | zqj (0) | 8.7 | 64.9 | 7.5x |
11. The triangles of the WebGPU subset
Where it runs. software_3d_execute draws the triangles of a command stream of the WebGPU subset on the processor, in 2D (Solid_Color_2D) and in 3D (Vertex_Color_3D) (The access to a GPU for acceleration: The WebGPU of the system in the repository). The design of the step is in From the desktop of today to a light web browser: SIMD on the hot paths in the repository.
The first form of floats. Each other vector form computes with integers. The rasterizer computes with f32, and it keeps the rule of the same bytes in this way:
- Each lane does the operations of the scalar form, in the same order: the edge functions, the barycentric weights, the depth,
1 / wand the four channels corrected for perspective. An addition and a multiplication of IEEE 754 give the same bits in a lane as in a scalar register. The compiler fuses no multiplication and addition without a flag that allows it, and the build gives none: the host tests pass in a build for a processor with FMA (-microarch:native). - The product of an edge function that depends on the row only is computed once for each row, as a scalar. This product is the same value that the scalar form computes at each pixel.
- The test of the top-left rule is written as the refusal of the scalar form (
w < 0, orw == 0on an edge that is not top or left), not as its negation. A NaN fails both comparisons, so both forms keep such a pixel. - A lane that is refused divides 1 by 1, so no lane divides by zero. The form never reads the value of a refused lane.
| Part | Form |
|---|---|
| The setup of a triangle: the order of the vertices, the area, the box clipped to the scissor rectangle, the top-left edges | scalar, once for each triangle, shared by the two forms (triangle_setup) |
| The three edge values of 4 adjacent pixels | vector, #simd[4]f32 |
| The coverage test with the top-left rule | vector: comparisons and masks, no branch |
| A group of 4 pixels outside the triangle | one test (simd_reduce_and), before the interpolation |
The barycentric weights, the depth, 1 / w and the color and alpha corrected for perspective | vector |
| The depth test, the 24-bit depth and the blend in linear light | scalar, write_fragment_pixel, one pixel at a time in lane order |
| The last 0 to 3 columns of a row | the scalar form, shade_triangle_pixel |
The vector form writes each pixel through write_fragment_pixel, as the scalar form does. So no load and no store passes the slice of the caller, and the blend of a pixel reads the result of the pixel before it, in the same order as the scalar form.
The host tests (src/library/modus/contract/webgpu_subset/rasterizer_forms_test.odin) run each stream in each form through software_3d_execute_in_form, which is private to the package. They compare each color byte, each depth value and the summary (pixels_shaded, triangles_rasterized). The textures start with the same random content, so a pass with Load_Op.Load blends and tests depth over it. Each test runs the widths 1, 3, 5, 17, 63, 64 and 65 and the formats RGBA8_Unorm, BGRA8_Unorm and R8_Unorm:
cube_forms_give_the_same_bytes: a cube of 12 triangles, turned and seen in perspective, with its 24-bit depth buffer, at 8 angles, at heights of 1 and 37, withClearandLoad.blended_triangle_forms_give_the_same_bytes: 6 translucent 3D triangles that overlap,Src_AlphaandOne_Minus_Src_Alpha, with alphas from below 0 to above 1, vertices behind the eye, with and without a depth buffer.shared_edge_forms_shade_each_pixel_once: a fan whose edges pass through pixel centers, a square cut on its diagonal, and a grid of moved points that covers the whole picture. The test checks that the count of shaded pixels equals the count of pixels and that no pixel keeps the clear color: no crack and no pixel shaded twice.edge_case_forms_give_the_same_bytes: random triangles from far off the screen, scissor rectangles, a repeated vertex, three points on a line, an area at the bound of a degenerate triangle, tiny triangles, places of 10^7 pixels, vertices on pixel centers and corners, and triangle strips.
A test that fails gives the first wrong byte or depth value and the value of each form. A change of the order of one sum of the vector form, or a top-left rule that is not applied, makes these tests fail.
The measure on the host. The test is opt-in: it runs only with -define:MEASURE=true. It prints the time of a frame of a cube that fills most of a picture of 640 x 480 in each form, and fails only when the forms shade a different count of pixels:
nix develop -c odin test src/library/modus/contract/webgpu_subset -o:speed -microarch:x86-64 -vet -collection:modus=src/library/modus -define:MEASURE=true -define:ODIN_TEST_NAMES=webgpu_subset.cube_speed_of_each_form
The last run had a host load of 13, above the limit of 8. So these times rank the forms and are not valid measures of speed. One frame of 197 328 pixels, the least of 5 runs of 60 frames:
| Format | Scalar form | Vector form |
|---|---|---|
RGBA8_Unorm | 17.9 ms | 16.8 ms |
BGRA8_Unorm | 22.7 ms | 14.2 ms |
R8_Unorm | 16.4 ms | 12.7 ms |
Most of the time of a pixel is in write_fragment_pixel: the square root of the encoding of each channel takes four divisions. That part is still scalar.
