This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Crashed when call time.time() after using _mm_xor_si64
Type: crash Stage:
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Xiongzhi Gao, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2016-01-02 08:49 by Xiongzhi Gao, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg257337 - (view) Author: Xiongzhi Gao (Xiongzhi Gao) Date: 2016-01-02 08:49
The version of windows is Windows 7 Service Pack 1.
The version of Python is 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32.
The version of compiler in visual studio 10 is 16.00.40219.01 for 80x86.

I try to use SWIG to port a function that use _mm_xor_si64 like this:

packed_sse.i

> %module packed_sse
> %{
> extern long long _packed_mm_xor_si64(long long m1, long long m2);
> %}
> extern long long _packed_mm_xor_si64(long long m1, long long m2);

packed_sse.c

> #include <mmintrin.h>
> 
> __inline __m64 int64_to_m64 (const long long i) {
>     union {
>         long long i;
>         __m64 v;
>     } u;
>     u.i = i;
>     return u.v;
> }
> 
> __inline long long m64_to_int64 (const __m64 v) {
>     union {
>         long long i;
>         __m64 v;
>     } u;
>     u.v = v;
>     return u.i;
> }
> 
> long long _packed_mm_xor_si64(long long m1, long long m2) {
>     __m64 m64_m1 = int64_to_m64(m1), m64_m2 = int64_to_m64(m2);
>     __m64 m64_result = _mm_xor_si64(m64_m1, m64_m2);
>     return m64_to_int64(m64_result);

I use swig and compiler to port C to Python.
    
I try to test like this, it works:

test_swig.py

> # -*- coding: utf-8 -*-
> # !/bin/env python2
> 
> import random
> 
> import packed_sse
> 
> 
> if __name__ == "__main__":
>     for i in range(100000):
>         a, b = random.getrandbits(20), random.getrandbits(20)
>         _ = packed_sse._packed_mm_xor_si64(
>             a, b
>         )
>         assert a ^ b == _        

But when I try to profile the function like this, the output of first `print time.time() - _beg` is `nan` and Python crashed when run into second `print time.time() - _beg`:
    
profile_swig.py

> # -*- coding: utf-8 -*-
> # !/bin/env python2
> 
> import random
> import time
> 
> import packed_sse
> 
> 
> if __name__ == "__main__":
>     _beg = time.time()
>     for i in range(100000):
>         _ = packed_sse._packed_mm_xor_si64(
>             random.getrandbits(20), random.getrandbits(20)
>         )
>     print time.time() - _beg  # First
>     _beg = time.time()
>     for i in range(100000):
>         _ = random.getrandbits(20) ^ random.getrandbits(20)
>     print time.time() - _beg  # Second

I try to use `gdb` on MingGW to debug it, it said:
    
> (gdb) stop
> (gdb) c
> Continuing.
> 
> Program received signal SIGSEGV, Segmentation fault.
> [Switching to Thread 7172.0xadc]
> 0x534fbe6c in python27!_Py_dg_dtoa () from C:\Windows\system32\python27.dll
msg258416 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2016-01-16 21:39
This is something that you need to fix in the code using MMX. I don't recall the details, but there is an instruction that needs to be executed between using MMX and using the FPU.

Python can't make this judgement every time it calls into native code, so the external code has to do it.
History
Date User Action Args
2022-04-11 14:58:25adminsetgithub: 70181
2016-01-16 21:39:07steve.dowersetstatus: open -> closed
resolution: not a bug
messages: + msg258416
2016-01-02 08:49:38Xiongzhi Gaocreate