Skip to content

Instantly share code, notes, and snippets.

@DocBohn
Last active May 12, 2025 21:32
Show Gist options
  • Select an option

  • Save DocBohn/e165bb0745877656be2e5e2ab52b22af to your computer and use it in GitHub Desktop.

Select an option

Save DocBohn/e165bb0745877656be2e5e2ab52b22af to your computer and use it in GitHub Desktop.
Code demonstrating the use of the `restrict` keyword
#include <string.h>
void simple_string_copy_without_restrict(char *destination, char const *source) {
for (unsigned int i = 0; i <= strlen(source); i++) {
destination[i] = source[i];
}
}
void simple_string_copy_with_restrict(char *restrict destination, char const *restrict source) {
for (unsigned int i = 0; i <= strlen(source); i++) {
destination[i] = source[i];
}
}
simple_string_copy_without_restrict:
stp x29, x30, [sp, -48]!
mov x29, sp
stp x19, x20, [sp, 16]
stp x21, x22, [sp, 32]
mov x22, x0
mov x21, x1
mov w19, 0
b .L2
.L3:
ldrb w1, [x21, w19, uxtw]
strb w1, [x22, w19, uxtw]
add w19, w19, 1
.L2:
uxtw x20, w19
mov x0, x21
bl strlen
cmp x20, x0
bls .L3
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x29, x30, [sp], 48
ret
simple_string_copy_with_restrict:
stp x29, x30, [sp, -48]!
mov x29, sp
stp x19, x20, [sp, 16]
stp x21, x22, [sp, 32]
mov x22, x0
mov x21, x1
mov w19, 0
b .L6
.L7:
ldrb w1, [x21, w19, uxtw]
strb w1, [x22, w19, uxtw]
add w19, w19, 1
.L6:
uxtw x20, w19
mov x0, x21
bl strlen
cmp x20, x0
bls .L7
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x29, x30, [sp], 48
ret
simple_string_copy_without_restrict:
pushq %r13
pushq %r12
pushq %rbp
pushq %rbx
subq $8, %rsp
movq %rdi, %r13
movq %rsi, %r12
movl $0, %ebp
jmp .L2
.L3:
movzbl (%r12,%rbx), %eax
movb %al, 0(%r13,%rbx)
addl $1, %ebp
.L2:
movl %ebp, %ebx
movq %r12, %rdi
call strlen
cmpq %rbx, %rax
jnb .L3
addq $8, %rsp
popq %rbx
popq %rbp
popq %r12
popq %r13
ret
simple_string_copy_with_restrict:
pushq %r13
pushq %r12
pushq %rbp
pushq %rbx
subq $8, %rsp
movq %rdi, %r13
movq %rsi, %r12
movl $0, %ebp
jmp .L6
.L7:
movzbl (%r12,%rbx), %eax
movb %al, 0(%r13,%rbx)
addl $1, %ebp
.L6:
movl %ebp, %ebx
movq %r12, %rdi
call strlen
cmpq %rbx, %rax
jnb .L7
addq $8, %rsp
popq %rbx
popq %rbp
popq %r12
popq %r13
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment