KleidiAI Coverage Report


Directory: ./
File: test/reference/cast.cpp
Date: 2025-10-20 13:18:31
Coverage Exec Excl Total
Lines: 76.9% 30 1 40
Functions: 80.0% 8 0 10
Branches: 46.2% 49 0 106

Line Branch Exec Source
1 //
2 // SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6
7 #include "test/reference/cast.hpp"
8
9 #include <cstddef>
10 #include <cstdint>
11
12 #include "kai/kai_common.h"
13 #include "test/common/bfloat16.hpp"
14 #include "test/common/buffer.hpp"
15 #include "test/common/data_type.hpp"
16 #include "test/common/float16.hpp"
17 #include "test/common/memory.hpp"
18 #include "test/common/round.hpp"
19
20 namespace kai::test {
21
22 template <typename DstType, typename SrcType>
23 11192 Buffer cast(const void* src, size_t length) {
24 11192 Buffer dst(round_up_division(length * size_in_bits<DstType>, 8));
25
26
10/12
✓ Branch 0 taken 3130780 times.
✓ Branch 1 taken 3984 times.
✓ Branch 2 taken 2721210 times.
✓ Branch 3 taken 2384 times.
✓ Branch 4 taken 3475372 times.
✓ Branch 5 taken 108 times.
✓ Branch 6 taken 2361560 times.
✓ Branch 7 taken 3736 times.
✓ Branch 8 taken 1942616 times.
✓ Branch 9 taken 980 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
13642730 for (size_t i = 0; i < length; ++i) {
27
20/48
✓ Branch 0 taken 3130780 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3130780 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3130780 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3130780 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2721210 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2721210 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2721210 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2721210 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 3475372 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 3475372 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 3475372 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 3475372 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 2361560 times.
✓ Branch 26 taken 2361560 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 2361560 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 2361560 times.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✓ Branch 33 taken 1942616 times.
✓ Branch 34 taken 1942616 times.
✗ Branch 35 not taken.
✓ Branch 36 taken 1942616 times.
✗ Branch 37 not taken.
✓ Branch 38 taken 1942616 times.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
13631538 write_array(dst.data(), i, static_cast<DstType>(read_array<SrcType>(src, i)));
28 13631538 }
29
30 11192 return dst;
31 11192 }
32
33 template <>
34 Buffer cast<BFloat16<false>, Float16>(const void* src, size_t length) {
35 Buffer dst(round_up_division(length * size_in_bits<BFloat16<>>, 8));
36
37 for (size_t i = 0; i < length; ++i) {
38 float interim = static_cast<float>(read_array<Float16>(src, i));
39 write_array(dst.data(), i, BFloat16<false>(interim));
40 }
41
42 return dst;
43 }
44
45 template <>
46 40 Buffer cast<BFloat16<true>, Float16>(const void* src, size_t length) {
47 40 Buffer dst(round_up_division(length * size_in_bits<BFloat16<>>, 8));
48
49
2/2
✓ Branch 0 taken 593616 times.
✓ Branch 1 taken 40 times.
593656 for (size_t i = 0; i < length; ++i) {
50
2/4
✓ Branch 0 taken 593616 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 593616 times.
✗ Branch 3 not taken.
593616 float interim = static_cast<float>(read_array<Float16>(src, i));
51
3/6
✓ Branch 0 taken 593616 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 593616 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 593616 times.
✗ Branch 5 not taken.
593616 write_array(dst.data(), i, BFloat16<true>(interim));
52 593616 }
53
54 40 return dst;
55 40 }
56
57 template Buffer cast<Float16, float>(const void* src, size_t length);
58 template Buffer cast<BFloat16<false>, float>(const void* src, size_t length);
59 template Buffer cast<BFloat16<true>, float>(const void* src, size_t length);
60 template Buffer cast<float, Float16>(const void* src, size_t length);
61 template Buffer cast<float, BFloat16<false>>(const void* src, size_t length);
62 template Buffer cast<float, BFloat16<true>>(const void* src, size_t length);
63
64 148 Buffer cast(const void* src, kai::test::DataType src_dt, DataType dst_dt, size_t height, size_t width) {
65 148 const auto length = height * width;
66
67
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
148 if (src_dt == DataType::BF16 && dst_dt == DataType::FP32) {
68 return cast<float, BFloat16<>>(src, length);
69
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
148 } else if (src_dt == DataType::FP16 && dst_dt == DataType::BF16) {
70 40 return cast<BFloat16<>, Float16>(src, length);
71
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 } else if (src_dt == DataType::FP32 && dst_dt == DataType::BF16) {
72 108 return cast<BFloat16<>, float>(src, length);
73 }
74
75 KAI_ERROR("Unsupported cast data type!");
76 148 }
77
78 8262 Buffer cast_qsu4_qsi4(const void* src, size_t length) {
79 8262 Buffer dst(round_up_division(length, 2));
80
81
2/2
✓ Branch 0 taken 49710168 times.
✓ Branch 1 taken 8262 times.
49718430 for (size_t i = 0; i < length; ++i) {
82
5/10
✓ Branch 0 taken 49710168 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49710168 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 49710168 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 49710168 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 49710168 times.
✗ Branch 9 not taken.
49710168 write_array(dst.data(), i, static_cast<UInt4>(static_cast<int32_t>(read_array<Int4>(src, i)) + 8));
83 49710168 }
84
85 8262 return dst;
86 8262 }
87
88 } // namespace kai::test
89