Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ggml/src/ggml-cpu/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ static inline float bf16_to_f32(ggml_bf16_t x) {
return GGML_BF16_TO_FP32(x);
}

static inline float i32_to_f32(int32_t x) {
return x;
}

static inline int32_t f32_to_i32(float x) {
return x;
}

Comment on lines +31 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Define f32↔i32 semantics; add saturation and explicit casts to prevent UB and warnings

  • Implicit int→float and float→int conversions can trigger -Wconversion and yield UB for NaN/out‑of‑range floats. Make casts explicit and clamp float→int32_t.

Apply this diff:

 static inline float i32_to_f32(int32_t x) {
-    return x;
+    return static_cast<float>(x);
 }
 
 static inline int32_t f32_to_i32(float x) {
-    return x;
+    // Saturate and truncate toward zero to match C/C++ cast semantics
+    if (std::isnan(x)) return 0;
+    constexpr float kMin = static_cast<float>(std::numeric_limits<int32_t>::min());
+    constexpr float kMax = static_cast<float>(std::numeric_limits<int32_t>::max());
+    if (x <= kMin) return std::numeric_limits<int32_t>::min();
+    if (x >= kMax) return std::numeric_limits<int32_t>::max();
+    return static_cast<int32_t>(x);
 }

Add the following includes near the existing C++ includes (outside this hunk):

#include <limits>
#include <cmath>
#include <cstdint> // if not already provided transitively
🤖 Prompt for AI Agents
In ggml/src/ggml-cpu/common.h around lines 31 to 38, the conversion helpers use
implicit conversions that cause -Wconversion warnings and undefined behavior for
NaN/out-of-range floats; update as follows: add includes <limits>, <cmath>, and
<cstdint> near the other C++ includes, change i32_to_f32 to use an explicit
static_cast<float>(x), and change f32_to_i32 to explicitly handle NaN and
saturate to int32 bounds using std::numeric_limits<int32_t>::min()/max() (e.g.,
if std::isnan(x) return 0; if x <= min return min; if x >= max return max;
otherwise return static_cast<int32_t>(x) or use a defined rounding strategy),
ensuring all casts are explicit to avoid UB and warnings.

static inline float f32_to_f32(float x) {
return x;
}
Expand All @@ -54,6 +62,12 @@ struct type_conversion_table<ggml_bf16_t> {
static constexpr ggml_bf16_t (*from_f32)(float) = f32_to_bf16;
};

template <>
struct type_conversion_table<int32_t> {
static constexpr float (*to_f32)(int32_t) = i32_to_f32;
static constexpr int32_t (*from_f32)(float) = f32_to_i32;
};

static std::pair<int64_t, int64_t> get_thread_range(const struct ggml_compute_params * params, const struct ggml_tensor * src0) {
const int64_t ith = params->ith;
const int64_t nth = params->nth;
Expand Down
Loading