From c003281c537e6348745c2c845207e63d16145f5c Mon Sep 17 00:00:00 2001 From: nihui Date: Thu, 24 Aug 2023 16:24:56 +0800 Subject: [PATCH] fix unaryop round build with old glibc (#4963) --- src/layer/arm/unaryop_arm.cpp | 8 ++++++++ src/layer/arm/unaryop_arm_asimdhp.cpp | 4 ++++ src/layer/loongarch/unaryop_loongarch.cpp | 4 ++++ src/layer/mips/unaryop_mips.cpp | 4 ++++ src/layer/unaryop.cpp | 4 ++++ src/layer/x86/unaryop_x86.cpp | 4 ++++ 6 files changed, 28 insertions(+) diff --git a/src/layer/arm/unaryop_arm.cpp b/src/layer/arm/unaryop_arm.cpp index 56c673dec46d..5a054cc7c4d6 100644 --- a/src/layer/arm/unaryop_arm.cpp +++ b/src/layer/arm/unaryop_arm.cpp @@ -402,10 +402,14 @@ struct unary_op_round #endif return y; #else +#ifdef FE_TONEAREST int old_rm = fegetround(); fesetround(FE_TONEAREST); +#endif float y = nearbyintf(x); +#ifdef FE_TONEAREST fesetround(old_rm); +#endif return y; #endif } @@ -431,13 +435,17 @@ struct unary_op_round #else float tmp[4]; vst1q_f32(tmp, x); +#ifdef FE_TONEAREST int old_rm = fegetround(); fesetround(FE_TONEAREST); +#endif tmp[0] = nearbyintf(tmp[0]); tmp[1] = nearbyintf(tmp[1]); tmp[2] = nearbyintf(tmp[2]); tmp[3] = nearbyintf(tmp[3]); +#ifdef FE_TONEAREST fesetround(old_rm); +#endif float32x4_t y = vld1q_f32(tmp); return y; #endif diff --git a/src/layer/arm/unaryop_arm_asimdhp.cpp b/src/layer/arm/unaryop_arm_asimdhp.cpp index a18d6780ca84..02532db4114b 100644 --- a/src/layer/arm/unaryop_arm_asimdhp.cpp +++ b/src/layer/arm/unaryop_arm_asimdhp.cpp @@ -471,10 +471,14 @@ struct unary_op_round_fp16s :); return y; #else +#ifdef FE_TONEAREST int old_rm = fegetround(); fesetround(FE_TONEAREST); +#endif __fp16 y = (__fp16)nearbyintf(x); +#ifdef FE_TONEAREST fesetround(old_rm); +#endif return y; #endif } diff --git a/src/layer/loongarch/unaryop_loongarch.cpp b/src/layer/loongarch/unaryop_loongarch.cpp index 67ae6da0eaff..4d4818cb5af0 100644 --- a/src/layer/loongarch/unaryop_loongarch.cpp +++ b/src/layer/loongarch/unaryop_loongarch.cpp @@ -383,10 +383,14 @@ struct unary_op_round :); return y; #else +#ifdef FE_TONEAREST int old_rm = fegetround(); fesetround(FE_TONEAREST); +#endif float y = nearbyintf(x); +#ifdef FE_TONEAREST fesetround(old_rm); +#endif return y; #endif } diff --git a/src/layer/mips/unaryop_mips.cpp b/src/layer/mips/unaryop_mips.cpp index da51b8b55060..b923535a2d8a 100644 --- a/src/layer/mips/unaryop_mips.cpp +++ b/src/layer/mips/unaryop_mips.cpp @@ -397,10 +397,14 @@ struct unary_op_round :); return y; #else +#ifdef FE_TONEAREST int old_rm = fegetround(); fesetround(FE_TONEAREST); +#endif float y = nearbyintf(x); +#ifdef FE_TONEAREST fesetround(old_rm); +#endif return y; #endif } diff --git a/src/layer/unaryop.cpp b/src/layer/unaryop.cpp index 44bc9c1f2d1c..2fe77717ed3c 100644 --- a/src/layer/unaryop.cpp +++ b/src/layer/unaryop.cpp @@ -198,10 +198,14 @@ struct unary_op_round float operator()(const float& x) const { // round to nearest even +#ifdef FE_TONEAREST int old_rm = fegetround(); fesetround(FE_TONEAREST); +#endif float y = nearbyintf(x); +#ifdef FE_TONEAREST fesetround(old_rm); +#endif return y; } }; diff --git a/src/layer/x86/unaryop_x86.cpp b/src/layer/x86/unaryop_x86.cpp index cb014d171b6b..8629ab2093b4 100644 --- a/src/layer/x86/unaryop_x86.cpp +++ b/src/layer/x86/unaryop_x86.cpp @@ -578,10 +578,14 @@ struct unary_op_round { // round to nearest even // return (x + 12582912.f) - 12582912.f; +#ifdef FE_TONEAREST int old_rm = fegetround(); fesetround(FE_TONEAREST); +#endif float y = nearbyintf(x); +#ifdef FE_TONEAREST fesetround(old_rm); +#endif return y; } #if __SSE2__