aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/libc/clz.s
blob: 1ccff2e100bab09f519e07083122990014491a79 (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
# PSn00bSDK leading zero count intrinsics
# (C) 2022-2023 spicyjpeg - MPL licensed
#
# libgcc provides two functions used internally by GCC to count the number of
# leading zeroes in a value, __clzsi2() (32-bit) and __clzdi2() (64-bit). This
# file overrides them with smaller implementations that make use of the GTE's
# LZCS/LZCR registers.

.set noreorder

.set LZCS, $30
.set LZCR, $31

.section .text.__clzsi2, "ax", @progbits
.global __clzsi2
.type __clzsi2, @function

__clzsi2:
	mtc2  $a0, LZCS
	bltz  $a0, .Lreturn # if (value & (1 << 31)) return 0
	li    $v0, 0
	mfc2  $v0, LZCR # else return GTE_CLZ(value)

.Lreturn:
	jr    $ra
	nop

.section .text.__clzdi2, "ax", @progbits
.global __clzdi2
.type __clzdi2, @function

__clzdi2:
	mtc2  $a1, LZCS
	bltz  $a1, .Lreturn2 # if (msb & (1 << 31)) return 0
	li    $v0, 0
	bnez  $a1, .LreturnMSB # else if (msb) return GTE_CLZ(msb)
	nop

.LnoMSB:
	mtc2  $a0, LZCS
	bltz  $a0, .Lreturn2 # else if (lsb & (1 << 31)) return 32
	li    $v0, 32
	mfc2  $v0, LZCR # else return 32 + GTE_CLZ(lsb)

	jr    $ra
	addiu $v0, 32

.LreturnMSB:
	mfc2  $v0, LZCR

.Lreturn2:
	jr    $ra
	nop