@@ -780,7 +780,7 @@ int64_t hdr_total_count(const struct hdr_histogram* h)
780780
781781int64_t hdr_min (const struct hdr_histogram * h )
782782{
783- if (0 < hdr_count_at_index (h , 0 ))
783+ if (0 == hdr_total_count ( h ) || 0 < hdr_count_at_index (h , 0 ))
784784 {
785785 return 0 ;
786786 }
@@ -914,36 +914,60 @@ static int64_t get_value_from_idx_up_to_count(const struct hdr_histogram* h, int
914914}
915915
916916
917+ /* Target cumulative count in [1, total_count]; NaN/+inf -> total_count, -inf/negative -> 1 */
918+ static int64_t count_at_percentile (int64_t total_count , double percentile )
919+ {
920+ const double requested = percentile < 100.0 ? percentile : 100.0 ;
921+ const double cc = ((requested / 100 ) * (double ) total_count ) + 0.5 ;
922+ if (!(cc >= 1.0 )) return 1 ;
923+ if (cc >= (double ) total_count ) return total_count ;
924+ return (int64_t ) cc ;
925+ }
926+
917927int64_t hdr_value_at_percentile (const struct hdr_histogram * h , double percentile )
918928{
919- double requested_percentile = percentile < 100.0 ? percentile : 100.0 ;
920- int64_t count_at_percentile =
921- (int64_t ) (((requested_percentile / 100 ) * h -> total_count ) + 0.5 );
922- int64_t value_from_idx = get_value_from_idx_up_to_count (h , count_at_percentile );
929+ /* empty: the scan's not-found 0 must not be rounded up to highest_equivalent_value(0) */
930+ if (HDR_UNLIKELY (h -> total_count == 0 ))
931+ {
932+ return 0 ;
933+ }
934+ int64_t value_from_idx =
935+ get_value_from_idx_up_to_count (h , count_at_percentile (h -> total_count , percentile ));
923936 if (percentile == 0.0 )
924937 {
925938 return lowest_equivalent_value (h , value_from_idx );
926939 }
927940 return highest_equivalent_value (h , value_from_idx );
928941}
929942
943+ /* p0 is the bucket's lowest value, matching hdr_value_at_percentile */
944+ static int64_t percentile_bound (const struct hdr_histogram * h , double percentile , int64_t value )
945+ {
946+ return percentile == 0.0 ? lowest_equivalent_value (h , value ) : highest_equivalent_value (h , value );
947+ }
948+
930949int hdr_value_at_percentiles (const struct hdr_histogram * h , const double * percentiles , int64_t * values , size_t length )
931950{
932951 if (NULL == percentiles || NULL == values )
933952 {
934953 return EINVAL ;
935954 }
955+ if (HDR_UNLIKELY (h -> total_count == 0 ))
956+ {
957+ for (size_t i = 0 ; i < length ; i ++ )
958+ {
959+ values [i ] = 0 ;
960+ }
961+ return 0 ;
962+ }
936963
937964 struct hdr_iter iter ;
938965 const int64_t total_count = h -> total_count ;
939966 // to avoid allocations we use the values array for intermediate computation
940967 // i.e. to store the expected cumulative count at each percentile
941968 for (size_t i = 0 ; i < length ; i ++ )
942969 {
943- const double requested_percentile = percentiles [i ] < 100.0 ? percentiles [i ] : 100.0 ;
944- const int64_t count_at_percentile =
945- (int64_t ) (((requested_percentile / 100 ) * total_count ) + 0.5 );
946- values [i ] = count_at_percentile > 1 ? count_at_percentile : 1 ;
970+ values [i ] = count_at_percentile (total_count , percentiles [i ]);
947971 }
948972
949973 uint64_t total = 0 ; /* unsigned: no signed-overflow UB when a hostile block sum is added at once */
@@ -974,7 +998,7 @@ int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percen
974998 total += (uint64_t )counts [j ];
975999 while (at_pos < length && (int64_t )total >= values [at_pos ])
9761000 {
977- values [at_pos ] = highest_equivalent_value ( h , hdr_value_at_index (h , j ));
1001+ values [at_pos ] = percentile_bound ( h , percentiles [ at_pos ] , hdr_value_at_index (h , j ));
9781002 at_pos ++ ;
9791003 }
9801004 }
@@ -990,7 +1014,7 @@ int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percen
9901014 total += (uint64_t )counts [idx ];
9911015 while (at_pos < length && (int64_t )total >= values [at_pos ])
9921016 {
993- values [at_pos ] = highest_equivalent_value ( h , hdr_value_at_index (h , idx ));
1017+ values [at_pos ] = percentile_bound ( h , percentiles [ at_pos ] , hdr_value_at_index (h , idx ));
9941018 at_pos ++ ;
9951019 }
9961020 }
@@ -1005,11 +1029,17 @@ int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percen
10051029 total += (uint64_t )iter .count ;
10061030 while (at_pos < length && (int64_t )total >= values [at_pos ])
10071031 {
1008- values [at_pos ] = highest_equivalent_value ( h , iter .value );
1032+ values [at_pos ] = percentile_bound ( h , percentiles [ at_pos ] , iter .value );
10091033 at_pos ++ ;
10101034 }
10111035 }
10121036 }
1037+ /* unreached targets (counts below total_count) get the singular's not-found value */
1038+ for (; at_pos < length ; at_pos ++ )
1039+ {
1040+ values [at_pos ] = percentiles [at_pos ] == 0.0 ?
1041+ lowest_equivalent_value (h , 0 ) : highest_equivalent_value (h , 0 );
1042+ }
10131043 return 0 ;
10141044}
10151045
@@ -1020,6 +1050,11 @@ double hdr_mean(const struct hdr_histogram* h)
10201050 int64_t count = 0 ;
10211051 int64_t total_count = h -> total_count ;
10221052
1053+ if (total_count == 0 )
1054+ {
1055+ return 0.0 ;
1056+ }
1057+
10231058 hdr_iter_init (& iter , h );
10241059
10251060 while (hdr_iter_next (& iter ) && count < total_count )
@@ -1037,6 +1072,11 @@ double hdr_mean(const struct hdr_histogram* h)
10371072
10381073double hdr_stddev (const struct hdr_histogram * h )
10391074{
1075+ if (h -> total_count == 0 )
1076+ {
1077+ return 0.0 ;
1078+ }
1079+
10401080 double mean = hdr_mean (h );
10411081 double geometric_dev_total = 0.0 ;
10421082
0 commit comments