File: p.go

package info (click to toggle)
golang-1.19 1.19.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, bookworm-proposed-updates, experimental
  • size: 146,332 kB
  • sloc: asm: 117,020; ansic: 7,010; sh: 2,114; javascript: 1,119; perl: 1,099; python: 365; makefile: 110; cpp: 39; f90: 8; awk: 7; objc: 4
file content (52 lines) | stat: -rw-r--r-- 826 bytes parent folder | download | duplicates (22)
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
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package p

import (
	"fmt"
	"runtime"
)

var y int

//go:noinline
func Square(x int) {
	var pc0, pc1 [1]uintptr
	runtime.Callers(1, pc0[:]) // get PC at entry

	// a switch using jump table
	switch x {
	case 1:
		y = 1
	case 2:
		y = 4
	case 3:
		y = 9
	case 4:
		y = 16
	case 5:
		y = 25
	case 6:
		y = 36
	case 7:
		y = 49
	case 8:
		y = 64
	default:
		panic("too large")
	}

	// check PC is in the same function
	runtime.Callers(1, pc1[:])
	if pc1[0] < pc0[0] || pc1[0] > pc0[0]+1000000 {
		fmt.Printf("jump across DSO boundary. pc0=%x, pc1=%x\n", pc0[0], pc1[0])
		panic("FAIL")
	}

	if y != x*x {
		fmt.Printf("x=%d y=%d!=%d\n", x, y, x*x)
		panic("FAIL")
	}
}