summaryrefslogtreecommitdiff
path: root/zap/src/mem/utf8enclen.S
blob: 1c80e6a4f413ea9112bf8e754f89d04d10ae0670 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
	Copyright 2022 Gabriel Jensen.
	This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
	If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#include <zap/priv.h>

.globl zap_utf8enclen

zap_utf8enclen:

	/*
		zap_chr20 const * utf20
	*/

#if defined(__amd64__)

	# rdi: Address of the current character.
	# rax: Length of the string.
	# rsi: Current character.

	movq $0x0,%rax

	# Iterate over the input:
.loop:

	movl (%rdi),%esi

	# Test if we have reached the null-terminator:
	testl %esi,%esi
	jz .done

	cmpl $0xFFFF,%esi
	jg .oct4

	cmpl $0x7FF,%esi
	jg .oct3

	cmpl $0x7F,%esi
	jg .oct2

	# One octet:
.oct1:

	incq %rax

	jmp .cnt

	# Two octets:
.oct2:

	addq $0x2,%rax

	jmp .cnt

	# Three octets:
.oct3:

	addq $0x3,%rax

	jmp .cnt

	# Four octets:
.oct4:

	addq $0x4,%rax

	# Continue to the next codepoint:
.cnt:

	addq $0x4,%rdi
	jmp .loop

	# Done:
.done:

	ret

#endif