Skip to content

Commit

Permalink
fix the bug of std::abs
Browse files Browse the repository at this point in the history
  • Loading branch information
wnqn1597 committed Aug 16, 2023
1 parent 087e035 commit 191a00d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/layer/diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ int Diag::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) cons
if (dims == 1)
{
int w = bottom_blob.w;
int top_w = w + std::abs(diagonal);
int stride = top_w + 1;
int top_w = w + ((diagonal >= 0) ? diagonal : -diagonal);

top_blob.create(top_w, top_w, elemsize, opt.blob_allocator);
if (top_blob.empty())
Expand All @@ -58,10 +57,16 @@ int Diag::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) cons
{
int w = bottom_blob.w;
int h = bottom_blob.h;
float tmp = (w - h) / 2.0;

int len = std::min(w, h) - (int)std::max(std::abs(diagonal - tmp) - std::abs(tmp), 0.0f);
len = std::max(len, 0);
int len = 0;
int minimum = std::min(w - h, 0);
int maximum = std::max(w - h, 0);
if (diagonal <= maximum && diagonal >= minimum)
len = std::min(w, h);
else if (diagonal > -h && diagonal < minimum)
len = diagonal + h;
else if (diagonal > maximum && diagonal < w)
len = -diagonal + w;

top_blob.create(len, elemsize, opt.blob_allocator);
if (top_blob.empty())
Expand Down

0 comments on commit 191a00d

Please sign in to comment.