Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.5.3 for 循环 的算法有问题 #577

Open
feiquan123 opened this issue Jun 1, 2022 · 2 comments
Open

3.5.3 for 循环 的算法有问题 #577

feiquan123 opened this issue Jun 1, 2022 · 2 comments
Assignees
Labels

Comments

@feiquan123
Copy link

func LoopAdd(cnt, v0, step int) int {
	result := v0
	for i := 0; i < cnt; i++ {
		result += step
	}
	return result
}

比如 1+2+...+100 等差数列可以这样计算 LoopAdd(100, 1, 1),而 10+8+...+0 等差数列则可以这样计算 LoopAdd(5, 10, -2)。

问题描述

按这个算法
LoopAdd(100, 1, 1) = 101 期望:5050
LoopAdd(5, 10, -2) = 0 期望:30

@feiquan123 feiquan123 added the bug label Jun 1, 2022
@feiquan123
Copy link
Author

修改后:

func LoopAdd(cnt, v0, step int) int {
	result := v0
	next := v0
	for i := 1; i < cnt; i++ {
		next += step
		result += next
	}
	return result
}

汇编:

TEXT ·LoopAdd(SB),$0-32
	MOVQ cnt+8*0(FP), AX // cnt
	MOVQ v0+8*1(FP), BX // v0 /result
	MOVQ step+8*2(FP), CX // step

	MOVQ BX, R8 // next
	MOVQ $1, DX // i

LOOP_IF:
	CMPQ DX, AX // compare i, cnt
	JL LOOP_BODY // if i < cnt; goto LOOP_BODY
	JMP LOOP_END

LOOP_BODY:
	ADDQ CX, R8 // next += step
	ADDQ R8, BX // result += next
	ADDQ $1, DX // i++
	JMP LOOP_IF

LOOP_END:
	MOVQ BX, ret+8*3(FP) // return result
	RET

@chai2010
Copy link
Owner

发现 BUG 直接上 PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants