Skip to content

Instantly share code, notes, and snippets.

@sheaf
Created August 29, 2024 16:16
Show Gist options
  • Select an option

  • Save sheaf/f4d634bb0272491866ac8f57dfecde75 to your computer and use it in GitHub Desktop.

Select an option

Save sheaf/f4d634bb0272491866ac8f57dfecde75 to your computer and use it in GitHub Desktop.
GHC C call SIMD bug
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UnliftedFFITypes #-}
-- test C calls with SIMD vectors
module Main where
import GHC.Exts
import GHC.Prim
--foreign import ccall unsafe "sub"
-- sub :: DoubleX2# -> DoubleX2# -> DoubleX2#
foreign import ccall unsafe "add7"
add7 :: DoubleX2# -> DoubleX2# -> DoubleX2# -> DoubleX2# -> DoubleX2# -> DoubleX2# -> DoubleX2# -> DoubleX2#
--add7 :: Double# -> Double# -> Double# -> Double# -> Double# -> Double# -> Double# -> Double#
main :: IO ()
main = do
let x1, x2 :: DoubleX2#
x1 = packDoubleX2# (# 9.9##, 99.99## #)
x2 = packDoubleX2# (# 1.1##, 11.11## #)
y1, y2, y3, y4, y5, y6, y7 :: DoubleX2#
!y1 = packDoubleX2# (# 1.5##, 2.5## #)
!y2 = packDoubleX2# (# 10.25##, 20.25## #)
!y3 = packDoubleX2# (# 100.125##, 200.125## #)
!y4 = packDoubleX2# (# 1000.0625##, 2000.0625## #)
!y5 = packDoubleX2# (# 10000.03125##, 20000.03125## #)
!y6 = packDoubleX2# (# 100000.015625##, 200000.015625## #)
!y7 = packDoubleX2# (# 1000000.0078125##, 2000000.0078125## #)
-- !(# a, b #) = unpackDoubleX2# ( sub x1 x2 )
!(# c, d #) = unpackDoubleX2# ( add7 y1 y2 y3 y4 y5 y6 y7 )
-- print ( D# a, D# b )
print ( D# c, D# d )
{-
let
y1, y2, y3, y4, y5, y6, y7 :: Double#
!y1 = 1.5##
!y2 = 10.25##
!y3 = 100.125##
!y4 = 1000.0625##
!y5 = 10000.03125##
!y6 = 100000.015625##
!y7 = 1000000.0078125##
!c = add7 y1 y2 y3 y4 y5 y6 y7
print ( D# c )
-}
#include <xmmintrin.h>
__m128d sub(__m128d x, __m128d y)
{
return _mm_sub_pd(x,y);
}
__m128d add7(__m128d x1, __m128d x2, __m128d x3, __m128d x4, __m128d x5, __m128d x6, __m128d x7)
{
return _mm_add_pd(x1,_mm_add_pd(x2,_mm_add_pd(x3,_mm_add_pd(x4,_mm_add_pd(x5,_mm_add_pd(x6, x7))))));
}
/*
double add7(double x1, double x2, double x3, double x4, double x5, double x6, double x7)
{
return (x1 + x2 + x3 + x4 + x5 + x6 + x7);
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment