/******************************************************************************* * Copyright (c) 2015-2018 Skymind, Inc. * * This program and the accompanying materials are made available under the * terms of the Apache License, Version 2.0 which is available at * https://www.apache.org/licenses/LICENSE-2.0. * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * SPDX-License-Identifier: Apache-2.0 ******************************************************************************/ // // @author raver119@gmail.com // #include #include namespace nd4j { namespace ops { namespace helpers { template static void _hamming(NDArray &x, NDArray &y, NDArray &z) { auto xEws = x.ews(); auto yEws = y.ews(); auto xBuffer = x.bufferAsT(); auto yBuffer = y.bufferAsT(); Nd4jLong distance = 0; if (xEws == 1 && yEws == 1 && x.ordering() == y.ordering()) { PRAGMA_OMP_PARALLEL_FOR_SIMD_REDUCTION(+:distance) for (Nd4jLong e = 0; e < x.lengthOf(); e++) { auto _x = static_cast(xBuffer[e]); auto _y = static_cast(yBuffer[e]); distance += __builtin_popcountll(_x ^ _y); } } else if (xEws > 1 && yEws > 1 && x.ordering() == y.ordering()) { PRAGMA_OMP_PARALLEL_FOR_SIMD_REDUCTION(+:distance) for (Nd4jLong e = 0; e < x.lengthOf(); e++) { auto _x = static_cast(xBuffer[e * xEws]); auto _y = static_cast(yBuffer[e * yEws]); distance += __builtin_popcountll(_x ^ _y); } } else { PRAGMA_OMP_PARALLEL_FOR_SIMD_REDUCTION(+:distance) for (Nd4jLong e = 0; e < x.lengthOf(); e++) { auto _x = static_cast(x.e(e)); auto _y = static_cast(y.e(e)); distance += __builtin_popcountll(_x ^ _y); } } z.p(0, distance); } void hamming(LaunchContext *context, NDArray &x, NDArray &y, NDArray &output) { BUILD_DOUBLE_SELECTOR(x.dataType(), output.dataType(), _hamming, (x, y, output), INTEGER_TYPES, INDEXING_TYPES); } } } }