| /* | |
| * Copyright 2023 Google LLC | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://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. | |
| */ | |
| #include <riscv_vector.h> | |
| #include "risp4ml/common/utils.h" | |
| #include "risp4ml/isp_stages/dg.h" | |
| static const uint16_t kDgFractional = kRawPipelineFraction; | |
| static const uint16_t kDgUnityGain = 1 << kDgFractional; | |
| static DgParams dg_params = { | |
| .enable = true, | |
| .gains = {kDgUnityGain, kDgUnityGain, kDgUnityGain, kDgUnityGain}}; | |
| void set_dg_params(DgParams* params) { dg_params = *params; } | |
| void dg_process(Image* img) { | |
| if (!dg_params.enable) return; | |
| size_t vl; | |
| size_t n = img->height * img->width * img->num_channels; | |
| uint16_t gain = dg_params.gains[0]; | |
| uint32_t offset = 1 << (kDgFractional - 1); | |
| uint32_t max_val = kRawPipelineMaxVal << kDgFractional; | |
| // auxiliary variables | |
| vuint16m4_t vx; | |
| vuint32m8_t vy; | |
| uint16_t* x; | |
| for (size_t i = 0; i < n; i += vl) { | |
| x = img->data + i; | |
| vl = __riscv_vsetvl_e16m4(n - i); | |
| vx = __riscv_vle16_v_u16m4(x, vl); // load | |
| vy = __riscv_vwmulu(vx, gain, vl); // multiply | |
| vy = __riscv_vadd(vy, offset, vl); // add | |
| vy = __riscv_vminu(vy, max_val, vl); // clamp | |
| vx = __riscv_vnsrl(vy, kDgFractional, vl); // bit shift | |
| __riscv_vse16(x, vx, vl); // save | |
| } | |
| } |