github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/loopvar/testdata/for_complicated_esc_address.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  )
    11  
    12  func main() {
    13  	ss, sa := shared(23)
    14  	ps, pa := private(23)
    15  	es, ea := experiment(23)
    16  
    17  	fmt.Printf("shared s, a; private, s, a; experiment s, a = %d, %d;  %d, %d;  %d, %d\n", ss, sa, ps, pa, es, ea)
    18  
    19  	if ss != ps || ss != es || ea != pa || sa == pa {
    20  		os.Exit(11)
    21  	} else {
    22  		fmt.Println("PASS")
    23  	}
    24  }
    25  
    26  func experiment(x int) (int, int) {
    27  	sum := 0
    28  	var is []*int
    29  	for i := x; i != 1; i = i / 2 {
    30  		for j := 0; j < 10; j++ {
    31  			if i == j { // 10 skips
    32  				continue
    33  			}
    34  			sum++
    35  		}
    36  		i = i*3 + 1
    37  		if i&1 == 0 {
    38  			is = append(is, &i)
    39  			for i&2 == 0 {
    40  				i = i >> 1
    41  			}
    42  		} else {
    43  			i = i + i
    44  		}
    45  	}
    46  
    47  	asum := 0
    48  	for _, pi := range is {
    49  		asum += *pi
    50  	}
    51  
    52  	return sum, asum
    53  }
    54  
    55  func private(x int) (int, int) {
    56  	sum := 0
    57  	var is []*int
    58  	I := x
    59  	for ; I != 1; I = I / 2 {
    60  		i := I
    61  		for j := 0; j < 10; j++ {
    62  			if i == j { // 10 skips
    63  				I = i
    64  				continue
    65  			}
    66  			sum++
    67  		}
    68  		i = i*3 + 1
    69  		if i&1 == 0 {
    70  			is = append(is, &i)
    71  			for i&2 == 0 {
    72  				i = i >> 1
    73  			}
    74  		} else {
    75  			i = i + i
    76  		}
    77  		I = i
    78  	}
    79  
    80  	asum := 0
    81  	for _, pi := range is {
    82  		asum += *pi
    83  	}
    84  
    85  	return sum, asum
    86  }
    87  
    88  func shared(x int) (int, int) {
    89  	sum := 0
    90  	var is []*int
    91  	i := x
    92  	for ; i != 1; i = i / 2 {
    93  		for j := 0; j < 10; j++ {
    94  			if i == j { // 10 skips
    95  				continue
    96  			}
    97  			sum++
    98  		}
    99  		i = i*3 + 1
   100  		if i&1 == 0 {
   101  			is = append(is, &i)
   102  			for i&2 == 0 {
   103  				i = i >> 1
   104  			}
   105  		} else {
   106  			i = i + i
   107  		}
   108  	}
   109  
   110  	asum := 0
   111  	for _, pi := range is {
   112  		asum += *pi
   113  	}
   114  	return sum, asum
   115  }