Skip to content

Is there any shift right/left operation's implementation for typed_data Int32x4 on dart? #47114

Open

Description

@licy183

This tracker is for issues related to:

  • Dart core libraries dart:typed_data

I'm working on some crypto algorithms using SIMD on Dart. And I need to shift right all the lanes in Int32x4.

I have read the API Docs, and I have found the and/xor/or bit-wise operaion, but no shiftr/shiftl operaion.

Maybe I can implentment it as follwing:

import 'dart:typed_data';

void _run10000000TimesXorAndShuffle(int x, int y, int z, int w) {
  var ope = Int32x4(x, y, z, w);
  var ope2 = Int32x4(y, z, w, x);
  for (var i = 0; i < 10000000; ++i) {
    var c = ope ^ ope2;
    c.hashCode;
  }
}

void _run10000000TimesShiftRightAndShuffle(int x, int y, int z, int w) {
  var ope = Int32x4(x, y, z, w);
  for (var i = 0; i < 10000000; ++i) {
    var x = ope.x;
    var y = ope.y;
    var z = ope.z;
    var w = ope.w;
    var res = Int32x4(x << 1, y << 1, z << 1, w << 1);
    res.hashCode;
  }
}

void main() {
  var start = DateTime.now().millisecondsSinceEpoch;
  _run10000000TimesXorAndShuffle(0x918a57a0, 0x2095b753, 0x2041660, 0xe0e98a15);
  var end = DateTime.now().millisecondsSinceEpoch;
  print(end - start);
  start = DateTime.now().millisecondsSinceEpoch;
  _run10000000TimesShiftRightAndShuffle(
      0x918a57a0, 0x2095b753, 0x2041660, 0xe0e98a15);
  end = DateTime.now().millisecondsSinceEpoch;
  print(end - start);
}
// 30
// 7660

But it seems not efficient as the xor operation. So is there any other way to implement shiftr/shiftl operation on Int32x4, or will dart API add such method?

Activity

  1. lrhn commented on Sep 6, 2021

    @lrhn
    Member

    There is currently no shift operations on Int32x4.

    If all you need is to do a left-shift by one, you can probably get around it as var res = ope + ope;.
    Right shift, as you ask for, is much harder to simulate.

  2. added
    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
    type-enhancementA request for a change that isn't a bug
    on Sep 6, 2021
  3. licy183 commented on Sep 6, 2021

    @licy183
    Author

    So will Dart SDK add such operators in the future? I think this is important for mathematical calculations using SIMD on Dart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-typed-datatype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions