#3791·ebiten

internal/shader: make out-of-range shifts and integer division by zero consistent across backends

Author: hajimehoshiCreated Sep 26, 2026Updated Sep 26, 2026
Labelsbugos:linuxos:windowsos:macosos:androidos:iosos:freebsdos:js

Ebitengine Version

main at eebf01ce1

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • OpenBSD
  • Android
  • iOS
  • Nintendo Switch
  • PlayStation 5
  • Xbox
  • Web Browsers

Go Version (paste your go version output)

go version go1.27.0 darwin/arm64

What steps will reproduce the problem?

Run the program below on an Apple Silicon Mac twice: once with EBITENGINE_GRAPHICS_LIBRARY=metal and once with EBITENGINE_GRAPHICS_LIBRARY=opengl. The shader computes integer divisions, remainders and shifts by the uniform values Y = 0, S = -1 and L = 40. It writes each 32-bit result into one pixel with BlendCopy, and the program reads the pixels back.

Program
go
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/hajimehoshi/ebiten/v2"
)

const src = `//kage:unit pixels

package main

var Y int
var S int
var L int

func Fragment(dstPos vec4, src0Pos vec2, color vec4) vec4 {
	i := int(dstPos.x)
	x := 7
	n := -7
	v := 0
	if i == 0 {
		v = x / Y
	} else if i == 1 {
		v = n / Y
	} else if i == 2 {
		v = x % Y
	} else if i == 3 {
		v = n % Y
	} else if i == 4 {
		v = x << S
	} else if i == 5 {
		v = x >> S
	} else if i == 6 {
		v = n >> S
	} else if i == 7 {
		v = x << L
	} else if i == 8 {
		v = x >> L
	} else if i == 9 {
		v = n >> L
	}
	return vec4(float(v&255), float((v>>8)&255), float((v>>16)&255), float((v>>24)&255)) / 255
}
`

var names = []string{"7 / 0", "-7 / 0", "7 % 0", "-7 % 0", "7 << -1", "7 >> -1", "-7 >> -1", "7 << 40", "7 >> 40", "-7 >> 40"}

type game struct{}

func (g *game) Update() error {
	var info ebiten.DebugInfo
	ebiten.ReadDebugInfo(&info)
	fmt.Println("graphics library:", info.GraphicsLibrary)
	s, err := ebiten.NewShader([]byte(src))
	if err != nil {
		fmt.Println("compile error:", err)
		return ebiten.Termination
	}
	img := ebiten.NewImage(len(names), 1)
	op := &ebiten.DrawRectShaderOptions{}
	op.Blend = ebiten.BlendCopy
	op.Uniforms = map[string]any{"Y": 0, "S": -1, "L": 40}
	img.DrawRectShader(len(names), 1, s, op)
	buf := make([]byte, 4*len(names))
	img.ReadPixels(buf)
	for i, name := range names {
		v := int32(uint32(buf[4*i]) | uint32(buf[4*i+1])<<8 | uint32(buf[4*i+2])<<16 | uint32(buf[4*i+3])<<24)
		fmt.Printf("  %-9s = %d\n", name, v)
	}
	return ebiten.Termination
}

func (g *game) Draw(*ebiten.Image) {}

func (g *game) Layout(int, int) (int, int) {
	return 16, 16
}

func main() {
	go func() {
		time.Sleep(30 * time.Second)
		fmt.Println("timeout")
		os.Exit(3)
	}()
	ebiten.SetWindowVisible(false)
	if err := ebiten.RunGame(&game{}); err != nil {
		panic(err)
	}
}

What is the expected result?

The same result on every backend.

For a shift by a count of the bit size or more, Go's result is a natural candidate: 0 for a left shift, and every bit set to the sign bit for a right shift. For a negative shift count and an integer division by zero, Go panics at run time, which a shader cannot do, so the result is still to be decided.

What happens instead?

The backends disagree on shifts:

Expression Metal OpenGL Go
7 / y, y = 0 -1 -1 panic
-7 / y, y = 0 1 1 panic
7 % y, y = 0 7 7 panic
-7 % y, y = 0 -7 -7 panic
7 << s, s = -1 -2147483648 0 panic
7 >> s, s = -1 0 0 panic
-7 >> s, s = -1 -1 -1 panic
7 << l, l = 40 1792 0 0
7 >> l, l = 40 0 0 0
-7 >> l, l = 40 -1 -1 -1

Metal keeps only the low 5 bits of the count: 7 << 40 is 7 << 8, and 7 << -1 is 7 << 31. Division by zero happens to agree on these two backends, but as far as I know the shading languages leave it undefined, like shifts by an out-of-range count. Only an Apple Silicon Mac was measured. DirectX, WebGL, mobile and the consoles were not.

Anything else you feel useful to add?

A prototype for shifts, not committed, rewrites them in the front end:

  • A constant count of 32 or more: x << n becomes (x << 31) << 1, and x >> n becomes x >> 31.
  • A variable count: x >> n becomes x >> min(n, 31). x << n stores n in a temporary t and becomes (x << min(t, 31)) << (min(t, 32) - min(t, 31)). An ivecN count is compared with ivecN(31).
  • <<= and >>= get the same rewrite.

With it, Metal and OpenGL both gave Go's results for a count of 40, as a constant and as a variable, for int and ivec2 operands and for <<= and >>=. A negative count still gave each backend's own value. The rewrite only uses constructs the front end already emits: a min call, shifts and a subtraction. It does not use the IR's Selection expression, which the front end never emits today, so the backends outside this repository may not support it.

There is also a compile-time inconsistency: x / 0 with a variable x and a constant zero compiles, while x >> -1 has been rejected since 3fb2c423e. Go rejects both.


Filed by Claude (Claude Code), on behalf of @hajimehoshi.