Source file src/simd/testdata/timport/main2/main.go

     1  // Copyright 2026 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  // main2 validates cross-package SIMD package-variable handling in a package
     6  // that intentionally does not directly import "simd":
     7  //   - Package variables whose imported types (from timport/p) depend on SIMD
     8  //     solely through their method sets (crossStruct, crossIface).
     9  //   - Single-variable storage and address identity for an exported non-SIMD
    10  //     package variable (p.N) initialized via SIMD in timport/p.
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"os"
    16  	"timport/p"
    17  )
    18  
    19  // Package-level variables in a non-"simd"-importing package whose inferred
    20  // types from timport/p are SIMD-dependent solely through their method sets;
    21  // midway must recognize their dependence, inject @simd/@bridge, and specialize
    22  // them per size.
    23  var (
    24  	crossStruct = p.MakeHasSimdMethod()
    25  	crossIface  = p.MakeSimdMethodIface()
    26  )
    27  
    28  // checkCrossPkgMethodDepVars validates that package variables initialized from
    29  // imported method-set-SIMD-dependent types are properly initialized.
    30  func checkCrossPkgMethodDepVars() {
    31  	if crossStruct.X != 42 || crossIface == nil {
    32  		fmt.Printf("FAIL, unexpected cross-pkg values: X=%d iface=%v\n", crossStruct.X, crossIface)
    33  		os.Exit(1)
    34  	}
    35  }
    36  
    37  // checkSingleVarIdentity validates that p.N (type int, initialized from
    38  // simd.Int8s{}.Len() via init proxies) stays a single variable with a single
    39  // address (p.PN == &p.N) shared between timport/p and external callers.
    40  func checkSingleVarIdentity() {
    41  	fmt.Println("init:", p.N, p.GetN(), *p.PN)
    42  	if p.N < 16 || p.N&(p.N-1) != 0 {
    43  		fmt.Printf("FAIL, unexpected initial p.N = %d\n", p.N)
    44  		os.Exit(1)
    45  	}
    46  	if p.GetN() != p.N || *p.PN != p.N {
    47  		fmt.Printf("FAIL, init mismatch: p.N=%d p.GetN()=%d *p.PN=%d\n", p.N, p.GetN(), *p.PN)
    48  		os.Exit(1)
    49  	}
    50  	p.N = 1000
    51  	fmt.Println("after p.N=1000: p.N =", p.N, "p.GetN() =", p.GetN())
    52  	if p.N != 1000 || p.GetN() != 1000 {
    53  		fmt.Printf("FAIL, after p.N=1000: p.N=%d p.GetN()=%d\n", p.N, p.GetN())
    54  		os.Exit(1)
    55  	}
    56  	p.SetN(7)
    57  	fmt.Println("after p.SetN(7): p.N =", p.N, "p.GetN() =", p.GetN())
    58  	if p.N != 7 || p.GetN() != 7 {
    59  		fmt.Printf("FAIL, after p.SetN(7): p.N=%d p.GetN()=%d\n", p.N, p.GetN())
    60  		os.Exit(1)
    61  	}
    62  	fmt.Println("p.PN == &p.N:", p.PN == &p.N)
    63  	if p.PN != &p.N {
    64  		fmt.Println("FAIL, pointers are not equal")
    65  		os.Exit(1)
    66  	}
    67  }
    68  
    69  func main() {
    70  	checkCrossPkgMethodDepVars()
    71  	checkSingleVarIdentity()
    72  }
    73  

View as plain text