Smallest UTF32 to UTF8 converter
by
Bastian Blank
—
last modified
Sep 13, 2008 12:30 PM
I found some weird opcodes in the s390 instruction set some time ago. I finally want to use it as I have access to such a machine. The result is rather slim:
.globl cu41
.type cu41, @function
cu41:
.L2:
cu41 %r2,%r4
ipm %r1
srl %r1,28
chi %r1,3
je .L2
ltr %r1,%r1
je .L3
lcr %r1,%r1
lgfr %r3,%r1
.L3:
lgr %r2,%r3
br %r14
The cu41 opcode translates UTF32 to UTF8. Each "parameter" is a register pair [1], which describes address and length of the buffer. This opcode will return after a not specified number of translated characters. The rest is boiler plate code for condition code checking. The loop makes sure that anything is converted within the function unless another error occured.
| [1] | An even pair. gcc is not yet able to allocate such pairs on its own, so it is hardcoded. |
