{"action":"created","comment":{"url":"https://api.github.com/repos/mixxxdj/mixxx/pulls/comments/25130785","id":25130785,"diff_hunk":"@@ -17,19 +17,69 @@ typedef qint32 int32_t;\n // When changing, be carefull to not prevent the vectorizing\n // https://gcc.gnu.org/projects/tree-ssa/vectorization.html\n \n+// TODO() Check if uintptr_t is availabe on all our build targets and use that\n+// instead of size_t, we can remove the sizeof(size_t) check than \n+static inline bool useAlignedAlloc() {\n+    // This will work on all targets and compilers. \n+    // It will return true on MSVC 32 bit builds and false for\n+    // Linux 32 and 64 bit builds\n+    return (sizeof(long double) == 8 && sizeof(CSAMPLE*) <= 8 &&\n+            sizeof(CSAMPLE*) == sizeof(size_t));\n+}\n+\n // static\n-CSAMPLE* SampleUtil::alloc(unsigned int size) {\n-    // TODO(XXX) align the array\n-    return new CSAMPLE[size];\n+CSAMPLE* SampleUtil::alloc(int size) {\n+    // For optimal use of SSE registers, it is required to align\n+    // the sample buffers to 16 Byte (128 bit) boundary\n+\n+    // Pointers returned by malloc are aligned for the largest scalar type,\n+    // which is long double with usually 16 byte.\n+    // An exception is MSVC X86 where long double is mapped to double.\n+\n+    // In case of sizeof(long double) = 8\n+    // this code over allocates the requested buffer to be able to\n+    // shift the returned pointer to a 16 byte alignment\n+    // In the memory before, a pointer to the original\n+    // malloced area is stored used to free the memory\n+    // This code can be replaced by C11 <stdlib.h> aligned_alloc()\n+    // or MSVC ::_aligned_malloc(size, alignment) and  ::_aligned_free(ptr);\n+\n+    if (useAlignedAlloc()) {\n+        // We need to shift the alignment to 16\n+        const size_t alignment = 16;\n+        const size_t unaligned_size = sizeof(CSAMPLE[size]) + alignment;\n+        void* pUnaligned = std::malloc(unaligned_size);\n+        if (pUnaligned == NULL) {\n+            return NULL;\n+        }\n+        // Shift\n+        void* pAlligned = (void*)(((size_t)pUnaligned & ~(alignment - 1)) + alignment);\n+        // Store pointer to original relative to the shifted pointer\n+        *((void**)(pAlligned) - 1) = pUnaligned;\n+        return (CSAMPLE*)pAlligned;\n+    } else {\n+        // We are either correct aligned or on an exotic architecture\n+        return new CSAMPLE[size];\n+    }\n }\n \n void SampleUtil::free(CSAMPLE* pBuffer) {\n-    delete[] pBuffer;\n+    // See SampleUtil::alloc() for details\n+    if (useAlignedAlloc()) {\n+        if (pBuffer == NULL) {\n+            return;\n+        }\n+        // Pointer to the original memory is stored before pBuffer\n+        std::free(*((void**)((void*)pBuffer) - 1));\n+    } else {\n+        // We are either correct aligned or on an exotic architecture\n+        delete[] pBuffer;\n+    }\n }\n \n // static\n void SampleUtil::applyGain(CSAMPLE* pBuffer, CSAMPLE_GAIN gain,\n-        unsigned int iNumSamples) {","path":"src/sampleutil.cpp","position":70,"original_position":70,"commit_id":"55dfb5244ca830a444f7889f73e3a83120e509c8","original_commit_id":"55dfb5244ca830a444f7889f73e3a83120e509c8","user":{"login":"daschuer","id":1777442,"avatar_url":"https://avatars.githubusercontent.com/u/1777442?v=3","gravatar_id":"","url":"https://api.github.com/users/daschuer","html_url":"https://github.com/daschuer","followers_url":"https://api.github.com/users/daschuer/followers","following_url":"https://api.github.com/users/daschuer/following{/other_user}","gists_url":"https://api.github.com/users/daschuer/gists{/gist_id}","starred_url":"https://api.github.com/users/daschuer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daschuer/subscriptions","organizations_url":"https://api.github.com/users/daschuer/orgs","repos_url":"https://api.github.com/users/daschuer/repos","events_url":"https://api.github.com/users/daschuer/events{/privacy}","received_events_url":"https://api.github.com/users/daschuer/received_events","type":"User","site_admin":false},"body":"Yes. MSVC does not support vectorizing with unsigned. ","created_at":"2015-02-22T10:10:56Z","updated_at":"2015-02-22T10:10:56Z","html_url":"https://github.com/mixxxdj/mixxx/pull/483#discussion_r25130785","pull_request_url":"https://api.github.com/repos/mixxxdj/mixxx/pulls/483","_links":{"self":{"href":"https://api.github.com/repos/mixxxdj/mixxx/pulls/comments/25130785"},"html":{"href":"https://github.com/mixxxdj/mixxx/pull/483#discussion_r25130785"},"pull_request":{"href":"https://api.github.com/repos/mixxxdj/mixxx/pulls/483"}}},"pull_request":{"url":"https://api.github.com/repos/mixxxdj/mixxx/pulls/483","id":28801720,"html_url":"https://github.com/mixxxdj/mixxx/pull/483","diff_url":"https://github.com/mixxxdj/mixxx/pull/483.diff","patch_url":"https://github.com/mixxxdj/mixxx/pull/483.patch","issue_url":"https://api.github.com/repos/mixxxdj/mixxx/issues/483","number":483,"state":"open","locked":false,"title":"Auto SSE Vectorizing","user":{"login":"daschuer","id":1777442,"avatar_url":"https://avatars.githubusercontent.com/u/1777442?v=3","gravatar_id":"","url":"https://api.github.com/users/daschuer","html_url":"https://github.com/daschuer","followers_url":"https://api.github.com/users/daschuer/followers","following_url":"https://api.github.com/users/daschuer/following{/other_user}","gists_url":"https://api.github.com/users/daschuer/gists{/gist_id}","starred_url":"https://api.github.com/users/daschuer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daschuer/subscriptions","organizations_url":"https://api.github.com/users/daschuer/orgs","repos_url":"https://api.github.com/users/daschuer/repos","events_url":"https://api.github.com/users/daschuer/events{/privacy}","received_events_url":"https://api.github.com/users/daschuer/received_events","type":"User","site_admin":false},"body":"This helps on 32 bit windows version ...","created_at":"2015-02-06T11:23:40Z","updated_at":"2015-02-22T10:10:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"13c9f3ed68bf3137091b3463f755e537c900117b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mixxxdj/mixxx/pulls/483/commits","review_comments_url":"https://api.github.com/repos/mixxxdj/mixxx/pulls/483/comments","review_comment_url":"https://api.github.com/repos/mixxxdj/mixxx/pulls/comments/{number}","comments_url":"https://api.github.com/repos/mixxxdj/mixxx/issues/483/comments","statuses_url":"https://api.github.com/repos/mixxxdj/mixxx/statuses/55dfb5244ca830a444f7889f73e3a83120e509c8","head":{"label":"daschuer:vectorize","ref":"vectorize","sha":"55dfb5244ca830a444f7889f73e3a83120e509c8","user":{"login":"daschuer","id":1777442,"avatar_url":"https://avatars.githubusercontent.com/u/1777442?v=3","gravatar_id":"","url":"https://api.github.com/users/daschuer","html_url":"https://github.com/daschuer","followers_url":"https://api.github.com/users/daschuer/followers","following_url":"https://api.github.com/users/daschuer/following{/other_user}","gists_url":"https://api.github.com/users/daschuer/gists{/gist_id}","starred_url":"https://api.github.com/users/daschuer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daschuer/subscriptions","organizations_url":"https://api.github.com/users/daschuer/orgs","repos_url":"https://api.github.com/users/daschuer/repos","events_url":"https://api.github.com/users/daschuer/events{/privacy}","received_events_url":"https://api.github.com/users/daschuer/received_events","type":"User","site_admin":false},"repo":{"id":10669071,"name":"mixxx","full_name":"daschuer/mixxx","owner":{"login":"daschuer","id":1777442,"avatar_url":"https://avatars.githubusercontent.com/u/1777442?v=3","gravatar_id":"","url":"https://api.github.com/users/daschuer","html_url":"https://github.com/daschuer","followers_url":"https://api.github.com/users/daschuer/followers","following_url":"https://api.github.com/users/daschuer/following{/other_user}","gists_url":"https://api.github.com/users/daschuer/gists{/gist_id}","starred_url":"https://api.github.com/users/daschuer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daschuer/subscriptions","organizations_url":"https://api.github.com/users/daschuer/orgs","repos_url":"https://api.github.com/users/daschuer/repos","events_url":"https://api.github.com/users/daschuer/events{/privacy}","received_events_url":"https://api.github.com/users/daschuer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/daschuer/mixxx","description":"Mixxx is Free DJ software that gives you everything you need to perform live mixes.","fork":true,"url":"https://api.github.com/repos/daschuer/mixxx","forks_url":"https://api.github.com/repos/daschuer/mixxx/forks","keys_url":"https://api.github.com/repos/daschuer/mixxx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/daschuer/mixxx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/daschuer/mixxx/teams","hooks_url":"https://api.github.com/repos/daschuer/mixxx/hooks","issue_events_url":"https://api.github.com/repos/daschuer/mixxx/issues/events{/number}","events_url":"https://api.github.com/repos/daschuer/mixxx/events","assignees_url":"https://api.github.com/repos/daschuer/mixxx/assignees{/user}","branches_url":"https://api.github.com/repos/daschuer/mixxx/branches{/branch}","tags_url":"https://api.github.com/repos/daschuer/mixxx/tags","blobs_url":"https://api.github.com/repos/daschuer/mixxx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/daschuer/mixxx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/daschuer/mixxx/git/refs{/sha}","trees_url":"https://api.github.com/repos/daschuer/mixxx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/daschuer/mixxx/statuses/{sha}","languages_url":"https://api.github.com/repos/daschuer/mixxx/languages","stargazers_url":"https://api.github.com/repos/daschuer/mixxx/stargazers","contributors_url":"https://api.github.com/repos/daschuer/mixxx/contributors","subscribers_url":"https://api.github.com/repos/daschuer/mixxx/subscribers","subscription_url":"https://api.github.com/repos/daschuer/mixxx/subscription","commits_url":"https://api.github.com/repos/daschuer/mixxx/commits{/sha}","git_commits_url":"https://api.github.com/repos/daschuer/mixxx/git/commits{/sha}","comments_url":"https://api.github.com/repos/daschuer/mixxx/comments{/number}","issue_comment_url":"https://api.github.com/repos/daschuer/mixxx/issues/comments{/number}","contents_url":"https://api.github.com/repos/daschuer/mixxx/contents/{+path}","compare_url":"https://api.github.com/repos/daschuer/mixxx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/daschuer/mixxx/merges","archive_url":"https://api.github.com/repos/daschuer/mixxx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/daschuer/mixxx/downloads","issues_url":"https://api.github.com/repos/daschuer/mixxx/issues{/number}","pulls_url":"https://api.github.com/repos/daschuer/mixxx/pulls{/number}","milestones_url":"https://api.github.com/repos/daschuer/mixxx/milestones{/number}","notifications_url":"https://api.github.com/repos/daschuer/mixxx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/daschuer/mixxx/labels{/name}","releases_url":"https://api.github.com/repos/daschuer/mixxx/releases{/id}","created_at":"2013-06-13T15:21:23Z","updated_at":"2015-02-15T22:06:22Z","pushed_at":"2015-02-20T22:13:27Z","git_url":"git://github.com/daschuer/mixxx.git","ssh_url":"git@github.com:daschuer/mixxx.git","clone_url":"https://github.com/daschuer/mixxx.git","svn_url":"https://github.com/daschuer/mixxx","homepage":null,"size":286932,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"mixxxdj:master","ref":"master","sha":"2f98ee9c700852ce170b5484207d49b48af455e8","user":{"login":"mixxxdj","id":1743769,"avatar_url":"https://avatars.githubusercontent.com/u/1743769?v=3","gravatar_id":"","url":"https://api.github.com/users/mixxxdj","html_url":"https://github.com/mixxxdj","followers_url":"https://api.github.com/users/mixxxdj/followers","following_url":"https://api.github.com/users/mixxxdj/following{/other_user}","gists_url":"https://api.github.com/users/mixxxdj/gists{/gist_id}","starred_url":"https://api.github.com/users/mixxxdj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mixxxdj/subscriptions","organizations_url":"https://api.github.com/users/mixxxdj/orgs","repos_url":"https://api.github.com/users/mixxxdj/repos","events_url":"https://api.github.com/users/mixxxdj/events{/privacy}","received_events_url":"https://api.github.com/users/mixxxdj/received_events","type":"Organization","site_admin":false},"repo":{"id":10126031,"name":"mixxx","full_name":"mixxxdj/mixxx","owner":{"login":"mixxxdj","id":1743769,"avatar_url":"https://avatars.githubusercontent.com/u/1743769?v=3","gravatar_id":"","url":"https://api.github.com/users/mixxxdj","html_url":"https://github.com/mixxxdj","followers_url":"https://api.github.com/users/mixxxdj/followers","following_url":"https://api.github.com/users/mixxxdj/following{/other_user}","gists_url":"https://api.github.com/users/mixxxdj/gists{/gist_id}","starred_url":"https://api.github.com/users/mixxxdj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mixxxdj/subscriptions","organizations_url":"https://api.github.com/users/mixxxdj/orgs","repos_url":"https://api.github.com/users/mixxxdj/repos","events_url":"https://api.github.com/users/mixxxdj/events{/privacy}","received_events_url":"https://api.github.com/users/mixxxdj/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mixxxdj/mixxx","description":"Mixxx is Free DJ software that gives you everything you need to perform live mixes.","fork":false,"url":"https://api.github.com/repos/mixxxdj/mixxx","forks_url":"https://api.github.com/repos/mixxxdj/mixxx/forks","keys_url":"https://api.github.com/repos/mixxxdj/mixxx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mixxxdj/mixxx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mixxxdj/mixxx/teams","hooks_url":"https://api.github.com/repos/mixxxdj/mixxx/hooks","issue_events_url":"https://api.github.com/repos/mixxxdj/mixxx/issues/events{/number}","events_url":"https://api.github.com/repos/mixxxdj/mixxx/events","assignees_url":"https://api.github.com/repos/mixxxdj/mixxx/assignees{/user}","branches_url":"https://api.github.com/repos/mixxxdj/mixxx/branches{/branch}","tags_url":"https://api.github.com/repos/mixxxdj/mixxx/tags","blobs_url":"https://api.github.com/repos/mixxxdj/mixxx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mixxxdj/mixxx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mixxxdj/mixxx/git/refs{/sha}","trees_url":"https://api.github.com/repos/mixxxdj/mixxx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mixxxdj/mixxx/statuses/{sha}","languages_url":"https://api.github.com/repos/mixxxdj/mixxx/languages","stargazers_url":"https://api.github.com/repos/mixxxdj/mixxx/stargazers","contributors_url":"https://api.github.com/repos/mixxxdj/mixxx/contributors","subscribers_url":"https://api.github.com/repos/mixxxdj/mixxx/subscribers","subscription_url":"https://api.github.com/repos/mixxxdj/mixxx/subscription","commits_url":"https://api.github.com/repos/mixxxdj/mixxx/commits{/sha}","git_commits_url":"https://api.github.com/repos/mixxxdj/mixxx/git/commits{/sha}","comments_url":"https://api.github.com/repos/mixxxdj/mixxx/comments{/number}","issue_comment_url":"https://api.github.com/repos/mixxxdj/mixxx/issues/comments{/number}","contents_url":"https://api.github.com/repos/mixxxdj/mixxx/contents/{+path}","compare_url":"https://api.github.com/repos/mixxxdj/mixxx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mixxxdj/mixxx/merges","archive_url":"https://api.github.com/repos/mixxxdj/mixxx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mixxxdj/mixxx/downloads","issues_url":"https://api.github.com/repos/mixxxdj/mixxx/issues{/number}","pulls_url":"https://api.github.com/repos/mixxxdj/mixxx/pulls{/number}","milestones_url":"https://api.github.com/repos/mixxxdj/mixxx/milestones{/number}","notifications_url":"https://api.github.com/repos/mixxxdj/mixxx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mixxxdj/mixxx/labels{/name}","releases_url":"https://api.github.com/repos/mixxxdj/mixxx/releases{/id}","created_at":"2013-05-17T15:14:38Z","updated_at":"2015-02-22T04:46:30Z","pushed_at":"2015-02-22T04:46:30Z","git_url":"git://github.com/mixxxdj/mixxx.git","ssh_url":"git@github.com:mixxxdj/mixxx.git","clone_url":"https://github.com/mixxxdj/mixxx.git","svn_url":"https://github.com/mixxxdj/mixxx","homepage":"http://mixxx.org","size":313293,"stargazers_count":241,"watchers_count":241,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":194,"mirror_url":null,"open_issues_count":37,"forks":194,"open_issues":37,"watchers":241,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mixxxdj/mixxx/pulls/483"},"html":{"href":"https://github.com/mixxxdj/mixxx/pull/483"},"issue":{"href":"https://api.github.com/repos/mixxxdj/mixxx/issues/483"},"comments":{"href":"https://api.github.com/repos/mixxxdj/mixxx/issues/483/comments"},"review_comments":{"href":"https://api.github.com/repos/mixxxdj/mixxx/pulls/483/comments"},"review_comment":{"href":"https://api.github.com/repos/mixxxdj/mixxx/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/mixxxdj/mixxx/pulls/483/commits"},"statuses":{"href":"https://api.github.com/repos/mixxxdj/mixxx/statuses/55dfb5244ca830a444f7889f73e3a83120e509c8"}}}}