점수 : 161558 227038 259038 260038 263068
내가 찾을 수있는 가장 빠른 정수 제곱근 알고리즘을 취하고 나머지가 0인지 여부를 반환합니다.
# based on http://www.cc.utah.edu/~nahaj/factoring/isqrt.c.html
# converted to GOLF assembly for http://codegolf.stackexchange.com/questions/49356/testing-if-a-number-is-a-square
# unrolled for speed, original source commented out at bottom
start:
or u, t, 1 << 62
shr t, t, 1
gequ v, n, u
jz nope62, v
sub n, n, u
or t, t, 1 << 62
nope62:
or u, t, 1 << 60
shr t, t, 1
gequ v, n, u
jz nope60, v
sub n, n, u
or t, t, 1 << 60
nope60:
or u, t, 1 << 58
shr t, t, 1
gequ v, n, u
jz nope58, v
sub n, n, u
or t, t, 1 << 58
nope58:
or u, t, 1 << 56
shr t, t, 1
gequ v, n, u
jz nope56, v
sub n, n, u
or t, t, 1 << 56
nope56:
or u, t, 1 << 54
shr t, t, 1
gequ v, n, u
jz nope54, v
sub n, n, u
or t, t, 1 << 54
nope54:
or u, t, 1 << 52
shr t, t, 1
gequ v, n, u
jz nope52, v
sub n, n, u
or t, t, 1 << 52
nope52:
or u, t, 1 << 50
shr t, t, 1
gequ v, n, u
jz nope50, v
sub n, n, u
or t, t, 1 << 50
nope50:
or u, t, 1 << 48
shr t, t, 1
gequ v, n, u
jz nope48, v
sub n, n, u
or t, t, 1 << 48
nope48:
or u, t, 1 << 46
shr t, t, 1
gequ v, n, u
jz nope46, v
sub n, n, u
or t, t, 1 << 46
nope46:
or u, t, 1 << 44
shr t, t, 1
gequ v, n, u
jz nope44, v
sub n, n, u
or t, t, 1 << 44
nope44:
or u, t, 1 << 42
shr t, t, 1
gequ v, n, u
jz nope42, v
sub n, n, u
or t, t, 1 << 42
nope42:
or u, t, 1 << 40
shr t, t, 1
gequ v, n, u
jz nope40, v
sub n, n, u
or t, t, 1 << 40
nope40:
or u, t, 1 << 38
shr t, t, 1
gequ v, n, u
jz nope38, v
sub n, n, u
or t, t, 1 << 38
nope38:
or u, t, 1 << 36
shr t, t, 1
gequ v, n, u
jz nope36, v
sub n, n, u
or t, t, 1 << 36
nope36:
or u, t, 1 << 34
shr t, t, 1
gequ v, n, u
jz nope34, v
sub n, n, u
or t, t, 1 << 34
nope34:
or u, t, 1 << 32
shr t, t, 1
gequ v, n, u
jz nope32, v
sub n, n, u
or t, t, 1 << 32
nope32:
or u, t, 1 << 30
shr t, t, 1
gequ v, n, u
jz nope30, v
sub n, n, u
or t, t, 1 << 30
nope30:
or u, t, 1 << 28
shr t, t, 1
gequ v, n, u
jz nope28, v
sub n, n, u
or t, t, 1 << 28
nope28:
or u, t, 1 << 26
shr t, t, 1
gequ v, n, u
jz nope26, v
sub n, n, u
or t, t, 1 << 26
nope26:
or u, t, 1 << 24
shr t, t, 1
gequ v, n, u
jz nope24, v
sub n, n, u
or t, t, 1 << 24
nope24:
or u, t, 1 << 22
shr t, t, 1
gequ v, n, u
jz nope22, v
sub n, n, u
or t, t, 1 << 22
nope22:
or u, t, 1 << 20
shr t, t, 1
gequ v, n, u
jz nope20, v
sub n, n, u
or t, t, 1 << 20
nope20:
or u, t, 1 << 18
shr t, t, 1
gequ v, n, u
jz nope18, v
sub n, n, u
or t, t, 1 << 18
nope18:
or u, t, 1 << 16
shr t, t, 1
gequ v, n, u
jz nope16, v
sub n, n, u
or t, t, 1 << 16
nope16:
or u, t, 1 << 14
shr t, t, 1
gequ v, n, u
jz nope14, v
sub n, n, u
or t, t, 1 << 14
nope14:
or u, t, 1 << 12
shr t, t, 1
gequ v, n, u
jz nope12, v
sub n, n, u
or t, t, 1 << 12
nope12:
or u, t, 1 << 10
shr t, t, 1
gequ v, n, u
jz nope10, v
sub n, n, u
or t, t, 1 << 10
nope10:
or u, t, 1 << 8
shr t, t, 1
gequ v, n, u
jz nope8, v
sub n, n, u
or t, t, 1 << 8
nope8:
or u, t, 1 << 6
shr t, t, 1
gequ v, n, u
jz nope6, v
sub n, n, u
or t, t, 1 << 6
nope6:
or u, t, 1 << 4
shr t, t, 1
gequ v, n, u
jz nope4, v
sub n, n, u
or t, t, 1 << 4
nope4:
or u, t, 1 << 2
shr t, t, 1
gequ v, n, u
jz nope2, v
sub n, n, u
or t, t, 1 << 2
nope2:
or u, t, 1 << 0
shr t, t, 1
gequ v, n, u
jz nope0, v
sub n, n, u
nope0:
end:
not s, n # return !remainder
halt 0
# before unrolling...
#
# start:
# mov b, 1 << 62 # squaredbit = 01000000...
# loop: # do {
# or u, b, t # u = squaredbit | root
# shr t, t, 1 # root >>= 1
# gequ v, n, u # if remainder >= u:
# jz nope, v
# sub n, n, u # remainder = remainder - u
# or t, t, b # root = root | squaredbit
# nope:
# shr b, b, 2 # squaredbit >>= 2
# jnz loop, b # } while (squaredbit > 0)
# end:
# not s, n # return !remainder
# halt 0
편집 1 : 제곱 테스트를 제거하고! remainder를 직접 반환하고 테스트 당 3 ops를 저장하십시오.
편집 2 : 나머지를 n으로 직접 사용하고 테스트 당 1 op를 저장하십시오.
편집 3 : 루프 조건을 단순화하고 테스트 당 32 ops 저장
편집 4 : 루프를 풀고 테스트 당 약 65 ops를 절약하십시오.