Skip to content

Instantly share code, notes, and snippets.

@ficapy
Last active December 6, 2023 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ficapy/38736734eec3cd39ac4fa2c2adfa9b5d to your computer and use it in GitHub Desktop.
Save ficapy/38736734eec3cd39ac4fa2c2adfa9b5d to your computer and use it in GitHub Desktop.
mojo simd
#include <stdio.h>
#include <x86intrin.h>
// gcc -msse4.1 -O0 -o program program.c
int main() {
__m128i A = _mm_set_epi32(4, 3, 2, 1);
__m128i B = _mm_set_epi32(8, 7, 6, 5);
__m128i Cond = _mm_set_epi32(0, -1, -1, 0);
__m128i Result = _mm_blendv_epi8(A, B, Cond);
int result[4];
_mm_storeu_si128((__m128i*)result, Result);
printf("Result: %d %d %d %d\n", result[0], result[1], result[2], result[3]);
return 0;
}
import math
fn main():
let a = math.iota[DType.int32, 4](1)
let b = math.iota[DType.int32, 4](5)
let mask = SIMD[DType.bool, 4](True, False, False, True)
let c = mask.select(a, b)
print(c)
# https://mzaks.medium.com/counting-chars-with-simd-in-mojo-140ee730bd4d
from algorithm import vectorize
fn utf8_count(s: StringLiteral) -> Int:
alias simd_width_u8 = simdwidthof[DType.uint8]()
let p = s.data().bitcast[DType.uint8]()
let string_bytes_length = len(s)
var result = 0
@parameter
fn count[simd_width: Int](offset: Int):
result += ((p.simd_load[simd_width](offset) >> 6) != 0b10).cast[DType.uint8]().reduce_add().to_int()
vectorize[simd_width_u8, count](string_bytes_length)
return result
fn main():
print(utf8_count("你好,世界"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment