1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "berberis/intrinsics/intrinsics.h"
18
19 #include <cstdint>
20 #include <tuple>
21
22 namespace berberis::intrinsics {
23
Bclri(uint64_t src,uint8_t imm)24 std::tuple<uint64_t> Bclri(uint64_t src, uint8_t imm) {
25 return {src & ~(uint64_t{1} << imm)};
26 }
27
Bexti(uint64_t src,uint8_t imm)28 std::tuple<uint64_t> Bexti(uint64_t src, uint8_t imm) {
29 return {(src >> imm) & uint64_t{1}};
30 }
31
Binvi(uint64_t src,uint8_t imm)32 std::tuple<uint64_t> Binvi(uint64_t src, uint8_t imm) {
33 return {src ^ (uint64_t{1} << imm)};
34 }
35
Bseti(uint64_t src,uint8_t imm)36 std::tuple<uint64_t> Bseti(uint64_t src, uint8_t imm) {
37 return {src | (uint64_t{1} << imm)};
38 }
39
CPUClockCount()40 std::tuple<uint64_t> CPUClockCount() {
41 uint64_t a, d;
42 asm volatile("rdtsc" : "=a"(a), "=d"(d));
43 return (d << 32) | a;
44 }
45
Slliuw(uint32_t src,uint8_t imm)46 std::tuple<uint64_t> Slliuw(uint32_t src, uint8_t imm) {
47 return {uint64_t{src} << imm};
48 }
49
InitState()50 void InitState() {
51 // TODO(b/276787675): Initialize host MXCSR register once riscv64 intrinsics are supported.
52 }
53
54 } // namespace berberis::intrinsics
55