Skip to content

The results of arithmetic operations with u8 are not truncated to 8 bits, when assigned to f64 #2812

Closed

Description

@palisarbaro

Bug description

export function add(a: u8): f64 {
  let x:u8 = 128
  x = x + a
  return x;
}

This function compiles to

(module
 (type $0 (func (param i32) (result f64)))
 (memory $0 0)
 (export "add" (func $assembly/index/add))
 (export "memory" (memory $0))
 (func $assembly/index/add (param $0 i32) (result f64)
  local.get $0
  i32.const 128
  i32.add
  ;; i32.const 255   missing cast to u8
  ;; i32.and             
  f64.convert_i32_u
 )
)

Steps to reproduce

export function add(a: u8): f64 {
  let x:u8 = 128
  x = x + a
  return x;
}

From js

add(129) // actual 257; expected 1

AssemblyScript version

0.27.22

Activity

  1. CountBleck commented on Jan 14, 2024

    @CountBleck
    Member

    I'd assume this is a problem involving a missing Constraints.MustWrap.

  2. self-assigned this
    on Jan 15, 2024
  3. changed the title [-]The results of arithmetic operations with u8 are not truncated to 8 bytes, when assigned to f64[/-] [+]The results of arithmetic operations with u8 are not truncated to 8 bits, when assigned to f64[/+] on Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    ⌂ Proxy